Skip to content

Commit

Permalink
Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Genius_um committed Jan 24, 2025
1 parent 196afd9 commit 072ecec
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 134 deletions.
4 changes: 4 additions & 0 deletions playground.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
ini u8 var1 = [2]; // var1 -> u8 2 (top of the stack)
ini u8* var2 = [2 .%]; // var2 -> u8* 2 (top of the stack)
ini u8* var3 = [2 3 ..%]; // var3 -> u8* 2 (base of the stack)
ini u8 var4 = [2 3 ..% %]; // var4 -> u8 2 (base of the stack)
5 changes: 3 additions & 2 deletions specs.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- `extern` and `intern` keywords
-
- [ ] Advanced types
- [ ] Names
- [ ] Stack operators
13 changes: 11 additions & 2 deletions src/lib/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,28 @@ def check_instructions(self, blocks:list, inner:any=None):
self.check_instructions(segment_block.elements, entry)
elif instruction.verify("instruction", "return"):
if not inner_is_block:
self.raise_exception(self.InvalidInstructionContext, "Only in functions.")
self.raise_exception(self.InvalidInstructionContext, "Not in a function.")
if len(s_arguments) == 1:
value_token = s_arguments[0]
if not self.verify_literal_value_type(value_token):
self.raise_exception(self.InvalidInstructionSyntax, "Not a valid literal value type.")
value = values.LiteralValue(self, value_token, builder)
builder.ret(value.value)
try: builder.ret(value.value)
except AssertionError:
self.raise_exception(self.InvalidInstructionContext, "Function already returned.")
elif not len(s_arguments):
if str(function.function_type.return_type) != "void":
self.raise_exception(self.InvalidInstructionContext, "The function don't returns a void value.")
builder.ret_void()
try: builder.ret(value.value)
except AssertionError:
self.raise_exception(self.InvalidInstructionContext, "Function already returned.")
else:
self.raise_exception(self.InvalidInstructionSyntax, "Too many arguments.")
elif instruction.verify("instruction", "ini"):
if not inner_is_block:
self.raise_exception(self.InvalidInstructionContext, "Not in a function.")
... # TODO
else:
self.raise_exception(self.InvalidInstruction)

Expand Down
14 changes: 10 additions & 4 deletions src/lib/lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,15 @@
SLASH_STAR = "/*"
STAR_SLASH = "*/"
DOUBLE_COLON = "::"
DOT_PERCENTAGE = ".%"

# Three chars
DOT_DOT_PERCENTAGE = "..%"

# Operators
OPERATORS = [
HASHTAG, DOUBLE_HASHTAG, TILDE, PERCENTAGE, COLON, PERCENTAGE, EQUAL,
PLUS # Stack operators
HASHTAG, DOUBLE_HASHTAG, TILDE, COLON, PERCENTAGE, EQUAL,
PLUS, DOT_PERCENTAGE, DOT_DOT_PERCENTAGE # Stack operators
]

# Delimiters
Expand All @@ -52,7 +56,7 @@
REGISTERS = ["ax", "bx", "cx", "dx", "si", "di", "bp", "sp"]

# Instructions
INSTRUCTIONS = ["exit", "write", "ini", "func", "return"]
INSTRUCTIONS = ["ini", "func", "return"]

# Types
UNSIGNED_8 = ir.IntType(8)
Expand Down Expand Up @@ -83,8 +87,10 @@
"chr": CHAR,
"bool": BOOLEAN,
"void": VOID,
# Aliases :
"int": UNSIGNED_32,
"dec": FLOAT_64
"dec": FLOAT_64,
"byte": UNSIGNED_8
}

TYPES = list(TYPES_WITH_LLTYPES.keys())
Expand Down
13 changes: 12 additions & 1 deletion src/lib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ def parse(self, content:str):
next_part = utils.get_item_safe(parts, index + 1)
next_part_2 = utils.get_item_safe(parts, index + 2)
next_part_3 = utils.get_item_safe(parts, index + 3)
next_part_4 = utils.get_item_safe(parts, index + 4)

if part + next_part == lang.DOUBLE_SLASH:
break
Expand All @@ -142,6 +143,11 @@ def parse(self, content:str):
self.raise_sourcecode_exception(line_recreation, segments[-1]["line"], part_column, self.InvalidStringReference)
token = lang.Token(string, "string")
parts_to_skip = 1
parts_to_skip = 2
elif lang.is_a_decimal(part + next_part + next_part_2) and next_part_3 == lang.COLON and lang.is_a_type(next_part_4):
token = lang.Token(part + next_part + next_part_2)
token.type = lang.get_type_from_token(lang.Token(next_part_4, "type"))
parts_to_skip = 4
elif lang.is_a_decimal(part + next_part + next_part_2):
token = lang.Token(part + next_part + next_part_2)
parts_to_skip = 2
Expand Down Expand Up @@ -171,10 +177,15 @@ def parse(self, content:str):
token.size = int(part)
parts_to_skip = 2
elif (
lang.is_an_integer(part) or lang.is_a_decimal(part) or lang.is_a_valid_name(part) or lang.is_a_boolean(part) or part == lang.CLOSE_HOOK
lang.is_an_integer(part) or lang.is_a_valid_name(part) or lang.is_a_boolean(part) or part == lang.CLOSE_HOOK
) and next_part == lang.COLON and lang.is_a_type(next_part_2):
token = lang.Token(part)
token.type = lang.get_type_from_token(lang.Token(next_part_2, "type"))
elif part + next_part == lang.DOT_PERCENTAGE:
token = lang.Token(lang.DOT_PERCENTAGE, "operator")
parts_to_skip = 1
elif part + next_part + next_part_2 == lang.DOT_DOT_PERCENTAGE:
token = lang.Token(lang.DOT_DOT_PERCENTAGE, "operator")
parts_to_skip = 2
else:
token = lang.Token(part)
Expand Down
17 changes: 14 additions & 3 deletions src/lib/stack.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,19 @@ def __init__(self, builder:ir.IRBuilder, size:int, id:str):
name=f"stacktop_{id}"
)
self.builder.store(ir.Constant(lang.UNSIGNED_32, 0), self.top_ptr)
self.function = self.builder.function


self.size_ptr = self.builder.gep(
self.stack,
[ir.Constant(lang.UNSIGNED_32, 0), ir.Constant(lang.UNSIGNED_32, 2)],
name=f"stacksize_{id}"
)
self.builder.store(ir.Constant(lang.UNSIGNED_32, self.size), self.size_ptr)

self.base_ptr = self.builder.gep(
self.stack,
[ir.Constant(lang.UNSIGNED_32, 0), ir.Constant(lang.UNSIGNED_32, 0)],
name=f"stackbase_{self.id}"
)

def push(self, value:ir.Value):
if not value.type.is_pointer:
Expand All @@ -44,6 +49,12 @@ def push(self, value:ir.Value):
value = alloca
cast_value = self.builder.bitcast(value, lang.VOID_PTR)
return self.builder.call(self.push_function, [self.stack, cast_value])

def push_top_ptr(self):
self.push(self.builder.load(self.top_ptr))

def push_base_ptr(self):
self.push(self.builder.load(self.base_ptr))

def pop(self):
return self.builder.call(self.pop_function, [self.stack])
Expand Down Expand Up @@ -114,4 +125,4 @@ def define_pop(self):

pop_builder.ret(lang.NULL_PTR)

return pop_func
return pop_func
8 changes: 8 additions & 0 deletions src/lib/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
class LiteralValue():
class InvalidElementType(BaseException): ...
class InvalidLiteralValueType(BaseException): ...
class InvalidOperator(BaseException): ...

def __init__(self, compiler, token:lang.Token, builder:ir.IRBuilder):
self.compiler = compiler
Expand Down Expand Up @@ -69,6 +70,13 @@ def proc(self):
if self.compiler.verify_literal_value_type(element):
value = LiteralValue(self.compiler, element, self.builder)
self.stack.push(value.value)
elif lang.is_a_token(element) and element.verify_type("operator"):
if element.verify(lang.DOT_PERCENTAGE, "operator"):
self.stack.push_top_ptr()
elif element.verify(lang.DOT_DOT_PERCENTAGE, "operator"):
self.stack.push_base_ptr()
else:
self.compiler.raise_exception(self.InvalidOperator)
else:
self.compiler.raise_exception(self.InvalidLiteralValueType)
conv_type = lang.UNSIGNED_8.as_pointer()
Expand Down
28 changes: 0 additions & 28 deletions test1.py

This file was deleted.

42 changes: 0 additions & 42 deletions test2.py

This file was deleted.

36 changes: 0 additions & 36 deletions test3.py

This file was deleted.

Binary file removed tests/dev2
Binary file not shown.
13 changes: 0 additions & 13 deletions tests/dev2.ll

This file was deleted.

6 changes: 3 additions & 3 deletions tests/dev2.pim
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
func u8 add(u8 a, u8 b);

func u8 main() {
return 12;
return 13;
func byte main() {
// ini u8 x = 2;
return [45 .%];
};

0 comments on commit 072ecec

Please sign in to comment.