diff options
| author | Daniel Gavin <danielgavin5@hotmail.com> | 2022-03-17 10:20:55 +0100 |
|---|---|---|
| committer | Daniel Gavin <danielgavin5@hotmail.com> | 2022-03-17 10:20:55 +0100 |
| commit | 3871d4872fb8ca54839375af3b0ac5c5f5aa0375 (patch) | |
| tree | 88aba18586fd679d173b51cb9f722b98f6029dcd /src | |
| parent | f2e84036ecc3200b684c0ea6d16a7397f766acca (diff) | |
Fix no_bounds_check
Diffstat (limited to 'src')
| -rw-r--r-- | src/odin/printer/document.odin | 2 | ||||
| -rw-r--r-- | src/odin/printer/printer.odin | 26 | ||||
| -rw-r--r-- | src/odin/printer/visit.odin | 114 |
3 files changed, 101 insertions, 41 deletions
diff --git a/src/odin/printer/document.odin b/src/odin/printer/document.odin index 3f7bb69..5c14704 100644 --- a/src/odin/printer/document.odin +++ b/src/odin/printer/document.odin @@ -162,7 +162,6 @@ cons :: proc(lhs: ^Document, rhs: ^Document, allocator := context.allocator) -> } cons_with_opl :: proc(lhs: ^Document, rhs: ^Document, allocator := context.allocator) -> ^Document { - if _, ok := lhs.(Document_Nil); ok { return rhs } @@ -175,7 +174,6 @@ cons_with_opl :: proc(lhs: ^Document, rhs: ^Document, allocator := context.alloc } cons_with_nopl:: proc(lhs: ^Document, rhs: ^Document, allocator := context.allocator) -> ^Document { - if _, ok := lhs.(Document_Nil); ok { return rhs } diff --git a/src/odin/printer/printer.odin b/src/odin/printer/printer.odin index 1438b34..5e1cb63 100644 --- a/src/odin/printer/printer.odin +++ b/src/odin/printer/printer.odin @@ -103,6 +103,7 @@ make_printer :: proc(config: Config, allocator := context.allocator) -> Printer } +@private build_disabled_lines_info :: proc(p: ^Printer) { found_disable := false disable_position: tokenizer.Pos @@ -131,7 +132,30 @@ build_disabled_lines_info :: proc(p: ^Printer) { } } -print :: proc(p: ^Printer, file: ^ast.File) -> string { +print :: proc { + print_file, + print_expr, +} + +print_expr :: proc(p: ^Printer, expr: ^ast.Expr) -> string { + p.document = empty(); + p.document = cons(p.document, visit_expr(p, expr)) + p.string_builder = strings.make_builder(p.allocator) + context.allocator = p.allocator + + list := make([dynamic]Tuple, p.allocator) + + append(&list, Tuple { + document = p.document, + indentation = 0, + }) + + format(p.config.max_characters, &list, &p.string_builder, p) + + return strings.to_string(p.string_builder) +} + +print_file :: proc(p: ^Printer, file: ^ast.File) -> string { p.comments = file.comments p.string_builder = strings.make_builder(p.allocator) p.src = file.src diff --git a/src/odin/printer/visit.odin b/src/odin/printer/visit.odin index f67dcee..bf79d42 100644 --- a/src/odin/printer/visit.odin +++ b/src/odin/printer/visit.odin @@ -59,11 +59,13 @@ text_token :: proc(p: ^Printer, token: tokenizer.Token) -> ^Document { return cons(document, text(token.text)) } +@(private) text_position :: proc(p: ^Printer, value: string, pos: tokenizer.Pos) -> ^Document { document, _ := visit_comments(p, pos) return cons(document, text(value)) } +@(private) newline_position :: proc(p: ^Printer, amount: int, pos: tokenizer.Pos) -> ^Document { document, _ := visit_comments(p, pos) return cons(document, newline(amount)) @@ -158,6 +160,7 @@ visit_comments :: proc(p: ^Printer, pos: tokenizer.Pos) -> (^Document, int) { return document, lines } +@(private) visit_disabled :: proc(p: ^Printer, node: ^ast.Node) -> ^Document { if node.pos.line not_in p.disabled_lines { @@ -524,6 +527,16 @@ visit_attributes :: proc(p: ^Printer, attributes: ^[dynamic]^ast.Attribute, pos: } @(private) +visit_state_flags :: proc(p: ^Printer, flags: ast.Node_State_Flags) -> ^Document { + fmt.println("called", flags) + if .No_Bounds_Check in flags { + fmt.println("IIIN") + return cons(text("#no_bounds_check"), break_with_no_newline()) + } + return empty() +} + +@(private) visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Generic, empty_block := false, block_stmt := false) -> ^Document { using ast @@ -551,9 +564,9 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener document := move_line(p, v.pos) document = cons(document, cons_with_nopl(text("using"), visit_exprs(p, v.list, {.Add_Comma}))) return document - case ^Block_Stmt: - - document := move_line(p, v.pos) + case ^Block_Stmt: + document := move_line(p, v.pos) + document = cons(document, visit_state_flags(p, v.state_flags)) uses_do := v.uses_do @@ -588,6 +601,7 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener return document case ^If_Stmt: document := move_line(p, v.pos) + document = cons(document, visit_state_flags(p, v.state_flags)) if v.label != nil { document = cons(document, cons(visit_expr(p, v.label), cons(text(":"), break_with_space()))) @@ -628,6 +642,7 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener return document case ^Switch_Stmt: document := move_line(p, v.pos) + document = cons(document, visit_state_flags(p, v.state_flags)) if v.label != nil { document = cons(document, cons(visit_expr(p, v.label), cons(text(":"), break_with_space()))) @@ -668,6 +683,7 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener return document case ^Type_Switch_Stmt: document := move_line(p, v.pos) + document = cons(document, visit_state_flags(p, v.state_flags)) if v.label != nil { document = cons(document, cons(visit_expr(p, v.label), cons(text(":"), break_with_space()))) @@ -703,6 +719,7 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener return document case ^For_Stmt: document := move_line(p, v.pos) + document = cons(document, visit_state_flags(p, v.state_flags)) if v.label != nil { document = cons(document, cons(visit_expr(p, v.label), cons(text(":"), break_with_space()))) @@ -739,6 +756,7 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener return document case ^Inline_Range_Stmt: document := move_line(p, v.pos) + document = cons(document, visit_state_flags(p, v.state_flags)) if v.label != nil { document = cons(document, cons(visit_expr(p, v.label), cons(text(":"), break_with_space()))) @@ -764,6 +782,7 @@ visit_stmt :: proc(p: ^Printer, stmt: ^ast.Stmt, block_type: Block_Type = .Gener return document case ^Range_Stmt: document := move_line(p, v.pos) + document = cons(document, visit_state_flags(p, v.state_flags)) if v.label != nil { document = cons(document, cons(visit_expr(p, v.label), cons(text(":"), break_with_space()))) @@ -1256,6 +1275,7 @@ visit_expr :: proc(p: ^Printer, expr: ^ast.Expr, called_from: Expr_Called_Type = return empty() } +@(private) visit_begin_brace :: proc(p: ^Printer, begin: tokenizer.Pos, type: Block_Type, count := 0, same_line_spaces_before := 1) -> ^Document { set_source_position(p, begin) @@ -1272,6 +1292,7 @@ visit_begin_brace :: proc(p: ^Printer, begin: tokenizer.Pos, type: Block_Type, c } } +@(private) visit_end_brace :: proc(p: ^Printer, end: tokenizer.Pos, limit := 0) -> ^Document { if limit == 0 { return cons(move_line(p, end), text("}")) @@ -1285,6 +1306,7 @@ visit_end_brace :: proc(p: ^Printer, end: tokenizer.Pos, limit := 0) -> ^Documen } } +@(private) visit_block_stmts :: proc(p: ^Printer, stmts: []^ast.Stmt, split := false) -> ^Document { document := empty() @@ -1310,6 +1332,7 @@ List_Option :: enum u8 { List_Options :: distinct bit_set[List_Option] +@(private) visit_field_list :: proc(p: ^Printer, list: ^ast.Field_List, options := List_Options{}) -> ^Document { document := empty() if list.list == nil { @@ -1385,6 +1408,7 @@ visit_field_list :: proc(p: ^Printer, list: ^ast.Field_List, options := List_Opt return document } +@(private) visit_proc_type :: proc(p: ^Printer, proc_type: ast.Proc_Type) -> ^Document { document := text("proc") @@ -1404,7 +1428,7 @@ visit_proc_type :: proc(p: ^Printer, proc_type: ast.Proc_Type) -> ^Document { document = cons(document, nest(p.indentation_count, cons(break_with(""), visit_signature_list(p, proc_type.params, false)))) document = group(cons(document, cons(break_with(""), text(")")))) - if proc_type.results != nil { + if proc_type.results != nil && len(proc_type.results.list) > 0 { document = cons_with_nopl(document, text("-")) document = cons(document, text(">")) @@ -1438,6 +1462,7 @@ visit_proc_type :: proc(p: ^Printer, proc_type: ast.Proc_Type) -> ^Document { return document } +@(private) visit_binary_expr :: proc(p: ^Printer, binary: ast.Binary_Expr) -> ^Document { lhs: ^Document rhs: ^Document @@ -1459,6 +1484,7 @@ visit_binary_expr :: proc(p: ^Printer, binary: ast.Binary_Expr) -> ^Document { return cons_with_nopl(lhs, cons_with_opl(op, rhs)) } +@(private) visit_call_exprs :: proc(p: ^Printer, list: []^ast.Expr, ellipsis := false) -> ^Document { document := empty() @@ -1479,6 +1505,7 @@ visit_call_exprs :: proc(p: ^Printer, list: []^ast.Expr, ellipsis := false) -> ^ return document } +@(private) visit_field_flag :: proc(p: ^Printer, flags: ast.Field_Flags) -> ^Document { /* @@ -1517,58 +1544,65 @@ visit_field_flag :: proc(p: ^Printer, flags: ast.Field_Flags) -> ^Document { return document } +@(private) visit_signature_list :: proc(p: ^Printer, list: ^ast.Field_List, remove_blank := true) -> ^Document { document := empty() for field, i in list.list { + document = cons(document, visit_field(p, field, remove_blank)) + + if i != len(list.list) - 1 { + document = cons(document, cons(text(","), break_with_space())) + } else { + document = cons(document, if_break(",")) + } + } + + return document +} - flag := visit_field_flag(p, field.flags) +@(private) +visit_field :: proc(p: ^Printer, field: ^ast.Field, remove_blank := true) -> ^Document { + document := empty() + flag := visit_field_flag(p, field.flags) - named := false + named := false - for name in field.names { - if ident, ok := name.derived.(^ast.Ident); ok { - //for some reason the parser uses _ to mean empty - if ident.name != "_" || !remove_blank { - named = true - } - } else { - //alternative is poly names + for name in field.names { + if ident, ok := name.derived.(^ast.Ident); ok { + //for some reason the parser uses _ to mean empty + if ident.name != "_" || !remove_blank { named = true } + } else { + //alternative is poly names + named = true } + } - if named { - document = cons(document, cons_with_nopl(flag, visit_exprs(p, field.names, {.Add_Comma}))) + if named { + document = cons(document, cons_with_nopl(flag, visit_exprs(p, field.names, {.Add_Comma}))) - if len(field.names) != 0 && field.type != nil { - document = cons(document, cons(text(":"), break_with_no_newline())) - } + if len(field.names) != 0 && field.type != nil { + document = cons(document, cons(text(":"), break_with_no_newline())) } - - if field.type != nil && field.default_value != nil { - document = cons(document, visit_expr(p, field.type)) - document = cons_with_nopl(document, text("=")) - document = cons_with_nopl(document, visit_expr(p, field.default_value)) - } else if field.type != nil { - document = cons(document, visit_expr(p, field.type)) - } else { - document = cons_with_nopl(document, text(":")) - document = cons(document, text("=")) - document = cons_with_nopl(document, visit_expr(p, field.default_value)) - } - - if i != len(list.list) - 1 { - document = cons(document, cons(text(","), break_with_space())) - } else { - document = cons(document, if_break(",")) - } } - + if field.type != nil && field.default_value != nil { + document = cons(document, visit_expr(p, field.type)) + document = cons_with_nopl(document, text("=")) + document = cons_with_nopl(document, visit_expr(p, field.default_value)) + } else if field.type != nil { + document = cons(document, visit_expr(p, field.type)) + } else { + document = cons_with_nopl(document, text(":")) + document = cons(document, text("=")) + document = cons_with_nopl(document, visit_expr(p, field.default_value)) + } return document } +@(private) repeat_space :: proc(amount: int) -> ^Document { document := empty() for i := 0; i < amount; i += 1 { @@ -1577,6 +1611,7 @@ repeat_space :: proc(amount: int) -> ^Document { return document } +@(private) get_node_length :: proc(node: ^ast.Node) -> int { #partial switch v in node.derived { case ^ast.Ident: @@ -1592,6 +1627,7 @@ get_node_length :: proc(node: ^ast.Node) -> int { } } +@(private) get_possible_field_alignment :: proc(p: ^Printer, fields: []^ast.Field) -> int { longest_name := 0 @@ -1611,6 +1647,7 @@ get_possible_field_alignment :: proc(p: ^Printer, fields: []^ast.Field) -> int { return longest_name } +@(private) get_possible_comp_lit_alignment :: proc(p: ^Printer, exprs: []^ast.Expr) -> int { longest_name := 0 @@ -1631,6 +1668,7 @@ get_possible_comp_lit_alignment :: proc(p: ^Printer, exprs: []^ast.Expr) -> int return longest_name } +@(private) get_possible_enum_alignment :: proc(p: ^Printer, exprs: []^ast.Expr) -> int { longest_name := 0 |