Skip to content

Commit

Permalink
ALL: Add 'u' suffix for unsigned integers
Browse files Browse the repository at this point in the history
  • Loading branch information
MineGame159 committed Feb 24, 2024
1 parent 8917826 commit 2d6cb3e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 3 deletions.
10 changes: 9 additions & 1 deletion core/checker/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ func (c *checker) VisitLiteral(expr *ast.Literal) {
raw := expr.String()
last := raw[len(raw)-1]

if last == 'f' || last == 'F' {
if last == 'u' || last == 'U' {
_, err := strconv.ParseUint(raw[:len(raw)-1], 10, 32)
if err != nil {
c.error(expr, "Invalid unsigned integer")
expr.Result().SetInvalid()
}

kind = ast.U32
} else if last == 'f' || last == 'F' {
_, err := strconv.ParseFloat(raw[:len(raw)-1], 32)
if err != nil {
c.error(expr, "Invalid float")
Expand Down
4 changes: 4 additions & 0 deletions core/codegen/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func (c *codegen) VisitLiteral(expr *ast.Literal) {
raw := expr.String()
last := raw[len(raw)-1]

if last == 'u' || last == 'U' {
v, _ := strconv.ParseUint(raw, 10, 32)
value = &ir.IntConst{Typ: type_, Value: ir.Unsigned(v)}
}
if last == 'f' || last == 'F' {
v, _ := strconv.ParseFloat(raw[:len(raw)-1], 32)
value = &ir.FloatConst{Typ: type_, Value: v}
Expand Down
4 changes: 3 additions & 1 deletion core/scanner/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,9 @@ func (s *Scanner) integerOrFloat() PositionedToken {
}
}

if s.peek() == 'f' || s.peek() == 'F' {
if s.peek() == 'u' || s.peek() == 'U' {
s.advance()
} else if s.peek() == 'f' || s.peek() == 'F' {
s.advance()
}

Expand Down
2 changes: 1 addition & 1 deletion vscode/fireball.tmGrammar.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@
"name": "punctuation.definition.end.bracket.square.fb"
},
"number": {
"match": "\\b(0x[0-9a-zA-Z]+)|(0b(?:0|1)+)|(-?[0-9]+(\\.[0-9]+)?[fF]?)\\b",
"match": "\\b(0x[0-9a-zA-Z]+)|(0b(?:0|1)+)|(-?[0-9]+(\\.[0-9]+)?[uUfF]?)\\b",
"name": "constant.numeric.fb"
},
"character": {
Expand Down

0 comments on commit 2d6cb3e

Please sign in to comment.