Skip to content

Commit

Permalink
Hex and exp-number-constants #124
Browse files Browse the repository at this point in the history
  • Loading branch information
dbaumgarten committed Jun 26, 2022
1 parent 382afdc commit 32b7918
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/js/prism-nolol.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ Prism.languages.nolol = {
'operator': /\b(and|or|not)\b/i,
'function': /[a-z0-9_]+(?=\()/i,
'variable': /:[a-zA-Z0-9_:.]+|^[a-zA-Z]+[a-zA-Z0-9_.]*/,
'constant': /[0-9]+(\.[0-9]+)?/
'constant': /(([0-9]+(\.[0-9]+)?)e[0-9]+)|(0x[0-9a-fA-F]+)|(([0-9]+(\.[0-9]+)?))/
};
2 changes: 1 addition & 1 deletion docs/js/prism-yolol.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ Prism.languages.yolol = {
'operator': /(?<=[^a-zA-Z0-9_:.])(and|or)(?=[^a-zA-Z0-9_:.])/i,
'function': /[a-z0-9_]+(?=\()/i,
'variable': /:[a-zA-Z0-9_:.]+|^[a-zA-Z]+[a-zA-Z0-9_.]*/,
'constant': /[0-9]+(\.[0-9]+)?/
'constant': /(([0-9]+(\.[0-9]+)?)e[0-9]+)|(0x[0-9a-fA-F]+)|(([0-9]+(\.[0-9]+)?))/
};
3 changes: 3 additions & 0 deletions examples/yolol/operations5.yolol
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
:testhex=0xFF==255
:testsci=1.2e2==120
:done=1
7 changes: 7 additions & 0 deletions examples/yolol/operations5_test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
scripts:
- operations5.yolol
cases:
- name: TestOperationSuccess
outputs:
testhex: 1
testsci: 1
26 changes: 26 additions & 0 deletions pkg/number/number.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,32 @@ const decimals = 3

// FromString parses a string into a Number
func FromString(str string) (Number, error) {

// hex number
if strings.HasPrefix(str, "0x") {
i, err := strconv.ParseInt(str, 0, 64)
if err != nil {
return Zero, err
}
return FromInt(int(i)), nil
}

// scientific notation
if strings.Contains(str, "e") {
parts := strings.Split(str, "e")
num, err := strconv.ParseFloat(parts[0], 64)
if err != nil {
return Zero, err
}
exp, err := strconv.ParseInt(parts[1], 10, 64)
if err != nil {
return Zero, err
}
result := num * math.Pow10(int(exp))
return FromFloat64(result), nil
}

// regular number
parts := strings.Split(str, ".")
switch len(parts) {
case 1:
Expand Down
20 changes: 20 additions & 0 deletions pkg/number/number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,26 @@ func TestFromString(t *testing.T) {
teststr: "649.094",
want: FromFloat64(649.094),
},
{
teststr: "0xFF",
want: FromInt(255),
},
{
teststr: "0x0",
want: FromInt(0),
},
{
teststr: "0x01",
want: FromInt(1),
},
{
teststr: "3e2",
want: FromInt(300),
},
{
teststr: "1.2e2",
want: FromInt(120),
},
}
for _, tt := range tests {
got, err := FromString(tt.teststr)
Expand Down
19 changes: 18 additions & 1 deletion pkg/parser/ast/tokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ var identifierRegex = regexp.MustCompile("^:[a-zA-Z0-9_:.]+|^[a-zA-Z]+[a-zA-Z0-9

var numberRegex = regexp.MustCompile("^[0-9]+(\\.[0-9]+)?|^\\.[0-9]+")

var hexNumberRegex = regexp.MustCompile("^0x([0-9a-fA-F]+)")

var scientificNumberRegex = regexp.MustCompile("(^[0-9]+(\\.[0-9]+)?|^\\.[0-9]+)e(\\d+)")

var commentRegex = regexp.MustCompile("^\\/\\/([^\n]*)")

var whitespaceRegex = regexp.MustCompile("^[ \\t\r]+")
Expand Down Expand Up @@ -325,7 +329,20 @@ func (t *Tokenizer) getStringConstant() (*Token, int) {
}

func (t *Tokenizer) getNumberConstant() (*Token, int) {
found := t.NumberRegex.Find(t.remaining)
//hex-number
found := hexNumberRegex.Find(t.remaining)
if found != nil {
return t.newToken(TypeNumber, string(found)), len(found)
}

//scientific-number
found = scientificNumberRegex.Find(t.remaining)
if found != nil {
return t.newToken(TypeNumber, string(found)), len(found)
}

// normal number
found = t.NumberRegex.Find(t.remaining)
if found != nil {
return t.newToken(TypeNumber, string(found)), len(found)
}
Expand Down
12 changes: 12 additions & 0 deletions pkg/parser/ast/tokenizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ func TestTokenizer(t *testing.T) {
:var = "another--test"
:foo = (1+2.75)*3
:_=1
hexnum=oxFE
scinum=1.5e2
`

expected := `Line: 1, Coloumn: 1, Type: Newline
Expand Down Expand Up @@ -122,6 +124,16 @@ func TestTokenizer(t *testing.T) {
Line: 11, Coloumn: 5, Type: Number, Value: '1'
Line: 11, Coloumn: 6, Type: Newline
Line: 12, Coloumn: 1, Type: Whitespace, Value: ' '
Line: 12, Coloumn: 2, Type: ID, Value: 'hexnum'
Line: 12, Coloumn: 8, Type: Symbol, Value: '='
Line: 12, Coloumn: 9, Type: ID, Value: 'oxFE'
Line: 12, Coloumn: 13, Type: Newline
Line: 13, Coloumn: 1, Type: Whitespace, Value: ' '
Line: 13, Coloumn: 2, Type: ID, Value: 'scinum'
Line: 13, Coloumn: 8, Type: Symbol, Value: '='
Line: 13, Coloumn: 9, Type: Number, Value: '1.5e2'
Line: 13, Coloumn: 14, Type: Newline
Line: 14, Coloumn: 1, Type: Whitespace, Value: ' '
`

tk := ast.NewTokenizer()
Expand Down
2 changes: 1 addition & 1 deletion vscode-yolol/syntaxes/nolol.tmGrammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"name": "string"
},
"constant": {
"match": "[0-9]+(\\.[0-9]+)?",
"match": "(([0-9]+(\\.[0-9]+)?)e[0-9]+)|(0x[0-9a-fA-F]+)|(([0-9]+(\\.[0-9]+)?))",
"name": "constant.numeric"
},
"function": {
Expand Down
2 changes: 1 addition & 1 deletion vscode-yolol/syntaxes/yolol.tmGrammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"name": "string"
},
"constant": {
"match": "[0-9]+(\\.[0-9]+)?",
"match": "(([0-9]+(\\.[0-9]+)?)e[0-9]+)|(0x[0-9a-fA-F]+)|(([0-9]+(\\.[0-9]+)?))",
"name": "constant.numeric"
},
"function": {
Expand Down

0 comments on commit 32b7918

Please sign in to comment.