From 374e080f2612d45ce4525f696e651cbc19887738 Mon Sep 17 00:00:00 2001 From: Andrew Segavac Date: Sat, 29 May 2021 10:50:15 -0600 Subject: [PATCH] added support for floats --- boring/parse.py | 23 ++++++++++++++++++----- boring/type_checking.py | 21 ++++++++++++++------- examples/math/main.bl | 1 + notes.txt | 13 +++++++++++++ 4 files changed, 46 insertions(+), 12 deletions(-) diff --git a/boring/parse.py b/boring/parse.py index a7bb7cc..aba9492 100644 --- a/boring/parse.py +++ b/boring/parse.py @@ -62,6 +62,12 @@ class LiteralInt: type: TypeUsage +@dataclass +class LiteralFloat: + value: float + type: TypeUsage + + @dataclass class FunctionCall: source: "Expression" @@ -85,7 +91,7 @@ class VariableUsage: @dataclass class Expression: - expression: Union[LiteralInt, FunctionCall, VariableUsage, Operation] + expression: Union[LiteralInt, LiteralFloat, FunctionCall, VariableUsage, Operation] type: TypeUsage @@ -131,8 +137,9 @@ boring_grammar = r""" mult : "*" div : "/" - literal_int: SIGNED_NUMBER - identifier : NAME + literal_float : SIGNED_FLOAT + literal_int : SIGNED_INT + identifier : CNAME function_call : expression "(" [expression ("," expression)*] ")" @@ -152,6 +159,7 @@ boring_grammar = r""" | term term : literal_int + | literal_float | variable_usage | function_call | "(" expression ")" @@ -185,8 +193,9 @@ boring_grammar = r""" module : (function)* - %import common.CNAME -> NAME - %import common.SIGNED_NUMBER + %import common.CNAME + %import common.SIGNED_INT + %import common.SIGNED_FLOAT %import common.WS %ignore WS """ @@ -214,6 +223,10 @@ class TreeToBoring(Transformer): (n,) = n return LiteralInt(value=int(n), type=UnknownTypeUsage()) + def literal_float(self, f) -> LiteralFloat: + (f,) = f + return LiteralFloat(value=float(f), type=UnknownTypeUsage()) + def identifier(self, i) -> str: (i,) = i return str(i) diff --git a/boring/type_checking.py b/boring/type_checking.py index b88b24f..61739ea 100644 --- a/boring/type_checking.py +++ b/boring/type_checking.py @@ -131,6 +131,10 @@ class TypeChecker: if self.with_literal_int(env, type_env, subexpression): changed = True return changed + if isinstance(subexpression, parse.LiteralFloat): + if self.with_literal_float(env, type_env, subexpression): + changed = True + return changed if isinstance(subexpression, parse.FunctionCall): if self.with_function_call(env, type_env, subexpression): changed = True @@ -198,11 +202,14 @@ class TypeChecker: changed = True return changed - def with_literal_int(self, env: Environment, type_env: TypeEnvironment, literal_int: parse.LiteralInt) -> bool: - ints = [ - parse.DataTypeUsage(name=name) - for name in ["I8", "I16", "I32", "I64", "I128"] - ] - if not isinstance(literal_int.type, parse.UnknownTypeUsage): - assert literal_int.type in ints, f"{literal_int.type}" + def with_literal_float(self, env: Environment, type_env: TypeEnvironment, literal_float: parse.LiteralFloat) -> bool: + floats = ["F32", "F64", "F128"] + if not isinstance(literal_float.type, parse.UnknownTypeUsage): + assert literal_float.type.name in floats, f"{literal_float.type}" + return False + + def with_literal_int(self, env: Environment, type_env: TypeEnvironment, literal_int: parse.LiteralInt) -> bool: + ints = ["I8", "I16", "I32", "I64", "I128", "U8", "U16", "U32", "U64", "U128"] + if not isinstance(literal_int.type, parse.UnknownTypeUsage): + assert literal_int.type.name in ints, f"{literal_int.type}" return False diff --git a/examples/math/main.bl b/examples/math/main.bl index 56c7b22..4c492b1 100644 --- a/examples/math/main.bl +++ b/examples/math/main.bl @@ -1,5 +1,6 @@ fn add(a: I32, b: I32): I32 { let foo = 4; + let test_float: F32 = 10.2; a + b + foo } diff --git a/notes.txt b/notes.txt index 00d75d2..bfe30bf 100644 --- a/notes.txt +++ b/notes.txt @@ -26,3 +26,16 @@ class TypeUsage: result: Identifier # Result of useage - either is the type, or is the return value if it's a function type_args: List[Type] # Generics arguments: Optional[List[Type]] # Specified if it is a function, this is how you tell if it's a function + + + +TODO: +* ~Float Literals~ +* Block expression +* Return keyword +* Structs +* Generics +* Enums +* Methods +* Traits +* Async