Skip to content

Commit

Permalink
added range keyword
Browse files Browse the repository at this point in the history
  • Loading branch information
Glowman554 committed Jul 30, 2024
1 parent 434507e commit 53f9d2b
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 6 deletions.
7 changes: 7 additions & 0 deletions fire/firestorm/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ func (l *Lexer) Tokenize() []lexer.Token {
l.reverse()
tokens = append(tokens, lexer.NewToken(lexer.MORE, nil, l.pos))
}
case '.':
l.advance()
if l.current == '.' {
tokens = append(tokens, lexer.NewToken(lexer.RANGE_DOT, nil, l.pos))
} else {
panic("Illegal token " + string(l.current))
}
case '<':
l.advance()
if l.current == '=' {
Expand Down
2 changes: 2 additions & 0 deletions fire/firestorm/lexer/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (

INCREASE
DECREASE

RANGE_DOT
)

type Token struct {
Expand Down
57 changes: 57 additions & 0 deletions fire/firestorm/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,63 @@ func (p *Parser) keyword() []*parser.Node {
codeBlock := p.codeBlock()
p.expect(lexer.RBRACE)
return []*parser.Node{parser.NewNode(parser.END_EXEC, nil, nil, codeBlock)}
case "range":
p.advance()
from := p.expression()
p.expect(lexer.RANGE_DOT)

p.advance()
to := p.expression()
p.expect(lexer.ID)

if p.current.Value.(string) != "as" {
p.error("Expected as", p.current.Pos)
}
p.advanceExpect(lexer.ID)

as := p.current.Value.(string)
p.advanceExpect(lexer.ID)

modeStr := p.current.Value.(string)
mode, err := parser.GetRangeMode(modeStr)
if err != nil {
p.error(err.Error(), p.current.Pos)
}
p.advanceExpect(lexer.LBRACE)

codeBlock := p.codeBlock()
switch mode {
case parser.UP:
codeBlock = append(codeBlock, parser.NewNode(parser.VARIABLE_INCREASE, nil, nil, as))
case parser.DOWN:
codeBlock = append(codeBlock, parser.NewNode(parser.VARIABLE_DECREASE, nil, nil, as))
default:
panic("?")
}
p.expect(lexer.RBRACE)

ret := []*parser.Node{
parser.NewNode(parser.VARIABLE_DECLARATION, from, nil, parser.NamedDatatype{
UnnamedDatatype: parser.UnnamedDatatype{
Type: parser.INT,
IsArray: false,
},
Name: as,
}),
}

switch mode {
case parser.UP:
ret = append(ret, parser.NewNode(parser.CONDITIONAL_LOOP, parser.NewNode(parser.COMPARE,
parser.NewNode(parser.VARIABLE_LOOKUP, nil, nil, as), to, parser.Less), nil, codeBlock))
case parser.DOWN:
ret = append(ret, parser.NewNode(parser.CONDITIONAL_LOOP, parser.NewNode(parser.COMPARE,
parser.NewNode(parser.VARIABLE_LOOKUP, nil, nil, as), to, parser.More), nil, codeBlock))
default:
panic("?")
}

return ret
default:
return nil
}
Expand Down
5 changes: 2 additions & 3 deletions fire/firestorm/parser/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ import (
type Compare int

const (
Invalid = -1
More Compare = iota
More Compare = iota
Less
MoreEquals
LessEquals
Expand All @@ -33,6 +32,6 @@ func TokenTypeToCompare(t lexer.TokenType) (Compare, error) {
case lexer.NOT_EQUALS:
return NotEquals, nil
default:
return Invalid, fmt.Errorf("Invalid compare " + strconv.Itoa(int(t)))
return 0, fmt.Errorf("Invalid compare " + strconv.Itoa(int(t)))
}
}
5 changes: 2 additions & 3 deletions fire/firestorm/parser/datatype.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import (
type DataType int

const (
INVALID = -1
INT DataType = iota
INT DataType = iota
STR
VOID
CHR
Expand All @@ -35,7 +34,7 @@ func GetDatatypeFromString(t string) (DataType, error) {
case "i16":
return INT_16, nil
default:
return INVALID, fmt.Errorf("Invalid datatype " + t)
return 0, fmt.Errorf("Invalid datatype " + t)
}
}

Expand Down
21 changes: 21 additions & 0 deletions fire/firestorm/parser/range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package parser

import "fmt"

type RangeMode int

const (
UP = iota
DOWN
)

func GetRangeMode(t string) (DataType, error) {
switch t {
case "up":
return UP, nil
case "down":
return DOWN, nil
default:
return 0, fmt.Errorf("Invalid mode " + t)
}
}
2 changes: 2 additions & 0 deletions fire/firestorm/parser/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ const (
END_EXEC

OFFSET

RANGE_LOOP
)

type Node struct {
Expand Down

0 comments on commit 53f9d2b

Please sign in to comment.