started working on string literals

This commit is contained in:
SpookyDervish
2025-10-15 07:48:38 +11:00
parent 39cc0429da
commit 39a5151d97
5 changed files with 45 additions and 4 deletions

14
AST.py
View File

@@ -23,6 +23,7 @@ class NodeType(Enum):
FloatLiteral = "FloatLiteral" FloatLiteral = "FloatLiteral"
IdentifierLiteral = "IdentifierLiteral" IdentifierLiteral = "IdentifierLiteral"
BooleanLiteral = "BooleanLiteral" BooleanLiteral = "BooleanLiteral"
StringLiteral = "StringLiteral"
# Helper # Helper
FunctionParameter = "FunctionParameter" FunctionParameter = "FunctionParameter"
@@ -124,6 +125,19 @@ class BooleanLiteral(Expression):
"type": self.type().value, "type": self.type().value,
"value": self.value "value": self.value
} }
class StringLiteral(Expression):
def __init__(self, value: str = None) -> None:
self.value: str = value
def type(self) -> NodeType:
return NodeType.StringLiteral
def json(self) -> dict:
return {
"type": self.type().value,
"value": self.value
}
# endregion # endregion
# region Statements # region Statements

View File

@@ -17,9 +17,23 @@ add_entry:
ret i32 %".10" ret i32 %".10"
} }
define i32 @"sub"(i32 %".1", i32 %".2")
{
sub_entry:
%".4" = alloca i32
store i32 %".1", i32* %".4"
%".6" = alloca i32
store i32 %".2", i32* %".6"
%".8" = load i32, i32* %".4"
%".9" = load i32, i32* %".6"
%".10" = sub i32 %".8", %".9"
ret i32 %".10"
}
define i32 @"main"() define i32 @"main"()
{ {
main_entry: main_entry:
%".2" = call i32 @"add"(i32 2, i32 3) %".2" = call i32 @"add"(i32 50, i32 50)
ret i32 %".2" %".3" = call i32 @"sub"(i32 %".2", i32 125)
ret i32 %".3"
} }

View File

@@ -146,6 +146,8 @@ class Lexer:
tok = self.__new_token(TokenType.COLON, self.current_char) tok = self.__new_token(TokenType.COLON, self.current_char)
case ",": case ",":
tok = self.__new_token(TokenType.COMMA, self.current_char) tok = self.__new_token(TokenType.COMMA, self.current_char)
case '"':
tok = self.__new_token(TokenType.STRING, self.__read_string())
case None: case None:
tok = self.__new_token(TokenType.EOF, "") tok = self.__new_token(TokenType.EOF, "")
case _: case _:
@@ -162,4 +164,13 @@ class Lexer:
tok = self.__new_token(TokenType.ILLEGAL, self.current_char) tok = self.__new_token(TokenType.ILLEGAL, self.current_char)
self.__read_char() self.__read_char()
return tok return tok
def __read_string(self):
position: int = self.position + 1
while True:
self.__read_char()
if self.current_char == '"' or self.current_char is None:
break

View File

@@ -11,6 +11,7 @@ class TokenType(Enum):
IDENT = "IDENT" IDENT = "IDENT"
INT = "INT" INT = "INT"
FLOAT = "FLOAT" FLOAT = "FLOAT"
STRING = "STRING"
# Arithmetic symbols # Arithmetic symbols
PLUS = "PLUS" PLUS = "PLUS"

View File

@@ -3,5 +3,6 @@ add = Func(a: Int, b: Int): Int {
} }
main = Func(): Int { main = Func(): Int {
return add(2, 3); print("apples %i", add(2, 3));
return 0;
} }