From 757b442bdcc8687f548fabccdfcd535edca6a5bf Mon Sep 17 00:00:00 2001 From: Alex Snezhko Date: Sun, 26 Jan 2025 20:39:22 -0800 Subject: [PATCH] Add metadata helper AST records --- compiler/src/parsing/ast_helper.re | 8 +- compiler/src/parsing/parser.mly | 6 +- compiler/src/parsing/parser_header.re | 20 ++++- compiler/src/parsing/parser_utils.re | 2 +- compiler/src/parsing/parsetree.re | 12 ++- compiler/src/parsing/well_formedness.re | 7 +- compiler/src/typed/typecore.re | 11 ++- .../basic_functionality.16eea1e9.0.snapshot | 81 ++++++++----------- .../basic_functionality.7edbad66.0.snapshot | 81 ++++++++----------- compiler/test/suites/blocks.re | 4 +- 10 files changed, 122 insertions(+), 110 deletions(-) diff --git a/compiler/src/parsing/ast_helper.re b/compiler/src/parsing/ast_helper.re index 64d5bf6de..389d29bf8 100644 --- a/compiler/src/parsing/ast_helper.re +++ b/compiler/src/parsing/ast_helper.re @@ -210,7 +210,9 @@ module Expression = { pexp_loc: loc, pexp_core_loc: core_loc, // Placehoder; will be corrected later in parsing - pexp_in_parens: false, + pexp_meta: { + pexpmd_in_parens: false, + }, }; }; let ident = (~loc, ~core_loc, ~attributes=?, a) => @@ -433,7 +435,9 @@ module Toplevel = { ptop_loc: loc, ptop_core_loc: core_loc, // Placehoder; will be corrected later in parsing - ptop_ends_semi: false, + ptop_meta: { + pstmtmd_ends_semi: false, + }, }; }; let include_ = (~loc, ~core_loc, ~attributes=?, i) => diff --git a/compiler/src/parsing/parser.mly b/compiler/src/parsing/parser.mly index f05cea990..1cd4279ff 100644 --- a/compiler/src/parsing/parser.mly +++ b/compiler/src/parsing/parser.mly @@ -442,7 +442,7 @@ unop_expr: | prefix_op non_assign_expr { Expression.apply ~loc:(to_loc $loc) ~core_loc:(to_loc $loc) (mkid_expr $loc($1) [mkstr $loc($1) $1]) [{paa_label=Unlabeled; paa_expr=$2; paa_loc=(to_loc $loc($2))}] } paren_expr: - | lparen expr rparen { {$2 with pexp_in_parens=true} } + | lparen expr rparen { {$2 with pexp_meta={pexpmd_in_parens=true}} } app_arg: | expr { {paa_label=Unlabeled; paa_expr=$1; paa_loc=to_loc $loc} } @@ -683,8 +683,8 @@ record_exprs: | record_field comma lseparated_nonempty_list(comma, record_field) comma? { $1::$3 } block_body_inner: - | block_body_inner eos block_body_expr { {pblk_expr=$3; pblk_ends_semi=false}::(apply_semicolon_block $1 $2) } - | block_body_expr { [{pblk_expr=$1; pblk_ends_semi=false}] } + | block_body_inner eos block_body_expr { {pblk_expr=$3; pblk_meta={pstmtmd_ends_semi=false}}::(apply_semicolon_block $1 $2) } + | block_body_expr { [{pblk_expr=$1; pblk_meta={pstmtmd_ends_semi=false}}] } block_body: | block_body_inner ioption(eos) %prec SEMI { List.rev (apply_semicolon_block $1 (Option.value ~default:false $2)) } diff --git a/compiler/src/parsing/parser_header.re b/compiler/src/parsing/parser_header.re index d66d60fa8..611cc71e0 100644 --- a/compiler/src/parsing/parser_header.re +++ b/compiler/src/parsing/parser_header.re @@ -6,14 +6,30 @@ open Grain_utils; let apply_semicolon_block = (rev_block_exprs, has_semicolon) => { switch (rev_block_exprs) { - | [first, ...rest] => [{...first, pblk_ends_semi: has_semicolon}, ...rest] + | [first, ...rest] => [ + { + ...first, + pblk_meta: { + pstmtmd_ends_semi: has_semicolon, + }, + }, + ...rest, + ] | [] => [] }; }; let apply_semicolon_toplevels = (rev_toplevel_exprs, has_semicolon) => { switch (rev_toplevel_exprs) { - | [first, ...rest] => [{...first, ptop_ends_semi: has_semicolon}, ...rest] + | [first, ...rest] => [ + { + ...first, + ptop_meta: { + pstmtmd_ends_semi: has_semicolon, + }, + }, + ...rest, + ] | [] => [] }; }; diff --git a/compiler/src/parsing/parser_utils.re b/compiler/src/parsing/parser_utils.re index 37075f90d..c8a13de2a 100644 --- a/compiler/src/parsing/parser_utils.re +++ b/compiler/src/parsing/parser_utils.re @@ -44,7 +44,7 @@ let is_prefix_op = expr => { }; let rec starts_with_negative_value = (~bypass_parens, expr) => - if (bypass_parens && expr.pexp_in_parens) { + if (bypass_parens && expr.pexp_meta.pexpmd_in_parens) { false; } else { switch (expr.pexp_desc) { diff --git a/compiler/src/parsing/parsetree.re b/compiler/src/parsing/parsetree.re index baa558f7e..e272db00d 100644 --- a/compiler/src/parsing/parsetree.re +++ b/compiler/src/parsing/parsetree.re @@ -501,13 +501,19 @@ type attribute = Asttypes.attribute; [@deriving (sexp, yojson)] type attributes = Asttypes.attributes; +[@deriving (sexp, yojson)] +type expression_metadata = {pexpmd_in_parens: bool}; + +[@deriving (sexp, yojson)] +type stmtlike_metadata = {pstmtmd_ends_semi: bool}; + /** Type for expressions (i.e. things which evaluate to something) */ [@deriving (sexp, yojson)] type expression = { pexp_desc: expression_desc, pexp_attributes: attributes, - pexp_in_parens: bool, + pexp_meta: expression_metadata, [@sexp_drop_if sexp_locs_disabled] pexp_loc: Location.t, // The full location, including attributes [@sexp_drop_if sexp_locs_disabled] @@ -561,7 +567,7 @@ and expression_desc = [@deriving (sexp, yojson)] and block_expression = { pblk_expr: expression, - pblk_ends_semi: bool, + pblk_meta: stmtlike_metadata, } [@deriving (sexp, yojson)] @@ -681,7 +687,7 @@ and toplevel_stmt_desc = and toplevel_stmt = { ptop_desc: toplevel_stmt_desc, ptop_attributes: attributes, - ptop_ends_semi: bool, + ptop_meta: stmtlike_metadata, [@sexp_drop_if sexp_locs_disabled] ptop_loc: Location.t, // The full location, including attributes [@sexp_drop_if sexp_locs_disabled] diff --git a/compiler/src/parsing/well_formedness.re b/compiler/src/parsing/well_formedness.re index 5ffab4258..7ebb6129d 100644 --- a/compiler/src/parsing/well_formedness.re +++ b/compiler/src/parsing/well_formedness.re @@ -891,7 +891,7 @@ let line_starts_with_negative_value = (errs, super) => { let rec check_block_exprs_lone_negative = block_exprs => { switch (block_exprs) { | [first, ...[second, ..._] as rest] => - if (!first.pblk_ends_semi) { + if (!first.pblk_meta.pstmtmd_ends_semi) { report_lone_negative_value(second.pblk_expr); }; @@ -904,7 +904,10 @@ let line_starts_with_negative_value = (errs, super) => { switch (stmts) { | [first, ...[{ptop_desc: PTopExpr(expr)}, ..._] as rest] => switch (first) { - | {ptop_ends_semi: false, ptop_desc: PTopExpr(_) | PTopLet(_)} => + | { + ptop_meta: {pstmtmd_ends_semi: false}, + ptop_desc: PTopExpr(_) | PTopLet(_), + } => report_lone_negative_value(expr) | _ => () }; diff --git a/compiler/src/typed/typecore.re b/compiler/src/typed/typecore.re index 65ba8aab5..bfcea8723 100644 --- a/compiler/src/typed/typecore.re +++ b/compiler/src/typed/typecore.re @@ -1375,7 +1375,12 @@ and type_expect_ = ~loc=body.pexp_loc, ~core_loc=body.pexp_core_loc, List.map( - expr => {pblk_expr: expr, pblk_ends_semi: false}, + expr => { + pblk_expr: expr, + pblk_meta: { + pstmtmd_ends_semi: false, + }, + }, prelude @ [body], ), ) @@ -2110,7 +2115,9 @@ and type_construct = pexp_attributes: attrs, pexp_loc: loc, pexp_core_loc: loc, - pexp_in_parens: false, + pexp_meta: { + pexpmd_in_parens: false, + }, }, ], true, diff --git a/compiler/test/__snapshots__/basic_functionality.16eea1e9.0.snapshot b/compiler/test/__snapshots__/basic_functionality.16eea1e9.0.snapshot index 1b32fbdc3..140a7e5ea 100644 --- a/compiler/test/__snapshots__/basic_functionality.16eea1e9.0.snapshot +++ b/compiler/test/__snapshots__/basic_functionality.16eea1e9.0.snapshot @@ -1,48 +1,35 @@ basic functionality › binop2.6 -(module - (type $none_=>_i32 (func (result i32))) - (type $none_=>_none (func)) - (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) - (type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32))) - (import \"_genv\" \"tbl\" (table $tbl 0 funcref)) - (import \"_genv\" \"relocBase\" (global $relocBase_0 i32)) - (import \"_genv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32)) - (import \"_genv\" \"runtimeHeapStart\" (global $runtimeHeapStart_0 i32)) - (import \"_genv\" \"runtimeHeapNextPtr\" (global $runtimeHeapNextPtr_0 (mut i32))) - (import \"_genv\" \"metadataPtr\" (global $metadataPtr_0 i32)) - (import \"GRAIN$MODULE$runtime/gc.gr\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32))) - (import \"GRAIN$MODULE$pervasives.gr\" \"GRAIN$EXPORT$-\" (global $-_1113 (mut i32))) - (import \"GRAIN$MODULE$runtime/gc.gr\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32))) - (import \"GRAIN$MODULE$pervasives.gr\" \"-\" (func $-_1113 (param i32 i32 i32) (result i32))) - (global $GRAIN$TABLE_SIZE i32 (i32.const 0)) - (memory $0 0) - (elem $elem (global.get $relocBase_0)) - (export \"memory\" (memory $0)) - (export \"_gmain\" (func $_gmain)) - (export \"_start\" (func $_start)) - (export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE)) - (func $_gmain (result i32) - (local $0 i32) - (local $1 i32) - (local $2 i32) - (local $3 i64) - (local $4 f32) - (local $5 f64) - (block $compile_block.1 - (return_call $-_1113 - (call $incRef_0 - (global.get $GRAIN$EXPORT$incRef_0) - (global.get $-_1113) - ) - (i32.const 5) - (i32.const 5) - ) - ) - ) - (func $_start - (drop - (call $_gmain) - ) - ) - ;; custom section \"cmi\", size 368 -) +((mash_code + ((functions ()) + (imports + (((mimp_id ((name -))) (mimp_mod pervasives.gr) (mimp_name -) + (mimp_type + (MFuncImport (Managed (Unmanaged WasmI32) (Unmanaged WasmI32)) + ((Unmanaged WasmI32)))) + (mimp_kind MImportGrain) (mimp_setup MSetupNone) (mimp_used true)) + ((mimp_id ((name -))) (mimp_mod pervasives.gr) (mimp_name -) + (mimp_type (MGlobalImport Managed true)) (mimp_kind MImportGrain) + (mimp_setup MCallGetter) (mimp_used true)))) + (exports ()) + (main_body + (((instr_desc + (MReturnCallKnown (func -_1113) + (closure + ((immediate_desc + (MIncRef + ((immediate_desc (MImmBinding (MGlobalBind -_1113 Managed))) + (immediate_analyses ((last_usage Last)))))) + (immediate_analyses ((last_usage Unknown))))) + (func_type ((Managed Managed) (Managed))) + (args + (((immediate_desc (MImmConst (MConstI32 2))) + (immediate_analyses ((last_usage Unknown)))) + ((immediate_desc (MImmConst (MConstI32 2))) + (immediate_analyses ((last_usage Unknown))))))))))) + (main_body_stack_size + ((stack_size_ptr 0) (stack_size_i32 0) (stack_size_i64 0) + (stack_size_f32 0) (stack_size_f64 0))) + (globals ()) (function_table_elements ()) + (global_function_table_offset ((name function_table_global))) + (compilation_mode Normal) (type_metadata ))) + (signature )) diff --git a/compiler/test/__snapshots__/basic_functionality.7edbad66.0.snapshot b/compiler/test/__snapshots__/basic_functionality.7edbad66.0.snapshot index 181dd0863..2be21a12a 100644 --- a/compiler/test/__snapshots__/basic_functionality.7edbad66.0.snapshot +++ b/compiler/test/__snapshots__/basic_functionality.7edbad66.0.snapshot @@ -1,48 +1,35 @@ basic functionality › binop2.5 -(module - (type $none_=>_i32 (func (result i32))) - (type $none_=>_none (func)) - (type $i32_i32_=>_i32 (func (param i32 i32) (result i32))) - (type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32))) - (import \"_genv\" \"tbl\" (table $tbl 0 funcref)) - (import \"_genv\" \"relocBase\" (global $relocBase_0 i32)) - (import \"_genv\" \"moduleRuntimeId\" (global $moduleRuntimeId_0 i32)) - (import \"_genv\" \"runtimeHeapStart\" (global $runtimeHeapStart_0 i32)) - (import \"_genv\" \"runtimeHeapNextPtr\" (global $runtimeHeapNextPtr_0 (mut i32))) - (import \"_genv\" \"metadataPtr\" (global $metadataPtr_0 i32)) - (import \"GRAIN$MODULE$runtime/gc.gr\" \"GRAIN$EXPORT$incRef\" (global $GRAIN$EXPORT$incRef_0 (mut i32))) - (import \"GRAIN$MODULE$pervasives.gr\" \"GRAIN$EXPORT$-\" (global $-_1113 (mut i32))) - (import \"GRAIN$MODULE$runtime/gc.gr\" \"incRef\" (func $incRef_0 (param i32 i32) (result i32))) - (import \"GRAIN$MODULE$pervasives.gr\" \"-\" (func $-_1113 (param i32 i32 i32) (result i32))) - (global $GRAIN$TABLE_SIZE i32 (i32.const 0)) - (memory $0 0) - (elem $elem (global.get $relocBase_0)) - (export \"memory\" (memory $0)) - (export \"_gmain\" (func $_gmain)) - (export \"_start\" (func $_start)) - (export \"GRAIN$TABLE_SIZE\" (global $GRAIN$TABLE_SIZE)) - (func $_gmain (result i32) - (local $0 i32) - (local $1 i32) - (local $2 i32) - (local $3 i64) - (local $4 f32) - (local $5 f64) - (block $compile_block.1 - (return_call $-_1113 - (call $incRef_0 - (global.get $GRAIN$EXPORT$incRef_0) - (global.get $-_1113) - ) - (i32.const 5) - (i32.const 5) - ) - ) - ) - (func $_start - (drop - (call $_gmain) - ) - ) - ;; custom section \"cmi\", size 368 -) +((mash_code + ((functions ()) + (imports + (((mimp_id ((name -))) (mimp_mod pervasives.gr) (mimp_name -) + (mimp_type + (MFuncImport (Managed (Unmanaged WasmI32) (Unmanaged WasmI32)) + ((Unmanaged WasmI32)))) + (mimp_kind MImportGrain) (mimp_setup MSetupNone) (mimp_used true)) + ((mimp_id ((name -))) (mimp_mod pervasives.gr) (mimp_name -) + (mimp_type (MGlobalImport Managed true)) (mimp_kind MImportGrain) + (mimp_setup MCallGetter) (mimp_used true)))) + (exports ()) + (main_body + (((instr_desc + (MReturnCallKnown (func -_1113) + (closure + ((immediate_desc + (MIncRef + ((immediate_desc (MImmBinding (MGlobalBind -_1113 Managed))) + (immediate_analyses ((last_usage Last)))))) + (immediate_analyses ((last_usage Unknown))))) + (func_type ((Managed Managed) (Managed))) + (args + (((immediate_desc (MImmConst (MConstI32 2))) + (immediate_analyses ((last_usage Unknown)))) + ((immediate_desc (MImmConst (MConstI32 2))) + (immediate_analyses ((last_usage Unknown))))))))))) + (main_body_stack_size + ((stack_size_ptr 0) (stack_size_i32 0) (stack_size_i64 0) + (stack_size_f32 0) (stack_size_f64 0))) + (globals ()) (function_table_elements ()) + (global_function_table_offset ((name function_table_global))) + (compilation_mode Normal) (type_metadata ))) + (signature )) diff --git a/compiler/test/suites/blocks.re b/compiler/test/suites/blocks.re index fb0986a01..002466711 100644 --- a/compiler/test/suites/blocks.re +++ b/compiler/test/suites/blocks.re @@ -31,7 +31,9 @@ describe("blocks", ({test}) => { Identifier.IdentName(Location.mknoloc("Foo")), ), ), - pblk_ends_semi: false, + pblk_meta: { + pstmtmd_ends_semi: false, + }, }, ], ),