Skip to content
This repository was archived by the owner on Jul 12, 2025. It is now read-only.

Commit a15a35c

Browse files
committed
Using uint8 as token type to reduce memory usage
1 parent 557bbca commit a15a35c

File tree

4 files changed

+153
-76
lines changed

4 files changed

+153
-76
lines changed

compiler/parser/expression_parsing.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ func (p *Parser) parseInfixExpression(left ast.Expression) ast.Expression {
266266
preced := p.curPrecedence()
267267

268268
// prevent "* *" from being parsed
269-
if p.curToken.Literal == token.Asterisk && p.peekToken.Literal == token.Asterisk {
269+
if p.curToken.Literal == token.Asterisk.String() && p.peekToken.Literal == token.Asterisk.String() {
270270
msg := fmt.Sprintf("unexpected %s Line: %d", p.curToken.Literal, p.peekToken.Line)
271271
p.error = errors.InitError(msg, errors.UnexpectedTokenError)
272272
return nil

compiler/parser/flow_control_parsing.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ func (p *Parser) parseCaseConditional(base ast.Expression) *ast.ConditionalExpre
7474

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

7979
for p.peekTokenIs(token.Comma) {
8080
p.nextToken()
8181
p.nextToken()
8282

8383
right := p.parseExpression(precedence.Normal)
84-
rightInfix := newInfixExpression(base, token.Token{Type: token.Eq, Literal: token.Eq}, right)
85-
infix = newInfixExpression(infix, token.Token{Type: token.Or, Literal: token.Or}, rightInfix)
84+
rightInfix := newInfixExpression(base, token.Token{Type: token.Eq, Literal: token.Eq.String()}, right)
85+
infix = newInfixExpression(infix, token.Token{Type: token.Or, Literal: token.Or.String()}, rightInfix)
8686
}
8787

8888
return infix

compiler/token/token.go

Lines changed: 148 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package token
22

33
// Type is used to determine token type
4-
type Type string
4+
type Type uint8
55

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

1313
// Literals
1414
const (
15-
Illegal = "ILLEGAL"
16-
EOF = "EOF"
17-
18-
Constant = "CONSTANT"
19-
Ident = "IDENT"
20-
InstanceVariable = "INSTANCE_VAR"
21-
Int = "INT"
22-
Float = "FLOAT"
23-
String = "STRING"
24-
Comment = "COMMENT"
25-
26-
Assign = "="
27-
Plus = "+"
28-
PlusEq = "+="
29-
Minus = "-"
30-
MinusEq = "-="
31-
Bang = "!"
32-
Asterisk = "*"
33-
Pow = "**"
34-
Slash = "/"
35-
Dot = "."
36-
And = "&&"
37-
Or = "||"
38-
OrEq = "||="
39-
Modulo = "%"
40-
41-
LT = "<"
42-
LTE = "<="
43-
GT = ">"
44-
GTE = ">="
45-
COMP = "<=>"
46-
47-
Comma = ","
48-
Semicolon = ";"
49-
Colon = ":"
50-
Bar = "|"
51-
52-
LParen = "("
53-
RParen = ")"
54-
LBrace = "{"
55-
RBrace = "}"
56-
LBracket = "["
57-
RBracket = "]"
58-
59-
Eq = "=="
60-
NotEq = "!="
61-
Range = ".."
62-
63-
True = "TRUE"
64-
False = "FALSE"
65-
Null = "Null"
66-
If = "IF"
67-
ElsIf = "ELSIF"
68-
Else = "ELSE"
69-
Case = "CASE"
70-
When = "WHEN"
71-
Return = "RETURN"
72-
Next = "NEXT"
73-
Break = "BREAK"
74-
Def = "DEF"
75-
Self = "SELF"
76-
End = "END"
77-
While = "WHILE"
78-
Do = "DO"
79-
Yield = "YIELD"
80-
GetBlock = "GET_BLOCK"
81-
Class = "CLASS"
82-
Module = "MODULE"
83-
84-
ResolutionOperator = "::"
15+
Illegal Type = iota
16+
EOF
17+
18+
Constant
19+
Ident
20+
InstanceVariable
21+
Int
22+
Float
23+
String
24+
Comment
25+
26+
Assign
27+
Plus
28+
PlusEq
29+
Minus
30+
MinusEq
31+
Bang
32+
Asterisk
33+
Pow
34+
Slash
35+
Dot
36+
And
37+
Or
38+
OrEq
39+
Modulo
40+
41+
LT
42+
LTE
43+
GT
44+
GTE
45+
COMP
46+
47+
Comma
48+
Semicolon
49+
Colon
50+
Bar
51+
52+
LParen
53+
RParen
54+
LBrace
55+
RBrace
56+
LBracket
57+
RBracket
58+
59+
Eq
60+
NotEq
61+
Range
62+
63+
True
64+
False
65+
Null
66+
If
67+
ElsIf
68+
Else
69+
Case
70+
When
71+
Return
72+
Next
73+
Break
74+
Def
75+
Self
76+
End
77+
While
78+
Do
79+
Yield
80+
GetBlock
81+
Class
82+
Module
83+
84+
ResolutionOperator
8585
)
8686

87+
var tokenMap = map[Type]string{
88+
Illegal: "ILLEGAL",
89+
EOF: "EOF",
90+
91+
Constant: "CONSTANT",
92+
Ident: "IDENT",
93+
InstanceVariable: "INSTANCE_VAR",
94+
Int: "INT",
95+
Float: "FLOAT",
96+
String: "STRING",
97+
Comment: "COMMENT",
98+
99+
Assign: "=",
100+
Plus: "+",
101+
PlusEq: "+=",
102+
Minus: "-",
103+
MinusEq: "-=",
104+
Bang: "!",
105+
Asterisk: "*",
106+
Pow: "**",
107+
Slash: "/",
108+
Dot: ".",
109+
And: "&&",
110+
Or: "||",
111+
OrEq: "||=",
112+
Modulo: "%",
113+
114+
LT: "<",
115+
LTE: "<=",
116+
GT: ">",
117+
GTE: ">=",
118+
COMP: "<=>",
119+
120+
Comma: ",",
121+
Semicolon: ";",
122+
Colon: ":",
123+
Bar: "|",
124+
125+
LParen: "(",
126+
RParen: ")",
127+
LBrace: "{",
128+
RBrace: "}",
129+
LBracket: "[",
130+
RBracket: "]",
131+
132+
Eq: "==",
133+
NotEq: "!=",
134+
Range: "..",
135+
136+
True: "TRUE",
137+
False: "FALSE",
138+
Null: "Null",
139+
If: "IF",
140+
ElsIf: "ELSIF",
141+
Else: "ELSE",
142+
Case: "CASE",
143+
When: "WHEN",
144+
Return: "RETURN",
145+
Next: "NEXT",
146+
Break: "BREAK",
147+
Def: "DEF",
148+
Self: "SELF",
149+
End: "END",
150+
While: "WHILE",
151+
Do: "DO",
152+
Yield: "YIELD",
153+
GetBlock: "GET_BLOCK",
154+
Class: "CLASS",
155+
Module: "MODULE",
156+
157+
ResolutionOperator: "::",
158+
}
159+
160+
func (typ Type) String() string {
161+
return tokenMap[typ]
162+
}
163+
87164
var keywords = map[string]Type{
88165
"def": Def,
89166
"true": True,

native/ripper/ripper.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ func convertLex(t token.Type) string {
345345
case token.Slash:
346346
s = "slash"
347347
default:
348-
s = strings.ToLower(string(t))
348+
s = strings.ToLower(t.String())
349349
}
350350

351351
return "on_" + s

0 commit comments

Comments
 (0)