Skip to content

Commit

Permalink
Added built-in names of Python 1 to resolver. (#22)
Browse files Browse the repository at this point in the history
* Added built-in names of Python 1 to resolver.

* Allow use of JavaScript keywords as names.
  • Loading branch information
JJtan2002 authored Apr 3, 2024
1 parent 15f1ac1 commit a412b21
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 5 deletions.
57 changes: 54 additions & 3 deletions src/resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,61 @@ export class Resolver implements StmtNS.Visitor<void>, ExprNS.Visitor<void> {
this.ast = ast;
// The global environment
this.environment = new Environment(source, null, new Map([
["range", new Token(TokenType.NAME, "range", 0, 0, 0)],
// misc library
["get_time", new Token(TokenType.NAME, "get_time", 0, 0, 0)],
["print", new Token(TokenType.NAME, "print", 0, 0, 0)],
["stringify", new Token(TokenType.NAME, "stringify", 0, 0, 0)],
// @TODO add all the source pre-declared names here
["raw_print", new Token(TokenType.NAME, "raw_print", 0, 0, 0)],
["str", new Token(TokenType.NAME, "str", 0, 0, 0)],
["error", new Token(TokenType.NAME, "error", 0, 0, 0)],
["prompt", new Token(TokenType.NAME, "prompt", 0, 0, 0)],
//TODO: add is_integer and is_float to pylib, update resolver and createContext
["is_number", new Token(TokenType.NAME, "is_number", 0, 0, 0)],
["is_string", new Token(TokenType.NAME, "is_string", 0, 0, 0)],
["is_function", new Token(TokenType.NAME, "is_function", 0, 0, 0)],
["is_boolean", new Token(TokenType.NAME, "is_boolean", 0, 0, 0)],
["parse_int", new Token(TokenType.NAME, "parse_int", 0, 0, 0)],
["char_at", new Token(TokenType.NAME, "char_at", 0, 0, 0)],
["arity", new Token(TokenType.NAME, "arity", 0, 0, 0)],
["None", new Token(TokenType.NAME, "None", 0, 0, 0)],
["NaN", new Token(TokenType.NAME, "NaN", 0, 0, 0)],
["Infinity", new Token(TokenType.NAME, "Infinity", 0, 0, 0)],

// math library
["math_abs", new Token(TokenType.NAME, "math_abs", 0, 0, 0)],
["math_acos", new Token(TokenType.NAME, "math_acos", 0, 0, 0)],
["math_acosh", new Token(TokenType.NAME, "math_acosh", 0, 0, 0)],
["math_asin", new Token(TokenType.NAME, "math_asin", 0, 0, 0)],
["math_asinh", new Token(TokenType.NAME, "math_asinh", 0, 0, 0)],
["math_atan", new Token(TokenType.NAME, "math_atan", 0, 0, 0)],
["math_atan2", new Token(TokenType.NAME, "math_atan2", 0, 0, 0)],
["math_atanh", new Token(TokenType.NAME, "math_atanh", 0, 0, 0)],
["math_cbrt", new Token(TokenType.NAME, "math_cbrt", 0, 0, 0)],
["math_ceil", new Token(TokenType.NAME, "math_ceil", 0, 0, 0)],
["math_clz32", new Token(TokenType.NAME, "math_clz32", 0, 0, 0)],
["math_cos", new Token(TokenType.NAME, "math_cos", 0, 0, 0)],
["math_cosh", new Token(TokenType.NAME, "math_cosh", 0, 0, 0)],
["math_exp", new Token(TokenType.NAME, "math_exp", 0, 0, 0)],
["math_expm1", new Token(TokenType.NAME, "math_expm1", 0, 0, 0)],
["math_floor", new Token(TokenType.NAME, "math_floor", 0, 0, 0)],
["math_fround", new Token(TokenType.NAME, "math_fround", 0, 0, 0)],
["math_hypot", new Token(TokenType.NAME, "math_hypot", 0, 0, 0)],
["math_imul", new Token(TokenType.NAME, "math_imul", 0, 0, 0)],
["math_log", new Token(TokenType.NAME, "math_log", 0, 0, 0)],
["math_log1p", new Token(TokenType.NAME, "math_log1p", 0, 0, 0)],
["math_log2", new Token(TokenType.NAME, "math_log2", 0, 0, 0)],
["math_log10", new Token(TokenType.NAME, "math_log10", 0, 0, 0)],
["math_max", new Token(TokenType.NAME, "math_max", 0, 0, 0)],
["math_min", new Token(TokenType.NAME, "math_min", 0, 0, 0)],
["math_pow", new Token(TokenType.NAME, "math_pow", 0, 0, 0)],
["math_random", new Token(TokenType.NAME, "math_random", 0, 0, 0)],
["math_round", new Token(TokenType.NAME, "math_round", 0, 0, 0)],
["math_sign", new Token(TokenType.NAME, "math_sign", 0, 0, 0)],
["math_sin", new Token(TokenType.NAME, "math_sin", 0, 0, 0)],
["math_sinh", new Token(TokenType.NAME, "math_sinh", 0, 0, 0)],
["math_sqrt", new Token(TokenType.NAME, "math_sqrt", 0, 0, 0)],
["math_tan", new Token(TokenType.NAME, "math_tan", 0, 0, 0)],
["math_tanh", new Token(TokenType.NAME, "math_tanh", 0, 0, 0)],
["math_trunc", new Token(TokenType.NAME, "math_trunc", 0, 0, 0)],
]));
this.functionScope = null;
}
Expand Down
18 changes: 16 additions & 2 deletions src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,18 +124,32 @@ export class Translator implements StmtNS.Visitor<BaseNode>, ExprNS.Visitor<Base

// Converts our internal identifier to estree identifier.
private rawStringToIdentifier(name: string, stmtOrExpr: Stmt | Expr): Identifier {
const keywords = new Set<String>(['abstract', 'arguments', 'await', 'boolean', 'byte',
'case', 'catch', 'char', 'const', 'debugger', 'default', 'delete', 'do', 'double', 'enum',
'eval', 'export', 'extends', 'false', 'final', 'float', 'function', 'goto', 'implements',
'instanceof', 'int', 'interface', 'let', 'long', 'native', 'new', 'null', 'package',
'private', 'protected', 'public', 'short', 'static', 'super', 'switch', 'synchronized', 'this',
'throw', 'throws', 'transient', 'true', 'typeof', 'var', 'void', 'volatile'])

return {
type: 'Identifier',
name: name,
name: keywords.has(name) ? '$' + name : name,
loc: this.toEstreeLocation(stmtOrExpr),
};
}

// Token to estree identifier.
private convertToIdentifier(name: Token): Identifier {
const keywords = new Set<String>(['abstract', 'arguments', 'await', 'boolean', 'byte',
'case', 'catch', 'char', 'const', 'debugger', 'default', 'delete', 'do', 'double', 'enum',
'eval', 'export', 'extends', 'false', 'final', 'float', 'function', 'goto', 'implements',
'instanceof', 'int', 'interface', 'let', 'long', 'native', 'new', 'null', 'package',
'private', 'protected', 'public', 'short', 'static', 'super', 'switch', 'synchronized', 'this',
'throw', 'throws', 'transient', 'true', 'typeof', 'var', 'void', 'volatile'])

return {
type: 'Identifier',
name: name.lexeme,
name: keywords.has(name.lexeme) ? '$' + name.lexeme : name.lexeme,
loc: this.tokenToEstreeLocation(name),
};
}
Expand Down

0 comments on commit a412b21

Please sign in to comment.