def p_init(self, p): """init : const_expressions COMMA const_expression SEMICOLON | const_expression SEMICOLON""" if len(p) == 3: p[0] = TreeNode('const_expression', children=[p[1]]) else: p[0] = TreeNode('const_expressions', children=[p[1], p[3]])
def p_until_error(self, p): """until : UNTIL error NEWLINE statements ENDU | UNTIL bool_expression NEWLINE statements error | UNTIL bool_expression statements ENDU | UNTIL error NEWLINE func_body_statements ENDU | UNTIL bool_expression NEWLINE func_body_statements error | UNTIL bool_expression func_body_statements ENDU | until error""" if len(p) == 6: p[0] = TreeNode('until_error', value='Until error', children={'body': p[4]}, lineno=p.lineno(1), lexpos=p.lexpos(1)) sys.stderr.write(f'==> Error in \'until\'!\n') elif len(p) == 5: p[0] = TreeNode('until_error', value='Until error', children={'body': p[3]}, lineno=p.lineno(1), lexpos=p.lexpos(1)) try: sys.stderr.write(f'Error at {p.lineno(1)} line\n') except: sys.stderr.write(f'Error\n') sys.stderr.write(f'==> Condition and body are on the same line!\n') self.ok = False else: p[0] = TreeNode('until_error', value='While error', lineno=p.lineno(1), lexpos=p.lexpos(1)) sys.stderr.write(f'==> Error in \'until\'!\n')
def p_variant(self, p): """variant : NAME | NAME varsize | PARAM | PARAM varsize""" if len(p) == 2 and p[1] == 'PARAM': p[0] = TreeNode('func_param', value=p[1], lineno=p.lineno(1), lexpos=p.lexpos(1)) elif p[1] == 'PARAM': p[0] = TreeNode('func_param', value=p[1], children=p[2], lineno=p.lineno(1), lexpos=p.lexpos(1)) elif len(p) == 2: p[0] = TreeNode('variant', value=p[1], lineno=p.lineno(1), lexpos=p.lexpos(1)) else: p[0] = TreeNode('variant', value=p[1], children=p[2], lineno=p.lineno(1), lexpos=p.lexpos(1))
def p_init_list(self, p): """init_list : LBRACE inits RBRACE | LBRACE RBRACE""" if len(p) == 4: p[0] = TreeNode('init_list', children=p[2]) else: p[0] = TreeNode('empty_init_list', lineno=p.lineno(1), lexpos=p.lexpos(1))
def p_statements(self, p): """statements : statements statement | statement""" if len(p) == 2: p[0] = TreeNode('statement', children=p[1]) else: p[0] = TreeNode( 'statements', children=[p[1], TreeNode('statement', children=p[2])])
def p_function(self, p): """function : FUNC NAME NEWLINE func_body_statements ENDFUNC""" self._functions[p[2]] = TreeNode('function', children={'body': p[4]}, lineno=p.lineno(1), lexpos=p.lexpos(1)) p[0] = TreeNode('func_descriptor', value=p[2], lineno=p.lineno(1), lexpos=p.lexpos(2))
def p_func_body_statements(self, p): """func_body_statements : func_body_statements func_body_statement | func_body_statement""" if len(p) == 2: p[0] = TreeNode('func_body_statement', children=p[1]) else: p[0] = TreeNode('func_body_statements', children=[ p[1], TreeNode('func_body_statement', children=p[2]) ])
def p_function_call(self, p): """function_call : CALL NAME expression | CALL NAME""" if len(p) == 4: p[0] = TreeNode('function_call', value={'name': p[2]}, children=p[3], lineno=p.lineno(1), lexpos=p.lexpos(1)) else: p[0] = TreeNode('function_call', value={'name': p[2]}, lineno=p.lineno(1), lexpos=p.lexpos(1))
def p_string_math_expression(self, p): """string_math_expression : string_expression PLUS string_expression | MINUS string_expression""" if len(p) == 3: p[0] = TreeNode('unar_op', value=p[1], children=p[2], lineno=p.lineno(1), lexpos=p.lexpos(1)) else: p[0] = TreeNode('bin_op', value=p[2], children=[p[1], p[3]], lineno=p.lineno(2), lexpos=p.lexpos(2))
def p_assignment(self, p): """assignment : variant ASSIGNMENT expression""" p[0] = TreeNode('assignment', value=p[1], children=p[3], lineno=p.lineno(2), lexpos=p.lexpos(2))
def p_const_expression(self, p): """const_expression : const_math_expression | const""" p[0] = TreeNode('const_expression', children=p[1], lineno=p.lineno(1), lexpos=p.lexpos(1))
def p_const_expressions(self, p): """const_expressions : const_expressions COMMA const_expression | const_expression""" if len(p) == 2: p[0] = p[1] else: p[0] = TreeNode('const_expressions', children=[p[1], p[3]])
def p_inits(self, p): """inits : inits init | init""" if len(p) == 2: p[0] = p[1] else: p[0] = TreeNode('inits', children=[p[1], p[2]])
def p_convert(self, p): """convert : CONVERT type TO type variant""" p[0] = TreeNode('convert', value=[p[2], p[4]], children=p[5], lineno=p.lineno(1), lexpos=p.lexpos(1))
def p_digitize(self, p): """digitize : DIGITIZE type variant""" p[0] = TreeNode('digitize', value=p[2], children=p[3], lineno=p.lineno(1), lexpos=p.lexpos(1))
def p_decl_error(self, p): """declaration : VARIANT error | VARIANT variant ASSIGNMENT error | declaration error""" if len(p) == 3: p[0] = TreeNode('error', value='Declaration error', lineno=p.lineno(2), lexpos=p.lexpos(2)) sys.stderr.write(f'==> Error in variant declaration\n') else: p[0] = TreeNode('error', value='Initialization error', lineno=p.lineno(2), lexpos=p.lexpos(2)) sys.stderr.write(f'==> Error in variant initialization\n')
def p_return_error(self, p): """return : RETURN error | return error""" p[0] = TreeNode('return_error', value='Return error', lineno=p.lineno(1), lexpos=p.lexpos(1)) sys.stderr.write(f'==> Error in return!\n')
def p_command_error(self, p): """command : COMMAND error | command error""" p[0] = TreeNode('error', value='Command error', lineno=p.lineno(1), lexpos=p.lexpos(1)) sys.stderr.write(f'==> Error in a command!\n')
def p_digitize_error(self, p): """digitize : DIGITIZE error | digitize error""" p[0] = TreeNode('error', value='Digitize error', lineno=p.lineno(1), lexpos=p.lexpos(1)) sys.stderr.write(f'==> Error in digitize!\n')
def p_varsize(self, p): """varsize : LSQBRACKET decimal_expression RSQBRACKET | LSQBRACKET decimal_expression COMMA decimal_expression RSQBRACKET | LSQBRACKET RSQBRACKET""" if len(p) == 4: p[0] = TreeNode('varsize', children=p[2], lineno=p.lineno(2), lexpos=p.lexpos(2)) elif len(p) == 3: p[0] = TreeNode('empty_varsize', lineno=p.lineno(1), lexpos=p.lexpos(1)) else: p[0] = TreeNode('varsize', children=[p[2], p[4]], lineno=p.lineno(2), lexpos=p.lexpos(2))
def p_assignment_error(self, p): """assignment : variant ASSIGNMENT error | error ASSIGNMENT expression""" p[0] = TreeNode('error', value='Assignment error', children=p[2], lineno=p.lineno(1), lexpos=p.lexpos(1)) sys.stderr.write(f'==> Assignment error!\n')
def p_decimal_expression(self, p): """decimal_expression : dec_math_expression | decimal_const | variant | function_call""" p[0] = TreeNode('decimal_expression', children=p[1], lineno=p.lineno(1), lexpos=p.lexpos(1))
def p_bool_expression(self, p): """bool_expression : bool_math_expression | bool_const | variant | function_call""" p[0] = TreeNode('bool_expression', children=p[1], lineno=p.lineno(1), lexpos=p.lexpos(1))
def p_string_expression(self, p): """string_expression : string_math_expression | string_const | variant | function_call""" p[0] = TreeNode('string_expression', children=p[1], lineno=p.lineno(1), lexpos=p.lexpos(1))
def p_expression(self, p): """expression : math_expression | const | variant | function_call | command""" p[0] = TreeNode('expression', children=p[1], lineno=p.lineno(1), lexpos=p.lexpos(1))
def p_until(self, p): """until : UNTIL bool_expression NEWLINE statements ENDU | UNTIL bool_expression NEWLINE func_body_statements ENDU""" p[0] = TreeNode('until', children={ 'condition': p[2], 'body': p[4] }, lineno=p.lineno(1), lexpos=p.lexpos(1))
def p_while(self, p): """while : WHILE bool_expression NEWLINE statements ENDW | WHILE bool_expression NEWLINE func_body_statements ENDW""" p[0] = TreeNode('while', children={ 'condition': p[2], 'body': p[4] }, lineno=p.lineno(1), lexpos=p.lexpos(1))
def p_convert_error(self, p): """convert : CONVERT type TO type error | CONVERT type TO error | CONVERT type error | CONVERT error | convert error""" p[0] = TreeNode('error', value='Convertation error', lineno=p.lineno(1), lexpos=p.lexpos(1)) sys.stderr.write(f'==> Error in convertation!\n')
def p_varsize_error(self, p): """varsize : LSQBRACKET error RSQBRACKET | LSQBRACKET error COMMA decimal_expression RSQBRACKET | LSQBRACKET decimal_expression COMMA error RSQBRACKET | LSQBRACKET error COMMA error RSQBRACKET | varsize error""" p[0] = TreeNode('error', value='Variant size/index error', lineno=p.lineno(1), lexpos=p.lexpos(1)) sys.stderr.write(f'==> Error in variant size/index!\n')
def p_bool_const(self, p): """bool_const : TRUE | FALSE""" if p[1] == 'TRUE': val = True else: val = False p[0] = TreeNode('bool_const', value=val, lineno=p.lineno(1), lexpos=p.lexpos(1))