Commit 7e04915c authored by Tom Niget's avatar Tom Niget

Move constants

parent ac78d6fd
...@@ -6,4 +6,4 @@ def gàé(): ...@@ -6,4 +6,4 @@ def gàé():
if __name__ == "__main__": if __name__ == "__main__":
if True: if True:
a, b = gàé() # abc a, b, c = gàé() # abc
\ No newline at end of file \ No newline at end of file
...@@ -15,7 +15,7 @@ from transpiler.phases.desugar_op import DesugarOp ...@@ -15,7 +15,7 @@ from transpiler.phases.desugar_op import DesugarOp
colorama.init() colorama.init()
from transpiler.consts import MAPPINGS from transpiler.phases.emit_cpp.consts import MAPPINGS
from transpiler.exceptions import CompileError from transpiler.exceptions import CompileError
from transpiler.phases.desugar_with import DesugarWith from transpiler.phases.desugar_with import DesugarWith
from transpiler.phases.emit_cpp.file import FileVisitor from transpiler.phases.emit_cpp.file import FileVisitor
......
# coding: utf-8 # coding: utf-8
import ast import ast
SYMBOLS = { # SYMBOLS = {
ast.Eq: "==", # ast.Eq: "==",
ast.NotEq: '!=', # ast.NotEq: '!=',
ast.Pass: '/* pass */', # ast.Pass: '/* pass */',
ast.Mult: '*', # ast.Mult: '*',
ast.Add: '+', # ast.Add: '+',
ast.Sub: '-', # ast.Sub: '-',
ast.Div: '/', # ast.Div: '/',
ast.FloorDiv: '/', # TODO # ast.FloorDiv: '/', # TODO
ast.Mod: '%', # ast.Mod: '%',
ast.Lt: '<', # ast.Lt: '<',
ast.Gt: '>', # ast.Gt: '>',
ast.GtE: '>=', # ast.GtE: '>=',
ast.LtE: '<=', # ast.LtE: '<=',
ast.LShift: '<<', # ast.LShift: '<<',
ast.RShift: '>>', # ast.RShift: '>>',
ast.BitXor: '^', # ast.BitXor: '^',
ast.BitOr: '|', # ast.BitOr: '|',
ast.BitAnd: '&', # ast.BitAnd: '&',
ast.Not: '!', # ast.Not: '!',
ast.IsNot: '!=', # ast.IsNot: '!=',
ast.USub: '-', # ast.USub: '-',
ast.And: '&&', # ast.And: '&&',
ast.Or: '||' # ast.Or: '||'
} # }
"""Mapping of Python AST nodes to C++ symbols.""" # """Mapping of Python AST nodes to C++ symbols."""
#
DUNDER_SYMBOLS = { # DUNDER_SYMBOLS = {
"__eq__": "==", # "__eq__": "==",
"__ne__": "!=", # "__ne__": "!=",
"__lt__": "<", # "__lt__": "<",
"__gt__": ">", # "__gt__": ">",
"__ge__": ">=", # "__ge__": ">=",
"__le__": "<=", # "__le__": "<=",
"__add__": "+", # "__add__": "+",
"__sub__": "-", # "__sub__": "-",
"__mul__": "*", # "__mul__": "*",
"__div__": "/", # "__div__": "/",
"__mod__": "%", # "__mod__": "%",
"__lshift__": "<<", # "__lshift__": "<<",
"__rshift__": ">>", # "__rshift__": ">>",
"__xor__": "^", # "__xor__": "^",
"__or__": "|", # "__or__": "|",
"__and__": "&", # "__and__": "&",
"__invert__": "~", # "__invert__": "~",
"__neg__": "-", # "__neg__": "-",
"__pos__": "+", # "__pos__": "+",
} # }
#
PRECEDENCE = [ # PRECEDENCE = [
("()", "[]", ".",), # ("()", "[]", ".",),
("unary", "co_await"), # ("unary", "co_await"),
("*", "/", "%",), # ("*", "/", "%",),
("+", "-"), # ("+", "-"),
("<<", ">>"), # ("<<", ">>"),
("<", "<=", ">", ">="), # ("<", "<=", ">", ">="),
("==", "!="), # ("==", "!="),
("&",), # ("&",),
("^",), # ("^",),
("|",), # ("|",),
("&&",), # ("&&",),
("||",), # ("||",),
("?:", "co_yield"), # ("?:", "co_yield"),
(",",) # (",",)
] # ]
"""Precedence of C++ operators.""" # """Precedence of C++ operators."""
#
PRECEDENCE_LEVELS = {op: i for i, ops in enumerate(PRECEDENCE) for op in ops} # PRECEDENCE_LEVELS = {op: i for i, ops in enumerate(PRECEDENCE) for op in ops}
"""Mapping of C++ operators to their precedence level.""" # """Mapping of C++ operators to their precedence level."""
#
MAPPINGS = { # MAPPINGS = {
"True": "true", # "True": "true",
"False": "false", # "False": "false",
"None": "nullptr" # "None": "nullptr"
} # }
"""Mapping of Python builtin constants to C++ equivalents.""" # """Mapping of Python builtin constants to C++ equivalents."""
...@@ -34,22 +34,4 @@ class DesugarCompare(ast.NodeTransformer): ...@@ -34,22 +34,4 @@ class DesugarCompare(ast.NodeTransformer):
res.values.append(call) res.values.append(call)
if len(res.values) == 1: if len(res.values) == 1:
return res.values[0] return res.values[0]
return res return res
\ No newline at end of file
# def visit_Compare(self, node: ast.Compare):
# res = ast.BoolOp(ast.And(), [], **linenodata(node))
# operands = list(map(self.visit, [node.left, *node.comparators]))
# for left, op, right in zip(operands, node.ops, operands[1:]):
# lnd = make_lnd(left, right)
# call = ast.Compare(
# left,
# [op],
# [right],
# **lnd
# )
# if type(op) == ast.NotIn:
# call = ast.UnaryOp(ast.Not(), call, **lnd)
# res.values.append(call)
# if len(res.values) == 1:
# return res.values[0]
# return res
...@@ -28,6 +28,28 @@ SYMBOLS = { ...@@ -28,6 +28,28 @@ SYMBOLS = {
} }
"""Mapping of Python AST nodes to C++ symbols.""" """Mapping of Python AST nodes to C++ symbols."""
DUNDER_SYMBOLS = {
"__eq__": "==",
"__ne__": "!=",
"__lt__": "<",
"__gt__": ">",
"__ge__": ">=",
"__le__": "<=",
"__add__": "+",
"__sub__": "-",
"__mul__": "*",
"__div__": "/",
"__mod__": "%",
"__lshift__": "<<",
"__rshift__": ">>",
"__xor__": "^",
"__or__": "|",
"__and__": "&",
"__invert__": "~",
"__neg__": "-",
"__pos__": "+",
}
PRECEDENCE = [ PRECEDENCE = [
("()", "[]", ".",), ("()", "[]", ".",),
("unary", "co_await"), ("unary", "co_await"),
......
...@@ -6,7 +6,7 @@ from typing import List, Iterable ...@@ -6,7 +6,7 @@ from typing import List, Iterable
from transpiler.phases.typing.types import UserType, FunctionType, Promise from transpiler.phases.typing.types import UserType, FunctionType, Promise
from transpiler.phases.utils import make_lnd from transpiler.phases.utils import make_lnd
from transpiler.utils import compare_ast, linenodata from transpiler.utils import compare_ast, linenodata
from transpiler.consts import SYMBOLS, PRECEDENCE_LEVELS, DUNDER_SYMBOLS from transpiler.phases.emit_cpp.consts import SYMBOLS, PRECEDENCE_LEVELS, DUNDER_SYMBOLS
from transpiler.phases.emit_cpp import CoroutineMode, join, NodeVisitor from transpiler.phases.emit_cpp import CoroutineMode, join, NodeVisitor
from transpiler.phases.typing.scope import Scope, VarKind from transpiler.phases.typing.scope import Scope, VarKind
......
...@@ -3,7 +3,7 @@ import ast ...@@ -3,7 +3,7 @@ import ast
from dataclasses import dataclass from dataclasses import dataclass
from typing import Iterable from typing import Iterable
from transpiler.consts import SYMBOLS from transpiler.phases.emit_cpp.consts import SYMBOLS
from transpiler.phases.emit_cpp import CoroutineMode from transpiler.phases.emit_cpp import CoroutineMode
from transpiler.phases.emit_cpp.block import BlockVisitor from transpiler.phases.emit_cpp.block import BlockVisitor
from transpiler.phases.typing.scope import Scope from transpiler.phases.typing.scope import Scope
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment