def parse_functino_tail(self, first_signature, subject): first_body = self.parse_method_tail(False) first_method = ast.Method(first_signature, first_body) methods = [first_method] while self.at_word('on'): self.expect_word('on') next_signature = self.parse_functino_signature(subject, False) next_body = self.parse_method_tail(False) next_method = ast.Method(next_signature, next_body) methods.append(next_method) return methods
def parse_toplevel_declaration(self, annots): self.expect_word('def') subject = None is_prefix = False name = None allow_extra = False if self.at_type(Token.IDENTIFIER): # def <ident> ident = self.current().value name = self.expect_type(Token.IDENTIFIER) if self.at_punctuation(':='): # def <ident> := self.expect_punctuation(':=') value = self.parse_expression(True) return ast.NamespaceDeclaration(annots, name, value) else: if self.at_punctuation('('): # def <ident> ( subject = ast.Parameter(name, [data._SUBJECT], ast.Guard.eq(ast.Variable(name.shift_back()))) is_async = ast.Parameter(None, [data._TRANSPORT], ast.Guard.eq(ast.Literal(data._SYNC))) (params, operation, allow_extra, reified) = self.parse_parameters(Parser._SAUSAGES) body = self.parse_method_tail(True) selector = self.name_as_selector(operation) signature = ast.Signature([subject, selector, is_async] + params, allow_extra, reified) return ast.FunctionDeclaration(name, ast.Method(signature, body)) else: # def <ident> ... subject = ast.Parameter(name, [data._SUBJECT], ast.Guard.any()) elif self.at_word('type'): # def type <atomic> is <atomic> self.expect_word('type') subtype = self.parse_atomic_expression() self.expect_word('is') supertype = self.parse_atomic_expression() self.expect_statement_delimiter(True) return ast.IsDeclaration(subtype, supertype) elif self.at_type(Token.OPERATION): # def <operation> ... (name, is_async) = self.expect_type(Token.OPERATION) subject = self.parse_subject() is_prefix = True else: # def (<parameter>) subject = self.parse_subject() signature = self.parse_functino_signature([subject], is_prefix, name) body = self.parse_method_tail(True) stage = self.get_subject_stage(subject) return ast.MethodDeclaration(stage + 1, annots, ast.Method(signature, body))
def p_feature_method_no_formal_paramters(self, parse): ''' feature : ID LPAREN RPAREN COLON TYPE LBRACE expression RBRACE ''' parse[0] = AST.Method(name=parse[1], formal_parameters=tuple(), return_type=parse[5], body=parse[7])
def p_feature_method(self, parse): ''' feature : ID LPAREN formal_parameters_list RPAREN COLON TYPE LBRACE expression RBRACE ''' parse[0] = AST.Method(name=parse[1], formal_parameters=parse[3], return_type=parse[6], body=parse[8])
def p_method_decl_header_nonvoid(p): 'method_header : mod type ID' global current_context, current_vartable current_context = 'method' (v, s) = current_modifiers m = ast.Method(p[3], current_class, v, s, current_type) current_class.add_method(m) current_vartable = m.vars p[0] = m
def p_method_decl_header_void(p): 'method_header : mod VOID ID' global current_context, current_vartable current_context = 'method' (v, s) = current_modifiers m = ast.Method(p[3], current_class, v, s, ast.Type('void')) current_class.add_method(m) current_vartable = m.vars p[0] = m
def parse_for_expression(self, expect_delim): self.expect_word('for') sig = self.parse_signature() self.expect_word('in') elms = self.parse_expression(False) self.expect_word('do') body = self.parse_expression(expect_delim) thunk = ast.Lambda([ast.Method(sig, body)]) return ast.Invocation([ ast.Argument(data._SUBJECT, ast.Variable(data.Identifier(-1, data.Path(["core", "for"])))), ast.Argument(data._SELECTOR, ast.Literal(Parser._SAUSAGES)), ast.Argument(data._TRANSPORT, ast.Literal(data._SYNC)), ast.Argument(0, elms), ast.Argument(1, thunk), ])
def parse_when_expression(self, expect_delim): self.expect_word('when') self.expect_word('def') sig = self.parse_signature() self.expect_punctuation(':=') value = self.parse_expression(False) self.expect_word('do') body = self.parse_expression(expect_delim) thunk = ast.Lambda([ast.Method(sig, body)]) return ast.Invocation([ ast.Argument(data._SUBJECT, ast.Variable(data.Identifier(-1, data.Path(["core", "when_def"])))), ast.Argument(data._SELECTOR, ast.Literal(Parser._SAUSAGES)), ast.Argument(data._TRANSPORT, ast.Literal(data._SYNC)), ast.Argument(0, value), ast.Argument(1, thunk), ])