added type aliases, declaring impls

This commit is contained in:
Andrew Segavac
2021-06-13 10:38:13 -06:00
parent 972fbd064b
commit 02797309d9
5 changed files with 59 additions and 5 deletions

View File

@@ -2,6 +2,7 @@ import sys
from typing import List from typing import List
from boring.parse import boring_parser, TreeToBoring, pretty_print from boring.parse import boring_parser, TreeToBoring, pretty_print
from boring.type_checking import TypeChecker, Context from boring.type_checking import TypeChecker, Context
from boring.type_alias_resolution import TypeAliasResolver, Context as AliasContex
from boring import typedefs, parse from boring import typedefs, parse
builtins = { builtins = {
@@ -28,6 +29,9 @@ if __name__ == "__main__":
# print(tree) # print(tree)
result = TreeToBoring().transform(tree) result = TreeToBoring().transform(tree)
# pretty_print(result) # pretty_print(result)
alias_resolver = TypeAliasResolver()
alias_resolver.with_module(AliasContex([]), result)
pretty_print(result)
type_checker = TypeChecker() type_checker = TypeChecker()
while type_checker.with_module(Context(builtins, None), result): while type_checker.with_module(Context(builtins, None), result):
print("loop") print("loop")

View File

@@ -173,13 +173,25 @@ class StructTypeDeclaration:
fields: Dict[str, TypeUsage] fields: Dict[str, TypeUsage]
TypeDeclaration = Union[StructTypeDeclaration, PrimitiveTypeDeclaration] @dataclass
class AliasTypeDeclaration:
new: DataTypeUsage
old: TypeUsage
TypeDeclaration = Union[StructTypeDeclaration, PrimitiveTypeDeclaration, AliasTypeDeclaration]
@dataclass
class Impl:
struct: str
functions: List[Function]
@dataclass @dataclass
class Module: class Module:
functions: List[Function] functions: List[Function]
types: List[TypeDeclaration] types: List[TypeDeclaration]
impls: List[Impl]
boring_grammar = r""" boring_grammar = r"""
@@ -262,9 +274,14 @@ boring_grammar = r"""
struct_type_declaration : "type" identifier "struct" "{" (struct_definition_field ",")* "}" struct_type_declaration : "type" identifier "struct" "{" (struct_definition_field ",")* "}"
type_declaration : struct_type_declaration type_alias_declaration : "type" identifier "=" type_usage ";"
module : (function|type_declaration)* type_declaration : struct_type_declaration
| type_alias_declaration
impl : "impl" identifier "{" function* "}"
module : (function|type_declaration|impl)*
%import common.CNAME %import common.CNAME
%import common.SIGNED_INT %import common.SIGNED_INT
@@ -447,19 +464,29 @@ class TreeToBoring(Transformer):
fields = {key: value for (key, value) in struct_type_declaration[1:]} fields = {key: value for (key, value) in struct_type_declaration[1:]}
return StructTypeDeclaration(name=name, fields=fields) return StructTypeDeclaration(name=name, fields=fields)
def type_alias_declaration(self, type_alias_declaration) -> AliasTypeDeclaration:
(name, existing) = type_alias_declaration
return AliasTypeDeclaration(new=DataTypeUsage(name), old=type_alias_declaration)
def type_declaration(self, type_declaration): def type_declaration(self, type_declaration):
(type_declaration,) = type_declaration (type_declaration,) = type_declaration
return type_declaration return type_declaration
def impl(self, impl) -> Impl:
return Impl(struct=impl[0], functions=impl[1:])
def module(self, module_items) -> Module: def module(self, module_items) -> Module:
functions = [] functions = []
types = [] types = []
impls = []
for item in module_items: for item in module_items:
if isinstance(item, Function): if isinstance(item, Function):
functions.append(item) functions.append(item)
elif isinstance(item, Impl):
impls.append(item)
else: else:
types.append(item) types.append(item)
return Module(functions=functions, types=types) return Module(functions=functions, types=types, impls=impls)
boring_parser = Lark(boring_grammar, start="module", lexer="standard") boring_parser = Lark(boring_grammar, start="module", lexer="standard")

View File

@@ -97,7 +97,13 @@ class TypeChecker:
assert_exists(ctx, field) assert_exists(ctx, field)
for function in module.functions: for function in module.functions:
ctx.environment[function.name] = function ctx.environment[function.name] = function
changed = False changed = False
for impl in module.impls:
for function in impl.functions:
if self.with_function(ctx, function):
changed = True
for function in module.functions: for function in module.functions:
if self.with_function(ctx, function): if self.with_function(ctx, function):
changed = True changed = True
@@ -355,7 +361,7 @@ class TypeChecker:
def with_literal_struct( def with_literal_struct(
self, ctx: Context, literal_struct: parse.LiteralStruct self, ctx: Context, literal_struct: parse.LiteralStruct
) -> bool: ) -> bool:
assert literal_struct.name in ctx.environment assert literal_struct.name in ctx.environment, literal_struct.name
struct_declaration = ctx.environment[literal_struct.name] struct_declaration = ctx.environment[literal_struct.name]
assert isinstance(struct_declaration, parse.StructTypeDeclaration) assert isinstance(struct_declaration, parse.StructTypeDeclaration)
changed = False changed = False

View File

@@ -48,3 +48,15 @@ fn get_user_id(): U64 {
type User struct { type User struct {
id: U64, id: U64,
} }
impl User {
fn new(id: U64): Self {
return Self{
id: id,
};
}
fn get_id(self: Self): U64 {
return self.id;
}
}

View File

@@ -42,7 +42,10 @@ TODO:
* ~Literal~ * ~Literal~
* ~Getter~ * ~Getter~
* ~Setter~ * ~Setter~
* ~Type Aliases~
* Methods * Methods
* ~Declaration~
* Use
* Traits * Traits
* Generics * Generics
* Enums * Enums
@@ -51,3 +54,5 @@ TODO:
* Lambdas * Lambdas
* Async * Async
* Imports * Imports
* Visibility
* Const / Mut