From 39a5151d97abb6efd7f49650f08b6128b2c7a65b Mon Sep 17 00:00:00 2001 From: SpookyDervish <78246495+SpookyDervish@users.noreply.github.com> Date: Wed, 15 Oct 2025 07:48:38 +1100 Subject: [PATCH] started working on string literals --- AST.py | 14 ++++++++++++++ debug/ir.ll | 18 ++++++++++++++++-- lexer.py | 13 ++++++++++++- lexer_token.py | 1 + tests/test.pla | 3 ++- 5 files changed, 45 insertions(+), 4 deletions(-) diff --git a/AST.py b/AST.py index 668d3f5..a7bd515 100644 --- a/AST.py +++ b/AST.py @@ -23,6 +23,7 @@ class NodeType(Enum): FloatLiteral = "FloatLiteral" IdentifierLiteral = "IdentifierLiteral" BooleanLiteral = "BooleanLiteral" + StringLiteral = "StringLiteral" # Helper FunctionParameter = "FunctionParameter" @@ -124,6 +125,19 @@ class BooleanLiteral(Expression): "type": self.type().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 # region Statements diff --git a/debug/ir.ll b/debug/ir.ll index 3b32f62..1238b18 100644 --- a/debug/ir.ll +++ b/debug/ir.ll @@ -17,9 +17,23 @@ add_entry: 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"() { main_entry: - %".2" = call i32 @"add"(i32 2, i32 3) - ret i32 %".2" + %".2" = call i32 @"add"(i32 50, i32 50) + %".3" = call i32 @"sub"(i32 %".2", i32 125) + ret i32 %".3" } diff --git a/lexer.py b/lexer.py index 5fb1d72..6148f11 100644 --- a/lexer.py +++ b/lexer.py @@ -146,6 +146,8 @@ class Lexer: tok = self.__new_token(TokenType.COLON, self.current_char) case ",": tok = self.__new_token(TokenType.COMMA, self.current_char) + case '"': + tok = self.__new_token(TokenType.STRING, self.__read_string()) case None: tok = self.__new_token(TokenType.EOF, "") case _: @@ -162,4 +164,13 @@ class Lexer: tok = self.__new_token(TokenType.ILLEGAL, self.current_char) self.__read_char() - return tok \ No newline at end of file + 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 + + \ No newline at end of file diff --git a/lexer_token.py b/lexer_token.py index 68d1315..57bbe2d 100644 --- a/lexer_token.py +++ b/lexer_token.py @@ -11,6 +11,7 @@ class TokenType(Enum): IDENT = "IDENT" INT = "INT" FLOAT = "FLOAT" + STRING = "STRING" # Arithmetic symbols PLUS = "PLUS" diff --git a/tests/test.pla b/tests/test.pla index d20dadc..ac19427 100644 --- a/tests/test.pla +++ b/tests/test.pla @@ -3,5 +3,6 @@ add = Func(a: Int, b: Int): Int { } main = Func(): Int { - return add(2, 3); + print("apples %i", add(2, 3)); + return 0; } \ No newline at end of file