Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using uint8 as token type to reduce memory usage #872

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 1 addition & 1 deletion native/ripper/ripper.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ func convertLex(t token.Type) string {
case token.Slash:
s = "slash"
default:
s = strings.ToLower(string(t))
s = strings.ToLower(t.String())
}

return "on_" + s
Expand Down