Advertisement
tinyevil

Untitled

Jun 29th, 2018
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 11.24 KB | None | 0 0
  1. from jf.parser.lexer import TokenType
  2. from jf.parser.parser_base import *
  3. from jf.ast.toplevel import *
  4. from jf.ast.common import *
  5. from jf.ast.expr import *
  6. from jf.ast.stmt import *
  7. from jf.utils import *
  8. import functools
  9. from enum import IntEnum
  10.  
  11.  
  12. class PrecLevel(IntEnum):
  13.     ALL = 0
  14.  
  15.     LogicOr = 5
  16.     LogicAnd = 10
  17.     Comparison = 20
  18.     Multiplication = 30
  19.     Addition = 40
  20.  
  21.     Prefix = 60
  22.  
  23.     Indexing = 80
  24.  
  25.     Call = 90
  26.  
  27.  
  28. @mparser
  29. def block(parser):
  30.     yield exact(TokenType.Op_LBrace)
  31.     result = yield parser
  32.     yield exact(TokenType.Op_RBrace)
  33.     return result
  34.  
  35.  
  36. def block_or_semicolon(contents):
  37.     return choice(
  38.         exact(TokenType.Op_Semicolon).bind0(value(None)),
  39.         block(contents)
  40.     )
  41.  
  42.  
  43. def top_level_def():
  44.     return choice(
  45.         import_decl(),
  46.         namespace(),
  47.         struct(),
  48.         alias(),
  49.         function()
  50.     )
  51.  
  52.  
  53. def struct_def():
  54.     return choice(
  55.         alias(),
  56.         field(),
  57.         function(),
  58.         case()
  59.     )
  60.  
  61.  
  62. def struct_defs():
  63.     return many(struct_def())
  64.  
  65.  
  66. def top_level_defs():
  67.     return many(top_level_def())
  68.  
  69.  
  70. @mparser
  71. def namespace():
  72.     loc = yield get_loc()
  73.     yield exact(TokenType.K_Namespace)
  74.     name = yield exact(TokenType.Identifier)
  75.     contents = yield block_or_semicolon(top_level_defs())
  76.     return Namespace(loc, name, contents)
  77.  
  78.  
  79. @functools.lru_cache(maxsize=None)
  80. def base_expression(level):
  81.     prefix_ops = []
  82.     for token, op in [
  83.         (TokenType.K_Ref, UnOp.RefRef),
  84.         (TokenType.K_Ptr, UnOp.RefPtr),
  85.         (TokenType.K_RefIn, UnOp.RefIn),
  86.         (TokenType.K_RefOut, UnOp.RefOut),
  87.         (TokenType.Op_Star, UnOp.Deref),
  88.         (TokenType.K_Not, UnOp.LogicNot),
  89.         (TokenType.Op_Minus, UnOp.Negate),
  90.     ]:
  91.         (lambda op:
  92.             prefix_ops.append(
  93.                 exact(token)
  94.                 .bind0(expression(max(level, PrecLevel.Prefix)))
  95.                 .bind(lambda ex: value(ExprUnOp(ex, op)))
  96.             )
  97.         )(op)
  98.  
  99.     decode_ref_type = choice(
  100.         exact(TokenType.K_Ref).bind0(value(UnOp.AddressRef)),
  101.         exact(TokenType.K_Ptr).bind0(value(UnOp.AddressPtr)),
  102.         exact(TokenType.K_RefIn).bind0(value(UnOp.AddressRefIn)),
  103.         exact(TokenType.K_RefOut).bind0(value(UnOp.AddressRefOut)),
  104.         value(UnOp.AddressRef)
  105.     )
  106.  
  107.     prefix_ops.append(
  108.         exact(TokenType.Op_Amp)
  109.         .bind0(decode_ref_type)
  110.         .bind(lambda op:
  111.             expression(max(level, PrecLevel.Prefix))
  112.             .bind(lambda ex: value(ExprUnOp(ex, op)))
  113.         )
  114.     )
  115.  
  116.     prefix_ops.append(
  117.         between(
  118.             exact(TokenType.Op_LBracket),
  119.             exact(TokenType.Op_RBracket),
  120.             optional(expression())
  121.         )
  122.         .bind(lambda size_ex:
  123.             expression(max(level, PrecLevel.Prefix))
  124.             .bind(lambda ex: value(ExprArray(ex, size_ex)))
  125.         )
  126.     )
  127.  
  128.     return choice(
  129.         between(exact(TokenType.Op_LParen), exact(TokenType.Op_RParen), expression()),
  130.         exact(TokenType.Identifier).bind(lambda name: value(ExprID(name))),
  131.         exact(TokenType.FloatLiteral).bind(lambda content: value(ExprNum(content))),
  132.         exact(TokenType.IntLiteral).bind(lambda content: value(ExprNum(content))),
  133.         exact(TokenType.StringLiteral).bind(lambda content: value(ExprString(content))),
  134.         *prefix_ops
  135.     )
  136.  
  137.  
  138. @functools.lru_cache(maxsize=None)
  139. def expression_rhs(level):
  140.     parsers_list = []
  141.  
  142.     if level <= PrecLevel.Indexing:
  143.         parsers_list.append(
  144.             exact(TokenType.Op_Dot)
  145.             .bind0(exact(TokenType.Identifier))
  146.             .bind(lambda id: value(lambda lhs: ExprField(lhs, id)))
  147.         )
  148.     if level <= PrecLevel.LogicOr:
  149.         parsers_list.append(
  150.             exact(TokenType.K_Or)
  151.             .bind0(expression(PrecLevel.LogicOr + 1))
  152.             .bind(lambda rhs: value(lambda lhs: ExprBinOp(lhs, BinOp.LogicOr, rhs)))
  153.         )
  154.     if level <= PrecLevel.LogicAnd:
  155.         parsers_list.append(
  156.             exact(TokenType.K_And)
  157.             .bind0(expression(PrecLevel.LogicAnd + 1))
  158.             .bind(lambda rhs: value(lambda lhs: ExprBinOp(lhs, BinOp.LogicAnd, rhs)))
  159.         )
  160.     if level <= PrecLevel.Comparison:
  161.         for token, op in [
  162.             (TokenType.Op_EQ, BinOp.EQ),
  163.             (TokenType.Op_NEQ, BinOp.NEQ),
  164.             (TokenType.Op_LE, BinOp.LE),
  165.             (TokenType.Op_LT, BinOp.LT),
  166.             (TokenType.Op_GE, BinOp.GE),
  167.             (TokenType.Op_GT, BinOp.GT),
  168.         ]:
  169.             (lambda op:
  170.             parsers_list.append(
  171.                 exact(token)
  172.                 .bind0(expression(PrecLevel.Comparison + 1))
  173.                 .bind(lambda rhs: value(lambda lhs: ExprBinOp(lhs, op, rhs)))
  174.             ))(op)
  175.     if level <= PrecLevel.Multiplication:
  176.         for token, op in [
  177.             (TokenType.Op_Star, BinOp.Multiply),
  178.             (TokenType.Op_Div, BinOp.Divide),
  179.         ]:
  180.             (lambda op:
  181.             parsers_list.append(
  182.                 exact(token)
  183.                 .bind0(expression(PrecLevel.Multiplication + 1))
  184.                 .bind(lambda rhs: value(lambda lhs: ExprBinOp(lhs, op, rhs)))
  185.             ))(op)
  186.     if level <= PrecLevel.Addition:
  187.         for token, op in [
  188.             (TokenType.Op_Plus, BinOp.Add),
  189.             (TokenType.Op_Minus, BinOp.Subtract),
  190.         ]:
  191.             (lambda op:
  192.             parsers_list.append(
  193.                 exact(token)
  194.                 .bind0(expression(PrecLevel.Addition + 1))
  195.                 .bind(lambda rhs: value(lambda lhs: ExprBinOp(lhs, op, rhs)))
  196.             ))(op)
  197.  
  198.     if level <= PrecLevel.Call:
  199.         parsers_list.append(
  200.             between(
  201.                 exact(TokenType.Op_LParen),
  202.                 exact(TokenType.Op_RParen),
  203.                 sep_by(exact(TokenType.Op_Comma), expression())
  204.             )
  205.             .bind(lambda args: value(lambda lhs: ExprCall(lhs, args)))
  206.         )
  207.         parsers_list.append(
  208.             between(
  209.                 exact(TokenType.Op_LBracket),
  210.                 exact(TokenType.Op_RBracket),
  211.                 sep_by(exact(TokenType.Op_Comma), expression())
  212.             )
  213.             .bind(lambda args: value(lambda lhs: ExprSubscript(lhs, args)))
  214.         )
  215.  
  216.     def fn(expr):
  217.         return choice(
  218.             choice(
  219.                 *parsers_list
  220.             ).bind(lambda f: fn(f(expr))),
  221.             value(expr)
  222.         )
  223.     return fn
  224.  
  225.  
  226. @mparser
  227. def expression(level=PrecLevel.ALL):
  228.     prim = yield base_expression(level)
  229.  
  230.     return expression_rhs(level)(prim)
  231.  
  232.  
  233. @mparser
  234. def type_binder():
  235.     yield exact(TokenType.Op_Colon)
  236.     type = yield expression()
  237.     return type
  238.  
  239.  
  240. def maybe_type_binder():
  241.     return optional(type_binder())
  242.  
  243.  
  244. @mparser
  245. def param_group():
  246.     names = yield many1(exact(TokenType.Identifier))
  247.     type = yield maybe_type_binder()
  248.     return ParamGroup(names, type)
  249.  
  250.  
  251. @mparser
  252. def param_list():
  253.     groups = yield sep_by(
  254.         exact(TokenType.Op_Comma),
  255.         param_group()
  256.     )
  257.     return ParamList(groups)
  258.  
  259.  
  260. @mparser
  261. def generic_params():
  262.     yield exact(TokenType.Op_LBracket)
  263.     params = yield param_list()
  264.     yield exact(TokenType.Op_RBracket)
  265.     return params
  266.  
  267.  
  268. def maybe_generic_params():
  269.     return optional(generic_params())
  270.  
  271.  
  272. @mparser
  273. def function_params():
  274.     yield exact(TokenType.Op_LParen)
  275.     params = yield param_list()
  276.     yield exact(TokenType.Op_RParen)
  277.     return params
  278.  
  279.  
  280. @mparser
  281. def constraint():
  282.     return (yield expression())
  283.  
  284.  
  285. @mparser
  286. def where_clause():
  287.     yield exact(TokenType.K_Where)
  288.     constraints = sep_by(exact(TokenType.Op_Comma), constraint())
  289.     return constraints
  290.  
  291.  
  292. def maybe_where_clause():
  293.     return optional(where_clause())
  294.  
  295.  
  296. @mparser
  297. def return_statement():
  298.     loc = yield get_loc()
  299.     yield exact(TokenType.K_Return)
  300.     expr = yield expression()
  301.     yield exact(TokenType.Op_Semicolon)
  302.     return StmtReturn(loc, expr)
  303.  
  304.  
  305. @mparser
  306. def expression_statement():
  307.     loc = yield get_loc()
  308.     expr = yield expression()
  309.     result, assign = yield optional_with_result(assign_statement(loc, expr))
  310.     if result:
  311.         return assign
  312.     yield exact(TokenType.Op_Semicolon)
  313.     return StmtExpr(loc, expr)
  314.  
  315.  
  316. @mparser
  317. def assign_statement(loc, lhs):
  318.     yield exact(TokenType.Op_Assign)
  319.     rhs = yield expression()
  320.     yield exact(TokenType.Op_Semicolon)
  321.     return StmtAssign(loc, lhs, rhs)
  322.  
  323.  
  324. @mparser
  325. def while_statement():
  326.     loc = yield get_loc()
  327.     yield exact(TokenType.K_While)
  328.     cond = yield expression()
  329.     stmts = yield block(statements())
  330.     return StmtWhile(loc, cond, stmts)
  331.  
  332.  
  333. @mparser
  334. def if_branch():
  335.     loc = yield get_loc()
  336.     yield exact(TokenType.K_If)
  337.     cond = yield expression()
  338.     stmts = yield block(statements())
  339.     return IfBranch(loc, cond, stmts)
  340.  
  341.  
  342. @mparser
  343. def if_statement():
  344.     loc = yield get_loc()
  345.     branches = [(yield if_branch())]
  346.     else_block = None
  347.     while True:
  348.         if (yield optional(exact(TokenType.K_Else))):
  349.             branch = yield optional(if_branch())
  350.             if branch:
  351.                 branches.append(branch)
  352.                 continue
  353.             else:
  354.                 else_block = yield block(statements())
  355.                 break
  356.         else:
  357.             break
  358.     return StmtIf(loc, branches, else_block)
  359.  
  360.  
  361. def maybe_initializer():
  362.     return optional(exact(TokenType.Op_Assign).bind0(expression()))
  363.  
  364.  
  365. @mparser
  366. def var_statement():
  367.     loc = yield get_loc()
  368.     yield exact(TokenType.K_Var)
  369.     names = yield many1(exact(TokenType.Identifier))
  370.     type = yield maybe_type_binder()
  371.     initializer = yield maybe_initializer()
  372.     yield exact(TokenType.Op_Semicolon)
  373.     return StmtVar(loc, names, type, initializer)
  374.  
  375.  
  376. def statement():
  377.     return choice(
  378.         return_statement(),
  379.         while_statement(),
  380.         var_statement(),
  381.         if_statement(),
  382.         expression_statement()
  383.     ).named("statement")
  384.  
  385.  
  386. def statements():
  387.     return many(statement())
  388.  
  389.  
  390. def qual_id():
  391.     return sep_by1(exact(TokenType.Op_Dot), exact(TokenType.Identifier))
  392.  
  393.  
  394. def maybe_qual_id():
  395.     return optional(qual_id())
  396.  
  397.  
  398. @mparser
  399. def function_body():
  400.     stmts = yield block(statements())
  401.     return FunctionBody(stmts)
  402.  
  403.  
  404. @mparser
  405. def function_ffi():
  406.     loc = yield get_loc()
  407.     yield exact(TokenType.Op_Assign)
  408.     yield exact(TokenType.K_Import)
  409.     string = yield exact(TokenType.StringLiteral)
  410.     yield exact(TokenType.Op_Semicolon)
  411.     return FunctionFFI(loc, string)
  412.  
  413.  
  414. def function_def():
  415.     return choice(
  416.         exact(TokenType.Op_Semicolon).bind0(value(None)),
  417.         function_body(),
  418.         function_ffi()
  419.     )
  420.  
  421.  
  422. @mparser
  423. def field():
  424.     loc = yield get_loc()
  425.     yield exact(TokenType.K_Field)
  426.     names = yield many1(exact(TokenType.Identifier))
  427.     type = yield type_binder()
  428.     yield exact(TokenType.Op_Semicolon)
  429.     return StructField(loc, names, type)
  430.  
  431.  
  432. @mparser
  433. def case():
  434.     loc = yield get_loc()
  435.     yield exact(TokenType.K_Case)
  436.     name = yield exact(TokenType.Identifier)
  437.     params = yield optional(function_params())
  438.     type = yield maybe_type_binder()
  439.     defs = yield block_or_semicolon(struct_defs())
  440.     return StructCase(loc, name, params, type, defs)
  441.  
  442.  
  443. @mparser
  444. def struct():
  445.     loc = yield get_loc()
  446.     yield exact(TokenType.K_Struct)
  447.     name = yield exact(TokenType.Identifier)
  448.     params = yield maybe_generic_params()
  449.     kind = yield maybe_type_binder()
  450.     where = yield maybe_where_clause()
  451.     defs = yield block_or_semicolon(struct_defs())
  452.     return Struct(loc, name, params, kind, where, defs)
  453.  
  454.  
  455. @mparser
  456. def alias():
  457.     loc = yield get_loc()
  458.     yield exact(TokenType.K_Alias)
  459.     name = yield exact(TokenType.Identifier)
  460.     params = yield maybe_generic_params()
  461.     yield exact(TokenType.Op_Assign)
  462.     expr = yield expression()
  463.     yield exact(TokenType.Op_Semicolon)
  464.     return Alias(loc, name, params, expr)
  465.  
  466.  
  467. @mparser
  468. def function():
  469.     loc = yield get_loc()
  470.     kind = yield choice(exact(TokenType.K_Function), exact(TokenType.K_Method))
  471.     name = yield exact(TokenType.Identifier)
  472.     gen_params = yield maybe_generic_params()
  473.     params = yield function_params()
  474.     ret_ty = yield maybe_type_binder()
  475.     where = yield maybe_where_clause()
  476.     definition = yield function_def()
  477.     return Function(loc, kind, name, gen_params, params, ret_ty, where, definition)
  478.  
  479.  
  480. @mparser
  481. def import_decl():
  482.     loc = yield get_loc()
  483.     yield exact(TokenType.K_Import)
  484.     qid = yield maybe_qual_id()
  485.     from_qid = yield optional(exact(TokenType.K_From).bind0(qual_id()))
  486.     into_id = yield optional(exact(TokenType.K_Into).bind0(exact(TokenType.Identifier)))
  487.     yield exact(TokenType.Op_Semicolon)
  488.     return Import(loc, qid, from_qid, into_id)
  489.  
  490.  
  491. @mparser
  492. def file():
  493.     defs = yield top_level_defs()
  494.     yield exact(TokenType.EOF)
  495.     return File(defs)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement