Skip to content

Commit a412b21

Browse files
authored
Added built-in names of Python 1 to resolver. (#22)
* Added built-in names of Python 1 to resolver. * Allow use of JavaScript keywords as names.
1 parent 15f1ac1 commit a412b21

File tree

2 files changed

+70
-5
lines changed

2 files changed

+70
-5
lines changed

src/resolver.ts

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,61 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
133133
this.ast = ast;
134134
// The global environment
135135
this.environment = new Environment(source, null, new Map([
136-
["range", new Token(TokenType.NAME, "range", 0, 0, 0)],
136+
// misc library
137+
["get_time", new Token(TokenType.NAME, "get_time", 0, 0, 0)],
137138
["print", new Token(TokenType.NAME, "print", 0, 0, 0)],
138-
["stringify", new Token(TokenType.NAME, "stringify", 0, 0, 0)],
139-
// @TODO add all the source pre-declared names here
139+
["raw_print", new Token(TokenType.NAME, "raw_print", 0, 0, 0)],
140+
["str", new Token(TokenType.NAME, "str", 0, 0, 0)],
141+
["error", new Token(TokenType.NAME, "error", 0, 0, 0)],
142+
["prompt", new Token(TokenType.NAME, "prompt", 0, 0, 0)],
143+
//TODO: add is_integer and is_float to pylib, update resolver and createContext
144+
["is_number", new Token(TokenType.NAME, "is_number", 0, 0, 0)],
145+
["is_string", new Token(TokenType.NAME, "is_string", 0, 0, 0)],
146+
["is_function", new Token(TokenType.NAME, "is_function", 0, 0, 0)],
147+
["is_boolean", new Token(TokenType.NAME, "is_boolean", 0, 0, 0)],
148+
["parse_int", new Token(TokenType.NAME, "parse_int", 0, 0, 0)],
149+
["char_at", new Token(TokenType.NAME, "char_at", 0, 0, 0)],
150+
["arity", new Token(TokenType.NAME, "arity", 0, 0, 0)],
151+
["None", new Token(TokenType.NAME, "None", 0, 0, 0)],
152+
["NaN", new Token(TokenType.NAME, "NaN", 0, 0, 0)],
153+
["Infinity", new Token(TokenType.NAME, "Infinity", 0, 0, 0)],
154+
155+
// math library
156+
["math_abs", new Token(TokenType.NAME, "math_abs", 0, 0, 0)],
157+
["math_acos", new Token(TokenType.NAME, "math_acos", 0, 0, 0)],
158+
["math_acosh", new Token(TokenType.NAME, "math_acosh", 0, 0, 0)],
159+
["math_asin", new Token(TokenType.NAME, "math_asin", 0, 0, 0)],
160+
["math_asinh", new Token(TokenType.NAME, "math_asinh", 0, 0, 0)],
161+
["math_atan", new Token(TokenType.NAME, "math_atan", 0, 0, 0)],
162+
["math_atan2", new Token(TokenType.NAME, "math_atan2", 0, 0, 0)],
163+
["math_atanh", new Token(TokenType.NAME, "math_atanh", 0, 0, 0)],
164+
["math_cbrt", new Token(TokenType.NAME, "math_cbrt", 0, 0, 0)],
165+
["math_ceil", new Token(TokenType.NAME, "math_ceil", 0, 0, 0)],
166+
["math_clz32", new Token(TokenType.NAME, "math_clz32", 0, 0, 0)],
167+
["math_cos", new Token(TokenType.NAME, "math_cos", 0, 0, 0)],
168+
["math_cosh", new Token(TokenType.NAME, "math_cosh", 0, 0, 0)],
169+
["math_exp", new Token(TokenType.NAME, "math_exp", 0, 0, 0)],
170+
["math_expm1", new Token(TokenType.NAME, "math_expm1", 0, 0, 0)],
171+
["math_floor", new Token(TokenType.NAME, "math_floor", 0, 0, 0)],
172+
["math_fround", new Token(TokenType.NAME, "math_fround", 0, 0, 0)],
173+
["math_hypot", new Token(TokenType.NAME, "math_hypot", 0, 0, 0)],
174+
["math_imul", new Token(TokenType.NAME, "math_imul", 0, 0, 0)],
175+
["math_log", new Token(TokenType.NAME, "math_log", 0, 0, 0)],
176+
["math_log1p", new Token(TokenType.NAME, "math_log1p", 0, 0, 0)],
177+
["math_log2", new Token(TokenType.NAME, "math_log2", 0, 0, 0)],
178+
["math_log10", new Token(TokenType.NAME, "math_log10", 0, 0, 0)],
179+
["math_max", new Token(TokenType.NAME, "math_max", 0, 0, 0)],
180+
["math_min", new Token(TokenType.NAME, "math_min", 0, 0, 0)],
181+
["math_pow", new Token(TokenType.NAME, "math_pow", 0, 0, 0)],
182+
["math_random", new Token(TokenType.NAME, "math_random", 0, 0, 0)],
183+
["math_round", new Token(TokenType.NAME, "math_round", 0, 0, 0)],
184+
["math_sign", new Token(TokenType.NAME, "math_sign", 0, 0, 0)],
185+
["math_sin", new Token(TokenType.NAME, "math_sin", 0, 0, 0)],
186+
["math_sinh", new Token(TokenType.NAME, "math_sinh", 0, 0, 0)],
187+
["math_sqrt", new Token(TokenType.NAME, "math_sqrt", 0, 0, 0)],
188+
["math_tan", new Token(TokenType.NAME, "math_tan", 0, 0, 0)],
189+
["math_tanh", new Token(TokenType.NAME, "math_tanh", 0, 0, 0)],
190+
["math_trunc", new Token(TokenType.NAME, "math_trunc", 0, 0, 0)],
140191
]));
141192
this.functionScope = null;
142193
}

src/translator.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,32 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base
124124

125125
// Converts our internal identifier to estree identifier.
126126
private rawStringToIdentifier(name: string, stmtOrExpr: Stmt | Expr): Identifier {
127+
const keywords = new Set<String>(['abstract', 'arguments', 'await', 'boolean', 'byte',
128+
'case', 'catch', 'char', 'const', 'debugger', 'default', 'delete', 'do', 'double', 'enum',
129+
'eval', 'export', 'extends', 'false', 'final', 'float', 'function', 'goto', 'implements',
130+
'instanceof', 'int', 'interface', 'let', 'long', 'native', 'new', 'null', 'package',
131+
'private', 'protected', 'public', 'short', 'static', 'super', 'switch', 'synchronized', 'this',
132+
'throw', 'throws', 'transient', 'true', 'typeof', 'var', 'void', 'volatile'])
133+
127134
return {
128135
type: 'Identifier',
129-
name: name,
136+
name: keywords.has(name) ? '$' + name : name,
130137
loc: this.toEstreeLocation(stmtOrExpr),
131138
};
132139
}
133140

134141
// Token to estree identifier.
135142
private convertToIdentifier(name: Token): Identifier {
143+
const keywords = new Set<String>(['abstract', 'arguments', 'await', 'boolean', 'byte',
144+
'case', 'catch', 'char', 'const', 'debugger', 'default', 'delete', 'do', 'double', 'enum',
145+
'eval', 'export', 'extends', 'false', 'final', 'float', 'function', 'goto', 'implements',
146+
'instanceof', 'int', 'interface', 'let', 'long', 'native', 'new', 'null', 'package',
147+
'private', 'protected', 'public', 'short', 'static', 'super', 'switch', 'synchronized', 'this',
148+
'throw', 'throws', 'transient', 'true', 'typeof', 'var', 'void', 'volatile'])
149+
136150
return {
137151
type: 'Identifier',
138-
name: name.lexeme,
152+
name: keywords.has(name.lexeme) ? '$' + name.lexeme : name.lexeme,
139153
loc: this.tokenToEstreeLocation(name),
140154
};
141155
}

0 commit comments

Comments
 (0)