started working on string literals
This commit is contained in:
14
AST.py
14
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
|
||||
|
||||
18
debug/ir.ll
18
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"
|
||||
}
|
||||
|
||||
11
lexer.py
11
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 _:
|
||||
@@ -163,3 +165,12 @@ class Lexer:
|
||||
|
||||
self.__read_char()
|
||||
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
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ class TokenType(Enum):
|
||||
IDENT = "IDENT"
|
||||
INT = "INT"
|
||||
FLOAT = "FLOAT"
|
||||
STRING = "STRING"
|
||||
|
||||
# Arithmetic symbols
|
||||
PLUS = "PLUS"
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
Reference in New Issue
Block a user