Skip to content

Commit

Permalink
feat(compiler)!: Implement Infinity and NaN syntax (#1472)
Browse files Browse the repository at this point in the history
Closes #693
  • Loading branch information
spotandjake committed Jan 11, 2023
1 parent 6dd9680 commit 74a196d
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 1 deletion.
4 changes: 3 additions & 1 deletion compiler/src/parsing/lexer.re
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,13 @@ let dec_float_decimal_explicit = [%sedlex.regexp?
let dec_float_integral = [%sedlex.regexp?
(dec_digit, Star(dec_digit | '_'))
];
let dec_float_alphabetic = [%sedlex.regexp? "Infinity" | "NaN"];

let dec_float = [%sedlex.regexp?
(dec_float_integral, dec_float_decimal, Opt(dec_float_exp)) |
(dec_float_decimal_explicit, Opt(dec_float_exp)) |
(dec_float_integral, dec_float_exp)
(dec_float_integral, dec_float_exp) |
dec_float_alphabetic
];

let unsigned_float = [%sedlex.regexp? dec_float];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
basic functionality › infinity
(module
(type $none_=>_none (func))
(type $none_=>_i32 (func (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(import \"_grainEnv\" \"mem\" (memory $0 0))
(import \"_grainEnv\" \"tbl\" (table $tbl 0 funcref))
(import \"_grainEnv\" \"relocBase\" (global $relocBase_0 i32))
(import \"_grainEnv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$malloc\" (global $GRAIN$EXPORT$malloc_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"malloc\" (func $malloc_0 (param i32 i32) (result i32)))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
(export \"memory\" (memory $0))
(export \"_gmain\" (func $_gmain))
(export \"_gtype_metadata\" (func $_gtype_metadata))
(export \"_start\" (func $_start))
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
(func $_gtype_metadata
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i64)
(local $4 f32)
(local $5 f64)
(nop)
)
(func $_gmain (result i32)
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i64)
(local $4 f32)
(local $5 f64)
(return
(block $cleanup_locals.3 (result i32)
(local.set $0
(block $compile_block.2 (result i32)
(block $allocate_number.1 (result i32)
(i32.store
(local.tee $0
(call $malloc_0
(global.get $GRAIN$EXPORT$malloc_0)
(i32.const 16)
)
)
(i32.const 5)
)
(i32.store offset=4
(local.get $0)
(i32.const 2)
)
(f64.store offset=8
(local.get $0)
(f64.const inf)
)
(local.get $0)
)
)
)
(local.get $0)
)
)
)
(func $_start
(drop
(call $_gmain)
)
)
;; custom section \"cmi\", size 240
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
basic functionality › nan
(module
(type $none_=>_none (func))
(type $none_=>_i32 (func (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(import \"_grainEnv\" \"mem\" (memory $0 0))
(import \"_grainEnv\" \"tbl\" (table $tbl 0 funcref))
(import \"_grainEnv\" \"relocBase\" (global $relocBase_0 i32))
(import \"_grainEnv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$malloc\" (global $GRAIN$EXPORT$malloc_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"malloc\" (func $malloc_0 (param i32 i32) (result i32)))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
(export \"memory\" (memory $0))
(export \"_gmain\" (func $_gmain))
(export \"_gtype_metadata\" (func $_gtype_metadata))
(export \"_start\" (func $_start))
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
(func $_gtype_metadata
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i64)
(local $4 f32)
(local $5 f64)
(nop)
)
(func $_gmain (result i32)
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i64)
(local $4 f32)
(local $5 f64)
(return
(block $cleanup_locals.3 (result i32)
(local.set $0
(block $compile_block.2 (result i32)
(block $allocate_number.1 (result i32)
(i32.store
(local.tee $0
(call $malloc_0
(global.get $GRAIN$EXPORT$malloc_0)
(i32.const 16)
)
)
(i32.const 5)
)
(i32.store offset=4
(local.get $0)
(i32.const 2)
)
(f64.store offset=8
(local.get $0)
(f64.const nan:0x8000000000000)
)
(local.get $0)
)
)
)
(local.get $0)
)
)
)
(func $_start
(drop
(call $_gmain)
)
)
;; custom section \"cmi\", size 235
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
basic functionality › infinity_neg
(module
(type $none_=>_none (func))
(type $none_=>_i32 (func (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(import \"_grainEnv\" \"mem\" (memory $0 0))
(import \"_grainEnv\" \"tbl\" (table $tbl 0 funcref))
(import \"_grainEnv\" \"relocBase\" (global $relocBase_0 i32))
(import \"_grainEnv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32))
(import \"GRAIN$MODULE$runtime/gc\" \"GRAIN$EXPORT$malloc\" (global $GRAIN$EXPORT$malloc_0 (mut i32)))
(import \"GRAIN$MODULE$runtime/gc\" \"malloc\" (func $malloc_0 (param i32 i32) (result i32)))
(global $GRAIN$TABLE_SIZE i32 (i32.const 0))
(elem $elem (global.get $relocBase_0))
(export \"memory\" (memory $0))
(export \"_gmain\" (func $_gmain))
(export \"_gtype_metadata\" (func $_gtype_metadata))
(export \"_start\" (func $_start))
(export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE))
(func $_gtype_metadata
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i64)
(local $4 f32)
(local $5 f64)
(nop)
)
(func $_gmain (result i32)
(local $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i64)
(local $4 f32)
(local $5 f64)
(return
(block $cleanup_locals.3 (result i32)
(local.set $0
(block $compile_block.2 (result i32)
(block $allocate_number.1 (result i32)
(i32.store
(local.tee $0
(call $malloc_0
(global.get $GRAIN$EXPORT$malloc_0)
(i32.const 16)
)
)
(i32.const 5)
)
(i32.store offset=4
(local.get $0)
(i32.const 2)
)
(f64.store offset=8
(local.get $0)
(f64.const -inf)
)
(local.get $0)
)
)
)
(local.get $0)
)
)
)
(func $_start
(drop
(call $_gmain)
)
)
;; custom section \"cmi\", size 244
)
3 changes: 3 additions & 0 deletions compiler/test/suites/basic_functionality.re
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ describe("basic functionality", ({test, testSkip}) => {
assertSnapshot("oct_neg", "-0o77");
assertSnapshot("fals", "let x = false; x");
assertSnapshot("tru", "let x = true; x");
assertSnapshot("infinity", "let x = Infinity; x");
assertSnapshot("infinity_neg", "let x = -Infinity; x");
assertSnapshot("nan", "let x = NaN; x");
assertSnapshot(
"complex1",
"\n let x = 2, y = 3, z = if (true) { 4 } else { 5 };\n if (true) {\n print(y)\n y - (z + x)\n } else {\n print(8)\n 8\n }\n ",
Expand Down

0 comments on commit 74a196d

Please sign in to comment.