Skip to content

Commit

Permalink
Using uint8 as token type to reduce memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
aisk committed Dec 15, 2020
1 parent 557bbca commit b157fb4
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 75 deletions.
2 changes: 1 addition & 1 deletion compiler/parser/expression_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ func (p *Parser) parseInfixExpression(left ast.Expression) ast.Expression {
preced := p.curPrecedence()

// prevent "* *" from being parsed
if p.curToken.Literal == token.Asterisk && p.peekToken.Literal == token.Asterisk {
if p.curToken.Literal == token.Asterisk.String() && p.peekToken.Literal == token.Asterisk.String() {
msg := fmt.Sprintf("unexpected %s Line: %d", p.curToken.Literal, p.peekToken.Line)
p.error = errors.InitError(msg, errors.UnexpectedTokenError)
return nil
Expand Down
6 changes: 3 additions & 3 deletions compiler/parser/flow_control_parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ func (p *Parser) parseCaseConditional(base ast.Expression) *ast.ConditionalExpre

func (p *Parser) parseCaseCondition(base ast.Expression) *ast.InfixExpression {
first := p.parseExpression(precedence.Normal)
infix := newInfixExpression(base, token.Token{Type: token.Eq, Literal: token.Eq}, first)
infix := newInfixExpression(base, token.Token{Type: token.Eq, Literal: token.Eq.String()}, first)

for p.peekTokenIs(token.Comma) {
p.nextToken()
p.nextToken()

right := p.parseExpression(precedence.Normal)
rightInfix := newInfixExpression(base, token.Token{Type: token.Eq, Literal: token.Eq}, right)
infix = newInfixExpression(infix, token.Token{Type: token.Or, Literal: token.Or}, rightInfix)
rightInfix := newInfixExpression(base, token.Token{Type: token.Eq, Literal: token.Eq.String()}, right)
infix = newInfixExpression(infix, token.Token{Type: token.Or, Literal: token.Or.String()}, rightInfix)
}

return infix
Expand Down
219 changes: 148 additions & 71 deletions compiler/token/token.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package token

// Type is used to determine token type
type Type string
type Type uint8

// Token is structure for identifying input stream of characters
type Token struct {
Expand All @@ -12,78 +12,155 @@ type Token struct {

// Literals
const (
Illegal = "ILLEGAL"
EOF = "EOF"

Constant = "CONSTANT"
Ident = "IDENT"
InstanceVariable = "INSTANCE_VAR"
Int = "INT"
Float = "FLOAT"
String = "STRING"
Comment = "COMMENT"

Assign = "="
Plus = "+"
PlusEq = "+="
Minus = "-"
MinusEq = "-="
Bang = "!"
Asterisk = "*"
Pow = "**"
Slash = "/"
Dot = "."
And = "&&"
Or = "||"
OrEq = "||="
Modulo = "%"

LT = "<"
LTE = "<="
GT = ">"
GTE = ">="
COMP = "<=>"

Comma = ","
Semicolon = ";"
Colon = ":"
Bar = "|"

LParen = "("
RParen = ")"
LBrace = "{"
RBrace = "}"
LBracket = "["
RBracket = "]"

Eq = "=="
NotEq = "!="
Range = ".."

True = "TRUE"
False = "FALSE"
Null = "Null"
If = "IF"
ElsIf = "ELSIF"
Else = "ELSE"
Case = "CASE"
When = "WHEN"
Return = "RETURN"
Next = "NEXT"
Break = "BREAK"
Def = "DEF"
Self = "SELF"
End = "END"
While = "WHILE"
Do = "DO"
Yield = "YIELD"
GetBlock = "GET_BLOCK"
Class = "CLASS"
Module = "MODULE"

ResolutionOperator = "::"
Illegal Type = iota
EOF

Constant
Ident
InstanceVariable
Int
Float
String
Comment

Assign
Plus
PlusEq
Minus
MinusEq
Bang
Asterisk
Pow
Slash
Dot
And
Or
OrEq
Modulo

LT
LTE
GT
GTE
COMP

Comma
Semicolon
Colon
Bar

LParen
RParen
LBrace
RBrace
LBracket
RBracket

Eq
NotEq
Range

True
False
Null
If
ElsIf
Else
Case
When
Return
Next
Break
Def
Self
End
While
Do
Yield
GetBlock
Class
Module

ResolutionOperator
)

var tokenMap = map[Type]string{
Illegal: "ILLEGAL",
EOF: "EOF",

Constant: "CONSTANT",
Ident: "IDENT",
InstanceVariable: "INSTANCE_VAR",
Int: "INT",
Float: "FLOAT",
String: "STRING",
Comment: "COMMENT",

Assign: "=",
Plus: "+",
PlusEq: "+=",
Minus: "-",
MinusEq: "-=",
Bang: "!",
Asterisk: "*",
Pow: "**",
Slash: "/",
Dot: ".",
And: "&&",
Or: "||",
OrEq: "||=",
Modulo: "%",

LT: "<",
LTE: "<=",
GT: ">",
GTE: ">=",
COMP: "<=>",

Comma: ",",
Semicolon: ";",
Colon: ":",
Bar: "|",

LParen: "(",
RParen: ")",
LBrace: "{",
RBrace: "}",
LBracket: "[",
RBracket: "]",

Eq: "==",
NotEq: "!=",
Range: "..",

True: "TRUE",
False: "FALSE",
Null: "Null",
If: "IF",
ElsIf: "ELSIF",
Else: "ELSE",
Case: "CASE",
When: "WHEN",
Return: "RETURN",
Next: "NEXT",
Break: "BREAK",
Def: "DEF",
Self: "SELF",
End: "END",
While: "WHILE",
Do: "DO",
Yield: "YIELD",
GetBlock: "GET_BLOCK",
Class: "CLASS",
Module: "MODULE",

ResolutionOperator: "::",
}

func (typ Type) String() string {
return tokenMap[typ]
}

var keywords = map[string]Type{
"def": Def,
"true": True,
Expand Down

0 comments on commit b157fb4

Please sign in to comment.