AST works for variable reassignment
kinda coded horribly tho lol
This commit is contained in:
@@ -4,7 +4,7 @@ from typing import Callable
|
||||
from enum import Enum, auto
|
||||
|
||||
from AST import Statement, Expression, Program
|
||||
from AST import ExpressionStatement, AssignmentStatement, FunctionStatement, ReturnStatement, BlockStatement
|
||||
from AST import ExpressionStatement, AssignmentStatement, FunctionStatement, ReturnStatement, BlockStatement, ReassignStatement
|
||||
from AST import InfixExpression
|
||||
from AST import IntegerLiteral, FloatLiteral, IdentifierLiteral
|
||||
|
||||
@@ -136,39 +136,52 @@ class Parser:
|
||||
|
||||
if self.__peek_token_is(TokenType.EQ): # function definition
|
||||
# x = Func(): Int { return 10; }
|
||||
|
||||
self.__next_token()
|
||||
|
||||
func_stmt: FunctionStatement = FunctionStatement(name=stmt.name)
|
||||
if self.__peek_token_is(TokenType.TYPE):
|
||||
func_stmt: FunctionStatement = FunctionStatement(name=stmt.name)
|
||||
|
||||
if self.peek_token.literal != "Func":
|
||||
self.errors.append(f"Expected next token to be \"Func\", got {self.current_token.literal} instead.")
|
||||
return None
|
||||
|
||||
self.__next_token()
|
||||
|
||||
if not self.__expect_peek(TokenType.LPAREN):
|
||||
return None
|
||||
|
||||
func_stmt.parameters = []
|
||||
|
||||
if not self.__expect_peek(TokenType.TYPE): # Func word
|
||||
return None
|
||||
|
||||
if self.current_token.literal != "Func":
|
||||
self.errors.append(f"Expected next token to be \"Func\", got {self.current_token.literal} instead.")
|
||||
return None
|
||||
|
||||
if not self.__expect_peek(TokenType.LPAREN):
|
||||
return None
|
||||
|
||||
func_stmt.parameters = []
|
||||
if not self.__expect_peek(TokenType.RPAREN):
|
||||
return None
|
||||
|
||||
if not self.__expect_peek(TokenType.COLON):
|
||||
return None
|
||||
|
||||
if not self.__expect_peek(TokenType.TYPE):
|
||||
return None
|
||||
|
||||
func_stmt.return_type = self.current_token.literal
|
||||
|
||||
if not self.__expect_peek(TokenType.RPAREN):
|
||||
return None
|
||||
|
||||
if not self.__expect_peek(TokenType.COLON):
|
||||
return None
|
||||
|
||||
if not self.__expect_peek(TokenType.TYPE):
|
||||
return None
|
||||
|
||||
func_stmt.return_type = self.current_token.literal
|
||||
if not self.__expect_peek(TokenType.LBRACE):
|
||||
return None
|
||||
|
||||
func_stmt.body = self.__parse_block_statement()
|
||||
|
||||
if not self.__expect_peek(TokenType.LBRACE):
|
||||
return None
|
||||
|
||||
func_stmt.body = self.__parse_block_statement()
|
||||
return func_stmt
|
||||
else: # reassignment statement
|
||||
assign_stmt: ReassignStatement = ReassignStatement()
|
||||
|
||||
return func_stmt
|
||||
self.__next_token()
|
||||
|
||||
assign_stmt.ident = stmt.name
|
||||
assign_stmt.right_value = self.__parse_expression(PrecedenceType.P_LOWEST)
|
||||
|
||||
while not self.__current_token_is(TokenType.SEMICOLON) and not self.__current_token_is(TokenType.EOF):
|
||||
self.__next_token()
|
||||
|
||||
return assign_stmt
|
||||
|
||||
else:
|
||||
if not self.__expect_peek(TokenType.COLON):
|
||||
|
||||
Reference in New Issue
Block a user