-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
63 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package common | ||
|
||
import ( | ||
"strconv" | ||
"text/scanner" | ||
) | ||
|
||
type Literal interface { | ||
Value() interface{} | ||
} | ||
|
||
type BasicLit struct { | ||
Type rune | ||
Text string | ||
} | ||
|
||
func (lit *BasicLit) Value() interface{} { | ||
switch lit.Type { | ||
case scanner.Int, scanner.Float: | ||
value, err := strconv.ParseFloat(lit.Text, 64) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return value | ||
|
||
case scanner.String: | ||
value, err := strconv.Unquote(lit.Text) | ||
if err != nil { | ||
panic(err) | ||
} | ||
return value | ||
|
||
case scanner.Ident: | ||
switch lit.Text { | ||
case "true": | ||
return true | ||
case "false": | ||
return false | ||
default: | ||
return lit.Text | ||
} | ||
|
||
default: | ||
panic("invalid literal") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters