From eaf9567a3633ccb2feabc18d9bf423381db9d7cf Mon Sep 17 00:00:00 2001 From: Your Name Date: Sat, 7 Nov 2026 23:46:12 +0100 Subject: [PATCH] Dino compiler: language, formatter, language server, docs, editor support Compiler in Rust (lexer, parser, checker, C codegen), maps, chars, dino fmt, dino lsp (diagnostics, formatting, outline, definition, hover, completion, signature help), docs, examples, VS Code/Vim files. Co-Authored-By: Claude Sonnet 5.5 --- .gitignore | 1 + Cargo.lock | 7 + Cargo.toml | 7 + SPEC.md | 213 + dino-logo.svg | 141 + dino.svg | 55 + docs/01-first-steps.md | 100 + docs/02-values-and-types.md | 207 + docs/03-control-flow.md | 140 + docs/04-functions.md | 132 + docs/05-lists-and-maps.md | 196 + docs/06-structs-and-enums.md | 139 + docs/07-errors-and-nil.md | 123 + docs/08-memory.md | 140 + docs/09-generics-and-interfaces.md | 184 + docs/10-modules-and-stdlib.md | 123 + docs/11-cheatsheet.md | 127 + docs/12-calculator-walkthrough.md | 99 + docs/README.md | 50 + editors/README.md | 111 + editors/vim/ftdetect/dino.vim | 1 + editors/vim/syntax/dino.vim | 37 + editors/vscode/extension.js | 26 + editors/vscode/language-configuration.json | 19 + editors/vscode/package.json | 47 + editors/vscode/syntaxes/dino.tmLanguage.json | 103 + examples/01_hello_tool.dino | 29 + examples/02_game_loop.dino | 33 + examples/03_shared_app.dino | 32 + examples/04_types.dino | 23 + examples/basics/chars.dino | 37 + examples/basics/chars.out | 9 + examples/basics/closures.dino | 74 + examples/basics/closures.out | 13 + examples/basics/enum_data.dino | 101 + examples/basics/enum_data.out | 7 + examples/basics/enums.dino | 93 + examples/basics/enums.out | 14 + examples/basics/errors.dino | 48 + examples/basics/errors.out | 4 + examples/basics/fib.dino | 12 + examples/basics/fib.out | 15 + examples/basics/generic_types.dino | 53 + examples/basics/generic_types.out | 5 + examples/basics/hello.dino | 3 + examples/basics/hello.out | 1 + examples/basics/interface_values.dino | 79 + examples/basics/interface_values.out | 11 + examples/basics/interfaces.dino | 72 + examples/basics/interfaces.out | 6 + examples/basics/list_values.dino | 68 + examples/basics/list_values.out | 13 + examples/basics/lists.dino | 60 + examples/basics/lists.out | 13 + examples/basics/loops.dino | 51 + examples/basics/loops.out | 20 + examples/basics/maps.dino | 48 + examples/basics/maps.out | 16 + examples/basics/numbers.dino | 23 + examples/basics/numbers.out | 7 + examples/basics/optionals.dino | 53 + examples/basics/optionals.out | 8 + examples/basics/order.dino | 33 + examples/basics/order.out | 13 + examples/basics/own_flows.dino | 101 + examples/basics/own_flows.out | 7 + examples/basics/own_strings.dino | 101 + examples/basics/own_strings.out | 16 + examples/basics/own_structs.dino | 82 + examples/basics/own_structs.out | 14 + examples/basics/shared.dino | 69 + examples/basics/shared.out | 10 + examples/basics/sieve.dino | 45 + examples/basics/sieve.out | 3 + examples/basics/sized.dino | 50 + examples/basics/sized.out | 12 + examples/basics/stdlib.dino | 50 + examples/basics/stdlib.out | 21 + examples/basics/strings.dino | 19 + examples/basics/strings.out | 7 + examples/basics/structs.dino | 80 + examples/basics/structs.out | 9 + examples/basics/value_semantics.dino | 49 + examples/basics/value_semantics.out | 6 + examples/calc/ast.dino | 15 + examples/calc/eval.dino | 83 + examples/calc/main.dino | 43 + examples/calc/parser.dino | 130 + examples/calc/session.out | 30 + examples/calc/session.txt | 15 + examples/calc/token.dino | 49 + examples/modules/geometry.dino | 13 + examples/modules/main.dino | 6 + examples/modules/value_semantics.out | 6 + src/ast.rs | 273 ++ src/check.rs | 3823 ++++++++++++++++++ src/codegen_c.rs | 2650 ++++++++++++ src/driver.rs | 110 + src/error.rs | 50 + src/fmt.rs | 457 +++ src/hir.rs | 534 +++ src/index.rs | 398 ++ src/json.rs | 264 ++ src/lexer.rs | 591 +++ src/lib.rs | 18 + src/loader.rs | 68 + src/lsp.rs | 755 ++++ src/main.rs | 174 + src/moves.rs | 156 + src/parser.rs | 971 +++++ src/runtime.c | 610 +++ src/stdlib.rs | 153 + src/types.rs | 207 + tests/fmt.rs | 106 + tests/lsp.rs | 227 ++ tests/parse.rs | 256 ++ tests/run.rs | 2250 +++++++++++ 117 files changed, 19970 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 SPEC.md create mode 100644 dino-logo.svg create mode 100644 dino.svg create mode 100644 docs/01-first-steps.md create mode 100644 docs/02-values-and-types.md create mode 100644 docs/03-control-flow.md create mode 100644 docs/04-functions.md create mode 100644 docs/05-lists-and-maps.md create mode 100644 docs/06-structs-and-enums.md create mode 100644 docs/07-errors-and-nil.md create mode 100644 docs/08-memory.md create mode 100644 docs/09-generics-and-interfaces.md create mode 100644 docs/10-modules-and-stdlib.md create mode 100644 docs/11-cheatsheet.md create mode 100644 docs/12-calculator-walkthrough.md create mode 100644 docs/README.md create mode 100644 editors/README.md create mode 100644 editors/vim/ftdetect/dino.vim create mode 100644 editors/vim/syntax/dino.vim create mode 100644 editors/vscode/extension.js create mode 100644 editors/vscode/language-configuration.json create mode 100644 editors/vscode/package.json create mode 100644 editors/vscode/syntaxes/dino.tmLanguage.json create mode 100644 examples/01_hello_tool.dino create mode 100644 examples/02_game_loop.dino create mode 100644 examples/03_shared_app.dino create mode 100644 examples/04_types.dino create mode 100644 examples/basics/chars.dino create mode 100644 examples/basics/chars.out create mode 100644 examples/basics/closures.dino create mode 100644 examples/basics/closures.out create mode 100644 examples/basics/enum_data.dino create mode 100644 examples/basics/enum_data.out create mode 100644 examples/basics/enums.dino create mode 100644 examples/basics/enums.out create mode 100644 examples/basics/errors.dino create mode 100644 examples/basics/errors.out create mode 100644 examples/basics/fib.dino create mode 100644 examples/basics/fib.out create mode 100644 examples/basics/generic_types.dino create mode 100644 examples/basics/generic_types.out create mode 100644 examples/basics/hello.dino create mode 100644 examples/basics/hello.out create mode 100644 examples/basics/interface_values.dino create mode 100644 examples/basics/interface_values.out create mode 100644 examples/basics/interfaces.dino create mode 100644 examples/basics/interfaces.out create mode 100644 examples/basics/list_values.dino create mode 100644 examples/basics/list_values.out create mode 100644 examples/basics/lists.dino create mode 100644 examples/basics/lists.out create mode 100644 examples/basics/loops.dino create mode 100644 examples/basics/loops.out create mode 100644 examples/basics/maps.dino create mode 100644 examples/basics/maps.out create mode 100644 examples/basics/numbers.dino create mode 100644 examples/basics/numbers.out create mode 100644 examples/basics/optionals.dino create mode 100644 examples/basics/optionals.out create mode 100644 examples/basics/order.dino create mode 100644 examples/basics/order.out create mode 100644 examples/basics/own_flows.dino create mode 100644 examples/basics/own_flows.out create mode 100644 examples/basics/own_strings.dino create mode 100644 examples/basics/own_strings.out create mode 100644 examples/basics/own_structs.dino create mode 100644 examples/basics/own_structs.out create mode 100644 examples/basics/shared.dino create mode 100644 examples/basics/shared.out create mode 100644 examples/basics/sieve.dino create mode 100644 examples/basics/sieve.out create mode 100644 examples/basics/sized.dino create mode 100644 examples/basics/sized.out create mode 100644 examples/basics/stdlib.dino create mode 100644 examples/basics/stdlib.out create mode 100644 examples/basics/strings.dino create mode 100644 examples/basics/strings.out create mode 100644 examples/basics/structs.dino create mode 100644 examples/basics/structs.out create mode 100644 examples/basics/value_semantics.dino create mode 100644 examples/basics/value_semantics.out create mode 100644 examples/calc/ast.dino create mode 100644 examples/calc/eval.dino create mode 100644 examples/calc/main.dino create mode 100644 examples/calc/parser.dino create mode 100644 examples/calc/session.out create mode 100644 examples/calc/session.txt create mode 100644 examples/calc/token.dino create mode 100644 examples/modules/geometry.dino create mode 100644 examples/modules/main.dino create mode 100644 examples/modules/value_semantics.out create mode 100644 src/ast.rs create mode 100644 src/check.rs create mode 100644 src/codegen_c.rs create mode 100644 src/driver.rs create mode 100644 src/error.rs create mode 100644 src/fmt.rs create mode 100644 src/hir.rs create mode 100644 src/index.rs create mode 100644 src/json.rs create mode 100644 src/lexer.rs create mode 100644 src/lib.rs create mode 100644 src/loader.rs create mode 100644 src/lsp.rs create mode 100644 src/main.rs create mode 100644 src/moves.rs create mode 100644 src/parser.rs create mode 100644 src/runtime.c create mode 100644 src/stdlib.rs create mode 100644 src/types.rs create mode 100644 tests/fmt.rs create mode 100644 tests/lsp.rs create mode 100644 tests/parse.rs create mode 100644 tests/run.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2f7896d --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +target/ diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..adca287 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "dino" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..a6045ab --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "dino" +version = "0.1.0" +edition = "2021" +description = "The Dino programming language compiler" + +[dependencies] diff --git a/SPEC.md b/SPEC.md new file mode 100644 index 0000000..2f0b8c6 --- /dev/null +++ b/SPEC.md @@ -0,0 +1,213 @@ +# Dino language spec (draft) + +Goal: Go-like simplicity, C++/Rust speed, no GC, no low-level syntax. +Targets: game dev, CLI tools, desktop apps. + +## Decisions +- **Methods**: declared inside the struct body. Fields are accessed directly + (`self` available for the whole value). No receiver, no `mut` keyword: + the compiler infers whether a method mutates. +- **Variables**: `var x = 1` creates a changeable variable (type inferred; + `var x: int = 1` with a type). `x = 2` changes it. `const X = 1` is fixed. +- **Types are written after a colon**, everywhere: `var x: int = 1`, + `func area(w: float, h: float): float`, `struct User { name: string, age: int }`, + enum data `Circle(r: float)`, generic bounds `[T: Ordered]`, several results `func f(): (int, error)`. + Names in a row share a type: `(a, b: int)`, `x, y: float`. Without a colon + the compiler explains what to add. +- **Sized numbers**: `i8 i16 i32 u8 u16 u32 u64 f32`; `i64` is `int` and `f64` is + `float` (same type). A number literal takes the type asked for (`var x: u8 = 200`, + `b + 1`, `[1, 2]` as `[]u8`) and a literal that doesn't fit is an error. Different + number types never mix: convert with `u8(x)`, `f32(x)`, `int(x)`, ... Integers wrap + around on overflow; float to integer stops at the nearest end (NaN is 0). Dividing by + zero panics. Unsigned values can't be negated. `f32` prints the shortest text that + reads back the same. +- **Sharing**: `shared T` (reference counted), `weak T` (non-owning). + Everything else is plain ownership, inferred by the compiler. +- **Errors**: Go-style `(value, error)` returns, with `?` to propagate. +- **Missing values**: `T?` with `nil`. Compiler requires a nil check before use. +- **Strings**: interpolation with `"Hello {name}"`; literal brace is `\{`. +- **Visibility**: private by default, `pub` makes it public. +- **Loops**: `for` only: `for i in 0..10`, `for x in items`, `for cond { }`, + `for { }` (infinite), with `step` for strides. No C-style three-part loop. +- **Numbers**: `int` (64-bit) and `float` (64-bit) for everyday code; sized + types `i8 i16 i32 i64 u8 u16 u32 u64 f32 f64` when exact control is needed. + `var a = 42` is `int`, `var b = 3.14` is `float`. +- **print**: `print(a, b, c)` writes values separated by spaces, then a newline. +- **Conversions**: no implicit int/float mixing, except that an integer + literal fits a float (`var x: float = 1`, `x * 2`). Use `int(x)` / `float(x)`. +- **Integer overflow** wraps around; `/` and `%` by zero panic (exit code 2). +- **Evaluation order** is left to right, always (arguments, `&&`/`||` short-circuit). +- **Top-level `const`** may not call functions; uses are replaced by the value. +- **Structs are values**: `var b = a` copies; passing to a function copies. + Methods go inside the struct and see fields directly; `self` is the whole value. + Inside a method, a name resolves as: local/parameter, then field, then method + of the same struct (call it bare: `update(dt)`), then global function/const. +- **Mutation is inferred**: a method that assigns to a field (or calls a mutating + method on `self`) is mutating. Mutating methods can't be called on a `const` + or on a temporary like `make().update()`. +- **Struct literals**: positional `Vec2{1, 2}` needs every field; named + `Player{name: "A"}` may leave fields out (they start at zero: 0, 0.0, false, ""). + Initializers run in the order written. +- **Printing a struct** shows `Vec2{x: 1.0, y: 2.0}`. `==` on structs is not + allowed yet; compare fields. +- **Lists** `[]T`: `[1, 2, 3]`, `var xs: []int` (empty), `xs[i]`, `xs[i] = v`, + `xs.len()`, `xs.push(v)`, `xs.pop()`, `xs[1..3]` (a copy), `for x in xs`, + `for i, x in xs`. Lists are values: `var b = a` and passing to a function + copy. A function that never changes a list parameter gets it without a copy + (the compiler works this out); returning a local list moves it. + Looping walks the list as it was when the loop started. `print` shows `[1, 2]`. + Out-of-range index/slice and `pop()` on an empty list panic (exit code 2). + `s.len()` on a string is its length in bytes. +- **Enums** carry data per case: `enum Shape { Circle(r float) Rect(w, h float) Point }`, + with methods inside, like structs. Build a value with the qualified name: + `Shape.Circle(2.0)`, `Shape.Point`. Enums are values (copied like structs); a + case may hold lists. The zero value is the first case. Printing shows + `Shape.Circle(2.0)`. `==` works on enums whose cases carry no data. + An enum can't contain itself directly, but may hold a list of itself. +- **match** is exhaustive: missing cases are a compile error (the message lists + them); `_` covers the rest. Works on enums, int, bool, string. + Patterns: `Circle(r)`, `Rect(w, _)`, `Point`, `Shape.Point`, literals, `_`, or a + lowercase name that takes the whole value. A capitalized name that isn't a case + is an error (never a silent catch-all). Nested patterns come later. + As a statement, arms may be blocks (and may `break`/`continue`/`return`). + As a value (`var x = match ...`, `return match ...`), every arm is a single + expression, or a lone `return`/`break`/`continue`. Arms of a value `match` must + have the same type. +- **Errors**: `error` is a built-in type: nil (no error) or a message. Make one with + `error("msg {x}")`; print it or use it in `"{err}"` (nil prints `nil`). + A function may return several values: `func f(): (int, error)`; take them with + `var n, err = f()` (`_` throws one away). `return v, nil` or the shorthand + `return error("x")` (zero values are filled in). The error ignored by a bare + `f()` statement is a compile error. + `call()?` passes the error up: on error the enclosing function returns it (zero + values for the rest), otherwise `?` gives the value. Needs a function whose last + result is `error`; works on `(T, error)` and `error` calls. + `func main(): error` is allowed: an error prints `error: msg` to stderr, exit code 1. +- **Optionals**: `T?` holds a `T` or `nil`. A `T` converts to `T?` implicitly. + Compare with `x == nil` / `x != nil`. To use the value, check first: the compiler + knows `x` is set inside `if x != nil { }`, after `if x == nil { return }` (or + break/continue), and on the right of `x != nil &&` / `x == nil ||`. Assigning to + `x` forgets that. (Reads are also checked at runtime, so it can never go wrong.) + Not yet: `T?` as a struct field / enum data / list element, `??`, `x!`, `match` on nil, + `?` on optionals. +- **Last use moves**: `var b = a` copies `a`, unless `a` is never used again (and no loop + is in the way): then `b` takes the value and nothing is copied (`src/moves.rs`). +- **Generic functions**: `func max[T: Ordered](a, b: T): T`. The types are worked out from the + arguments (`max(3, 9)`, `first(["a"])`; bare number literals become `int`, or `float` if any is + a float); there is no `max[int](...)` yet. Each set of types gets its own compiled copy, so + generics cost nothing at runtime. Rules after the colon: `Ordered` (numbers and strings) and + `Number`; no rule means anything goes, and a mistake then shows up when the copy is checked + (the message names the types). A generic function that is never used is not checked. + A generic function needs a parameter that mentions its type. +- **Interfaces** are rules for generics, Go-style: no `implements`. A type follows an + interface just by having its methods with the same parameters and result: + `interface Shape { area(): float name(): string }`, then `func show[T: Shape](s: T)` + (also `struct Gallery[T: Shape]`, `[T: m.Shape]` from another file). A type that doesn't + gets a message naming the missing or different method. Generic code may call the methods + the interface lists even if they are private in another file. +- **Interface values**: an interface is also a type: `var s: Shape = Circle{2.0}`, `[]Shape`, + `func f(s: Shape)`, struct fields, results. A struct or enum value with the right methods + converts automatically (the value is copied into the interface value), and `s.area()` calls + the method of whatever is inside. Interface values are values like any other: `var t = s` + copies the inside, and a method that changes the value (`s.scale(2.0)`) changes only that + variable's copy. `nil` is the empty interface value (`s == nil`); calling a method on it + panics (exit code 2). A list can mix types: `var xs: []Shape = [Circle{1.0}, Rect{2.0, 3.0}]`. + Printing shows the value inside. Not yet: turning an interface value back into its type, + one interface value used as another interface, interfaces with type parameters. + Interfaces can't take type parameters yet. +- **Generic structs and enums**: `struct Stack[T] { items: []T func push_item(x: T) { items.push(x) } }`, + `enum Maybe[T] { Some(value: T) None }`. Use them with the types written out: + `Stack[int]{}`, `var s: Stack[string]`, `Pair[string, float]{"pi", 3.14}`, + `Maybe[int].Some(3)`, `func f(): Maybe[int]`, and from another file `m.Stack[int]{}`. + The types are never guessed (`Stack{}` is an error). Like functions, each list of types + becomes its own type (`Stack[int]` prints with its types), rules `[T: Number]` work, and + they may contain themselves through lists (`kids: []Tree[T]`). A capitalized name followed + by `[types]` and then `{` or `.` is read as a generic type, anything else as indexing. + A generic type that is never used is not checked. +- **Shared values**: `var tex = shared Texture{...}` makes one value with a counter. `var b = a`, + passing it, putting it in a list or a struct field all give another owner (no copy of the value); + the value is freed when the last owner goes. Use it like the thing inside: `tex.name`, + `tex.touch()`, `tex.uses = 1`, `match light { ... }`. A change through any owner is seen + by all. Only structs and enums can be shared for now. Written as a type: `shared Texture`. + A shared value is not copied by `var b = a`, so two owners really are one value. +- **Weak pointers**: `weak win` gives a non-owning pointer to a shared value (type `weak Window`; + `weak Window?` means the same). It doesn't keep the value alive, so a window and its buttons + can point at each other without leaking (the button's side is `weak`). `w == nil` / + `w != nil` tell whether the value is still there; using a weak pointer whose value is gone + (or that was never set) panics with a clear message (exit code 2). A weak pointer starts + as `nil` (`Button{label: "x"}`) and can be assigned `nil`. To use the value as an owner, `w.upgrade()` gives a `shared T?`: the shared + value (now with one more owner, so it stays alive while you hold it) or `nil` if it is gone. + Not yet: `==` between shared values (identity), shared lists/strings/numbers. +- **Function values and closures**: the type is `func(int, string): bool` (no `: result` means + nothing is returned). Write one as `func(x: int): int { return x + 1 }`, or short as + `x => x + 1` / `(a, b) => a + b` where the types are known from the place it is used + (a parameter, a typed variable, a return, a struct field) or, for a generic function, + from its other arguments (`map(nums, x => x * 2)`). A top-level function is a value too: + `apply(double, 4)`. Call a function value like a function: `f(3)`, `b.on_click(name)`, + `make()(3)`. A closure **copies** the variables it uses when it is made and keeps its own copies + (so it can count: `n += 1` inside changes only its copy, which persists between calls). + Copying a closure (`var g = f`, passing it) copies its state too. Calling a function value + counts as changing it, like a method that changes `self`. A function value that was never + set panics when called (exit code 2). Not yet: closures with block bodies in short form, + methods as values, several results from a function value, `==` on functions. +- **Memory**: no GC, nothing to write. Every value has exactly one owner and is + freed when its owner's block ends (also on early `return`, `break`, `continue` + and `?`). Lists, structs, enums, optionals, errors and strings all follow this. + Assigning or passing a variable copies it (`var b = a`); a fresh value (a call + result, a literal) or a returned local moves without copying. Strings are + immutable and counted, so copying one only bumps a counter; literals are never + freed. A parameter that the function doesn't change is passed by address, so + passing lists around costs nothing. Every test program runs under the address + sanitizer, so a leak, double free or use after free fails the test. + +- **Imports**: `import os` (built-in) or `import utils` (the file `utils.dino` next to the + main file; `import tools.fs` is `tools/fs.dino`, used as `fs.name`). Use things as + `utils.double(2)`, `utils.Vec2{1.0, 2.0}`, `utils.Shape.Circle(1.0)`, `utils.SCALE`, + and `utils.Vec2` in types. Only `pub` functions, consts, types, methods and fields can be used + from another file (fields are private too unless written `pub x: int`). Each file has its own imports and its + own names, so two files may both define `f`. Import cycles and missing files are errors; + errors point at the file they are in. Names of module types print with the module + (`utils.Vec2{...}`). Importing a C library (`import c "x.h"`) is not supported yet. +- **Built-in modules**: `os` (`read_file write_file append_file exists exit env + read_line`, and the value `args`), `math` (`sqrt pow floor ceil round trunc sin cos tan asin acos atan atan2 exp log log10 hypot min max abs PI E`), + `time` (`now sleep`). Text has methods: `upper lower trim split lines fields replace + repeat contains starts_with ends_with index_of to_int to_float`; `[]string` has `join`. + +- **Maps** `map[K, V]`: `{"a": 1}` (types guessed), `map[string, int]{}` or `var m: map[string, int] = {}` (empty). + Keys: int/whole-number types, string, bool. Values: anything except `T?`. `m[k] = v` adds or replaces, + `m[k]` panics if the key is missing, `m[k] += 1` treats a missing key as zero, methods `has get get_or + remove keys values len`, `for k, v in m` / `for k in m`. Entries keep insertion order (hash index + + ordered arrays; `remove` is O(n)). Maps are values (`var b = a` copies). Not yet: `==`, generic `map[K,V]` params. +- **Docs**: `docs/` holds learner notes in markdown; a test compiles and runs every example in them. +- **Characters** `char`: one Unicode character, `'a'` `'\n'` `'é'` (escapes `\n \t \r \0 \\ \' \"`). Compare with + `== != < > <= >=`, `match` on them (`'+' => ...`), use as map keys; no arithmetic (`int(c)`, `char(n)`). + `string(x)` (or `"{c}"`) makes text. `for c in s` / `for i, c in s` walk characters (`i` = byte position, + invalid UTF-8 comes out as U+FFFD). `s[i]` is the character starting at byte `i` (panics out of range or + in the middle of a character); strings can't be assigned to. Methods `is_digit is_letter is_alnum is_space + is_upper is_lower upper lower` (ASCII plus Latin-1 letters). Not yet: `s.chars()`, `[]char`→string. +- **Formatter** `dino fmt [--check] ..` (`src/fmt.rs`): text based, so comments and blank lines survive. + 4-space indent, spaces around binary operators and after `,` `:`, tight `Point{...}` / map literals and `[]T`, + at most one blank line, trailing `//` comments of neighbouring lines aligned. It never moves line breaks (a one-line + block stays one line), indents continuation lines (after an operator, or inside open brackets) one level, and + verifies that the token stream is unchanged before writing. `examples/` must stay formatted (a test checks). +- **Editor support** (`editors/`): `dino lsp` is a language server (`src/lsp.rs`, own JSON in `src/json.rs`, no + dependencies): full-text sync, error diagnostics from the real checker (imports read from disk or from open + buffers; errors in other files shown at the top of the importing file; UTF-16 columns), whole-file formatting. + `editors/vscode` (extension + TextMate grammar), `editors/vim` (syntax), `editors/README.md` (Neovim, Helix, others). + Not yet: hover, go to definition, completion, rename; the compiler reports only the first error. +- **String slicing**: `s[a..b]`, `s[a..]`, `s[..b]` (byte positions, bounds checked; panics with "the length is N"). +- **Running**: `dino run file.dino a b c` passes `a b c` to the program (`os.args`). +- **Example**: `examples/calc/` is a multi-file calculator (tokenizer, parser, evaluator, shared scope); `session.txt` / `session.out` are checked by a test. + +## Open questions +- Braces vs indentation (currently braces, no semicolons) +- Concurrency details + +## Status +Working: functions, int/float/bool/string, var/const, if/else, for (infinite, +while, range with step, over lists), break/continue, string interpolation, print, +structs with methods (value semantics, inferred mutation), lists (value +semantics, bounds checked), enums with data + exhaustive match, errors (`error`, +multi-value returns, `?`), optionals (`T?`, `nil`, narrowing), `dino run`. +Not yet: nested match patterns, `==` on lists/structs, top-level const lists. These give a clear "not supported yet" error. +Known gaps: memory is only freed on normal exits (a panic just ends the program). diff --git a/dino-logo.svg b/dino-logo.svg new file mode 100644 index 0000000..e324fc4 --- /dev/null +++ b/dino-logo.svg @@ -0,0 +1,141 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/dino.svg b/dino.svg new file mode 100644 index 0000000..8d542b0 --- /dev/null +++ b/dino.svg @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/docs/01-first-steps.md b/docs/01-first-steps.md new file mode 100644 index 0000000..39a0ad5 --- /dev/null +++ b/docs/01-first-steps.md @@ -0,0 +1,100 @@ +# 1. First steps + +## Running a program + +Save this as `hello.dino`: + +```dino +func main() { + print("Hello, Dino!") +} +``` +```text +Hello, Dino! +``` + +Every program starts at `func main()`. Run it from the project folder: + +```text +cargo run -q -- run hello.dino +``` + +The compiler turns your Dino code into C and builds it with your C compiler (`cc`, so you +need gcc or clang installed). If you build the `dino` binary once, the commands are: + +| Command | What it does | +|---------|--------------| +| `dino run file.dino [args...]` | Compile and run. Extra words are given to the program (`os.args`). | +| `dino build file.dino -o out` | Make an executable you can keep. | +| `dino check file.dino` | Look for errors without building. | +| `dino emit-c file.dino` | Print the generated C. | +| `dino fmt file-or-folder...` | Tidy the code in place (`--check` only reports). | + +## print + +`print` takes any number of values, separates them with spaces, and ends the line. +It can print numbers, strings, lists, maps, structs, enums and more. + +```dino +func main() { + print("score:", 42, true) + print([1, 2, 3]) + print(1.5 + 1.5) +} +``` +```text +score: 42 true +[1, 2, 3] +3.0 +``` + +Notice that a float always shows a `.0` so you can tell `3.0` from `3`. + +## Comments + +```dino +// a comment runs to the end of the line +func main() { + print("hi") // so does this +} +``` +```text +hi +``` + +## Reading the error messages + +Dino tries hard to explain mistakes. For example, forgetting that `int` and `float` +don't mix: + +```text +error: can't use `int` and `float` together (convert with `float(x)`) +``` + +When a message offers a hint, try it. If it doesn't make sense, that is a bug in the +message and worth fixing. + +## Your editor + +`editors/` has colours, live error underlines and format-on-save for VS Code, Neovim, Vim and +Helix (any editor that can run a language server works). See `editors/README.md`. It is the +compiler itself doing the checking, so you see the same messages as in the terminal. The +server is started with `dino lsp`. + +## Formatting your code + +`dino fmt` rewrites your files in one standard style, so you never argue about spaces: + +```text +dino fmt hello.dino # one file +dino fmt src/ # every .dino file in a folder +dino fmt --check src/ # change nothing; exit code 1 if something is untidy +``` + +It uses 4-space indents, one space around operators, `a: int` colons, `Point{x: 1}` literals, +and at most one blank line in a row. It keeps your comments and **never moves a line break**, +so how you split things across lines is up to you. Trailing `//` comments on neighbouring +lines are lined up. Before saving, it checks that the code means exactly the same thing, and +it refuses files that don't parse. + +Next: [Values and types](02-values-and-types.md) diff --git a/docs/02-values-and-types.md b/docs/02-values-and-types.md new file mode 100644 index 0000000..c84aeb0 --- /dev/null +++ b/docs/02-values-and-types.md @@ -0,0 +1,207 @@ +# 2. Values and types + +## Variables + +`var` makes a variable you can change. The type is worked out from the value. + +```dino +func main() { + var score = 10 + score = score + 5 + score += 1 + print(score) + + var name: string = "Dino" // you may write the type after a colon + var ratio: float = 1 // a whole-number literal fits a float + print(name, ratio) +} +``` +```text +16 +Dino 1.0 +``` + +`const` is a value that never changes. It must be known when the program is compiled +(you can't call functions in it). + +```dino +const MAX = 100 +const GREETING = "hi" + +func main() { + print(MAX * 2, GREETING) +} +``` +```text +200 hi +``` + +> **Types go after a colon, everywhere.** `var x: int`, `func f(a: int): string`, +> `struct S { x: float }`. If you forget, the compiler tells you what to add. + +## The basic types + +| Type | Example | Notes | +|------|---------|-------| +| `int` | `42` | 64-bit whole number | +| `float` | `3.14` | 64-bit decimal number | +| `bool` | `true`, `false` | | +| `char` | `'a'` | one character (see below) | +| `string` | `"text"` | UTF-8 text, can't be changed in place | +| `error` | `error("oops")` | see [Errors and nil](07-errors-and-nil.md) | + +Sized numbers exist for when you need exact control: `i8 i16 i32 u8 u16 u32 u64 f32` +(`i64` is the same as `int`, `f64` the same as `float`). + +```dino +func main() { + var small: u8 = 200 + var wrapped = small + 100 // whole numbers wrap around: 300 - 256 + print(wrapped) + + var x = 7 + var y = 2 + print(x / y, x % y) // whole-number division + print(float(x) / float(y)) // convert before dividing for a decimal answer +} +``` +```text +44 +3 1 +3.5 +``` + +Rules worth remembering: + +- Different number types never mix. Convert explicitly: `float(x)`, `int(y)`, `u8(z)`. +- A number literal adapts to the type it is used with (`var b: u8 = 5`). +- Dividing by zero stops the program with a clear message. +- Converting a float to an integer cuts off the decimals. + +## Strings + +Put values inside text with `{ }`. Write a literal brace as `\{`. + +```dino +func main() { + var name = "Ada" + var age = 36 + print("Hello {name}, next year you are {age + 1}.") + print("tab\there, quote \" and brace \{ok}") + + var s = "Hello" + ", " + "world" + print(s.len(), s.upper(), s.contains("wor")) +} +``` +```text +Hello Ada, next year you are 37. +tab here, quote " and brace {ok} +12 HELLO, WORLD true +``` + +Handy string methods: `len upper lower trim split lines fields replace repeat contains +starts_with ends_with index_of to_int to_float`. See the [cheatsheet](11-cheatsheet.md). + +### Slicing + +`s[a..b]` is the part from byte `a` up to (not including) `b`. Leave a side out to go to the edge. + +```dino +func main() { + var s = "hello world" + print(s[0..5]) + print(s[6..]) + print(s[..5]) +} +``` +```text +hello +world +hello +``` + +A slice outside the string stops the program with a message that shows the length. +`s.len()` counts bytes, so a letter like `é` counts as 2. + +### Characters: `char` + +A `char` is **one character**, written with single quotes. Double quotes are for text. + +```dino +func main() { + var c = 'a' + print(c, c == 'a', c < 'b') + print('é', '\n' == '\n') // any Unicode character; escapes: \n \t \r \0 \\ \' + print(int('a'), char(66)) // a character and its number + print(string(c) + "!") // a character as text: string(c), or "{c}" +} +``` +```text +a true true +é true +97 B +a! +``` + +Going through a string gives characters: + +```dino +func main() { + var s = "héllo 42" + var letters = 0 + var digits = 0 + for c in s { + if c.is_letter() { letters += 1 } + if c.is_digit() { digits += 1 } + } + print(letters, digits) + + for i, c in "aé!" { print(i, c) } // i is the byte position + print(s[0], s[1], s[3]) // s[i] is the character that starts at byte i +} +``` +```text +5 2 +0 a +1 é +3 ! +h é l +``` + +- `s.chars()` gives all the characters as a list (`[]char`), so you can index it by + character number: `var cs = "héllo".chars()` then `cs[1]` is `é` and `cs.len()` is 5. +- `for c in s` walks whole characters, even multi-byte ones like `é` or `😀`. +- `s[i]` takes a **byte** position. If the position is in the middle of a character, or + outside the string, the program stops with a message. For plain English text, position + and character number are the same. +- Strings can't be changed in place, so `s[0] = 'x'` is an error. Build a new string: + `up += string(c.upper())`. +- Methods: `is_digit is_letter is_alnum is_space is_upper is_lower upper lower`. + (`upper`/`lower` change plain English letters.) +- `char` can be compared (`== != < > <= >=`), matched with `match`, and used as a map key. + It does no arithmetic: use `int(c)` and `char(n)` if you need that. + +```dino +func kind(c: char): string { + return match c { + '+' => "plus" + ' ' => "space" + _ => "other" + } +} + +func main() { + var counts: map[char, int] = {} + for c in "banana" { counts[c] += 1 } + print(counts, kind('+'), kind('x')) +} +``` +```text +{b: 1, a: 3, n: 2} plus other +``` + +### Comparing + +`==`, `!=`, `<`, `>` work on strings (alphabetical order). + +Next: [Control flow](03-control-flow.md) diff --git a/docs/03-control-flow.md b/docs/03-control-flow.md new file mode 100644 index 0000000..5e95b3c --- /dev/null +++ b/docs/03-control-flow.md @@ -0,0 +1,140 @@ +# 3. Control flow + +## if / else + +No parentheses around the condition. The braces are required. The condition must be a `bool`. + +```dino +func main() { + var temp = 21 + if temp > 30 { + print("hot") + } else if temp > 15 { + print("nice") + } else { + print("cold") + } +} +``` +```text +nice +``` + +Combine conditions with `&&`, `||` and `!`. The right side only runs when needed. + +## for: the only loop + +There is one keyword for every kind of loop. + +```dino +func main() { + for i in 0..3 { // 0, 1, 2: the end is not included + print("i =", i) + } + for i in 0..10 step 4 { // 0, 4, 8 + print(i) + } + + var names = ["ann", "bo"] + for n in names { // each item + print(n) + } + for i, n in names { // index and item + print(i, n) + } + + var k = 0 + for k < 3 { // while a condition holds + k += 1 + } + print(k) + + for { // forever, until break + k += 1 + if k > 5 { break } + if k == 4 { continue } + } + print(k) +} +``` +```text +i = 0 +i = 1 +i = 2 +0 +4 +8 +ann +bo +0 ann +1 bo +3 +6 +``` + +`break` leaves the loop; `continue` jumps to the next round. There is no C-style +`for (i = 0; i < n; i++)`. + +Looping over a list walks it as it was when the loop started, so changing the list inside +the loop is safe. + +## match + +`match` picks a branch by shape. It must cover every possibility, otherwise the compiler +lists what is missing. `_` means "anything else". + +```dino +enum Shape { + Circle(r: float) + Rect(w, h: float) + Point +} + +func area(s: Shape): float { + return match s { + Circle(r) => 3.0 * r * r + Rect(w, h) => w * h + Point => 0.0 + } +} + +func describe(n: int): string { + return match n { + 0 => "zero" + 1 => "one" + _ => "many" + } +} + +func main() { + print(area(Shape.Circle(2.0)), area(Shape.Rect(2.0, 3.0)), area(Shape.Point)) + print(describe(0), describe(1), describe(7)) +} +``` +```text +12.0 6.0 0.0 +zero one many +``` + +`match` works on enums, ints, bools and strings. Used as a **value** (like above) each arm +is one expression. Used as a **statement** an arm can be a block: + +```dino +func main() { + var cmd = "stop" + match cmd { + "go" => print("going") + "stop" => { + print("stopping") + print("done") + } + _ => print("unknown") + } +} +``` +```text +stopping +done +``` + +Next: [Functions](04-functions.md) diff --git a/docs/04-functions.md b/docs/04-functions.md new file mode 100644 index 0000000..a10e5d6 --- /dev/null +++ b/docs/04-functions.md @@ -0,0 +1,132 @@ +# 4. Functions + +```dino +func add(a: int, b: int): int { + return a + b +} + +func greet(name: string) { // no result: nothing after the parentheses + print("hi", name) +} + +func main() { + print(add(2, 3)) + greet("Ada") +} +``` +```text +5 +hi Ada +``` + +Names in a row can share a type: `func area(w, h: float): float`. + +## Several results + +A function can return more than one value. The most common use is a value and an `error`. + +```dino +func divide(a, b: int): (int, error) { + if b == 0 { + return 0, error("cannot divide by zero") + } + return a / b, nil +} + +func main() { + var q, err = divide(7, 2) + print(q, err) + var _, err2 = divide(1, 0) + print(err2) +} +``` +```text +3 nil +cannot divide by zero +``` + +More in [Errors and nil](07-errors-and-nil.md). + +## Parameters are safe + +A parameter is a copy as far as you can tell: changing it inside never changes the caller's +variable. (When a function only *reads* a parameter, the compiler passes it without copying, +so big lists are free to pass around.) + +```dino +func grow(xs: []int): []int { + xs.push(99) + return xs +} + +func main() { + var a = [1, 2] + var b = grow(a) + print(a, b) +} +``` +```text +[1, 2] [1, 2, 99] +``` + +## Functions are values + +A function has a type like `func(int): int`. You can store it, pass it and return it. + +```dino +func double(x: int): int { return x * 2 } + +func apply(f: func(int): int, x: int): int { + return f(x) +} + +func main() { + print(apply(double, 4)) + var triple = func(x: int): int { return x * 3 } + print(apply(triple, 4)) + print(apply(x => x + 1, 4)) // short form +} +``` +```text +8 +12 +5 +``` + +### Short lambdas + +`x => x * 2` and `(a, b) => a + b` are shorthand for a one-expression function. The types +come from where it is used, so it works where Dino already knows what function is expected +(a parameter, a typed variable, a struct field, or another argument of a generic function). +The short form is one expression; for several statements use the long `func(x: int): int { }`. + +### Closures remember + +A function written inside another can use the variables around it. It **copies** them when +it is created and keeps its own copy from then on. + +```dino +func make_counter(): func(): int { + var n = 0 + return func(): int { + n += 1 + return n + } +} + +func main() { + var next = make_counter() + print(next(), next(), next()) + + var offset = 10 + var add = func(x: int): int { return x + offset } + offset = 99 // the closure keeps its own 10 + print(add(1)) +} +``` +```text +1 2 3 +11 +``` + +Next: [Lists and maps](05-lists-and-maps.md) diff --git a/docs/05-lists-and-maps.md b/docs/05-lists-and-maps.md new file mode 100644 index 0000000..51f7b62 --- /dev/null +++ b/docs/05-lists-and-maps.md @@ -0,0 +1,196 @@ +# 5. Lists and maps + +## Lists: `[]T` + +An ordered, growable sequence. + +```dino +func main() { + var nums = [10, 20, 30] + nums.push(40) + nums[0] = 11 + print(nums, nums.len(), nums[1]) + print(nums.pop(), nums) + print(nums[1..3]) // a copy of part of the list + + var names: []string // empty list: write the type + names.push("ann") + print(names) + + var total = 0 + for n in nums { total += n } + print(total) +} +``` +```text +[11, 20, 30, 40] 4 20 +40 [11, 20, 30] +[20, 30] +[ann] +61 +``` + +Facts: +- An empty list needs a type: `var xs: []int`. +- Going outside the list (`nums[9]`) or popping an empty list stops the program with a message. +- **Lists are values.** `var b = a` copies the list. Changing `b` leaves `a` alone. + +```dino +func main() { + var a = [1, 2, 3] + var b = a + b.push(4) + print(a, b) +} +``` +```text +[1, 2, 3] [1, 2, 3, 4] +``` + +## Maps: `map[K, V]` + +A map finds a value by a **key** instead of a position, like a phone book. + +```dino +func main() { + var ages = {"alice": 30, "bob": 25} + ages["carol"] = 41 // add a new entry + ages["bob"] = 26 // change an existing one + print(ages["bob"], ages.len()) + print(ages) +} +``` +```text +26 3 +{alice: 30, bob: 26, carol: 41} +``` + +### Making a map + +```dino +func main() { + var a = {"x": 1, "y": 2} // types are guessed from the entries + var b: map[string, int] = {} // empty: the type comes from the variable + var c = map[int, string]{} // empty: the type is written on the right + b["z"] = 26 + c[7] = "seven" + print(a, b, c) +} +``` +```text +{x: 1, y: 2} {z: 26} {7: seven} +``` + +An empty `{}` always needs a type from one of those two places. + +### Keys + +A key can be an `int` (or another whole-number type), a `string` or a `bool`. +The value can be anything: numbers, strings, lists, structs, even other maps. + +### Reading + +| Code | Result | +|------|--------| +| `m[k]` | the value; **stops the program** if `k` is missing | +| `m.has(k)` | `true` or `false` | +| `m.get(k)` | the value or `nil` (type `V?`, see [Errors and nil](07-errors-and-nil.md)) | +| `m.get_or(k, d)` | the value, or `d` if `k` is missing | +| `m.len()` | number of entries | +| `m.keys()` | a list of the keys | +| `m.values()` | a list of the values | + +```dino +func main() { + var stock = {"apple": 3, "pear": 0} + print(stock.has("apple"), stock.has("fig")) + print(stock.get("apple"), stock.get("fig")) + print(stock.get_or("fig", -1)) + print(stock.keys(), stock.values()) +} +``` +```text +true false +3 nil +-1 +[apple, pear] [3, 0] +``` + +### Changing + +`m[k] = v` adds or replaces. `m.remove(k)` deletes (and does nothing if the key is missing). + +```dino +func main() { + var m = {"a": 1, "b": 2} + m["c"] = 3 + m.remove("a") + print(m) +} +``` +```text +{b: 2, c: 3} +``` + +### Counting: `+=` starts from zero + +The most common map job is counting. `m[k] += 1` on a key that isn't there yet starts from +zero, so you don't have to check first. + +```dino +func main() { + var counts: map[string, int] = {} + for w in "a b a c b a".split(" ") { + counts[w] += 1 + } + print(counts) +} +``` +```text +{a: 3, b: 2, c: 1} +``` + +(Only the compound forms `+= -= *= /= %=` do this. Plain *reading* a missing key with `m[k]` still stops the program.) + +### Looping + +Entries come out **in the order they were added**, so output is always predictable. + +```dino +func main() { + var prices = {"tea": 2.5, "cake": 4.0} + for name, price in prices { + print(name, "costs", price) + } + for name in prices { // just the keys + print(name) + } +} +``` +```text +tea costs 2.5 +cake costs 4.0 +tea +cake +``` + +### Maps are values too + +`var b = a` copies the whole map. A map inside a struct is copied with the struct. + +```dino +func main() { + var a = {"x": 1} + var b = a + b["y"] = 2 + print(a.len(), b.len()) +} +``` +```text +1 2 +``` + +Looking things up is fast: it does not slow down as the map grows. (`remove` has to +re-arrange the entries to keep their order, so it is slower on huge maps.) + +Next: [Structs and enums](06-structs-and-enums.md) diff --git a/docs/06-structs-and-enums.md b/docs/06-structs-and-enums.md new file mode 100644 index 0000000..167a731 --- /dev/null +++ b/docs/06-structs-and-enums.md @@ -0,0 +1,139 @@ +# 6. Structs and enums + +## Structs + +A struct groups related values. **Methods go inside the struct** and see the fields directly. + +```dino +struct Player { + name: string + hp: int + x, y: float // names in a row share a type + + func hurt(amount: int) { // changes hp, so Dino knows it modifies the player + hp -= amount + } + + func alive(): bool { + return hp > 0 + } +} + +func main() { + var p = Player{name: "Ada", hp: 10} // fields left out start at zero + p.hurt(4) + print(p.hp, p.alive()) + print(p) +} +``` +```text +6 true +Player{name: Ada, hp: 6, x: 0.0, y: 0.0} +``` + +Ways to build one: + +- Named: `Player{name: "Ada", hp: 10}`. Any field may be left out; it starts at `0`, `0.0`, + `false`, `""` or an empty list. +- Positional: `Vec2{1.0, 2.0}`. Every field must be given. + +**You never write `mut`.** A method that assigns to a field is detected as changing the +struct, and the compiler stops you from calling it on a `const` or on a temporary. + +Inside a method, a bare name means: a local or parameter first, then a field, then another +method of the same struct (`hurt(2)`), then a global function. + +### Structs are values + +```dino +struct Point { x, y: int } + +func main() { + var a = Point{1, 2} + var b = a + b.x = 99 + print(a, b) +} +``` +```text +Point{x: 1, y: 2} Point{x: 99, y: 2} +``` + +`var b = a` is a real copy. If you want one value with several owners, see +[`shared`](08-memory.md). + +### Methods that return values, and nested data + +```dino +struct Vec2 { + x, y: float + + func length_squared(): float { + return (x * x + y * y) + } + func plus(o: Vec2): Vec2 { + return Vec2{x + o.x, y + o.y} + } +} + +struct Ship { + pos: Vec2 + trail: []Vec2 +} + +func main() { + var s = Ship{pos: Vec2{1.0, 1.0}} + s.trail.push(s.pos) + s.pos = s.pos.plus(Vec2{2.0, 0.0}) + print(s.pos, s.trail, s.pos.length_squared()) +} +``` +```text +Vec2{x: 3.0, y: 1.0} [Vec2{x: 1.0, y: 1.0}] 10.0 +``` + +`==` on whole structs is not available yet; compare the fields. + +## Enums + +An enum is "one of these cases", and each case can carry data. + +```dino +enum Event { + Quit + Key(code: int) + Click(x, y: int) + + func describe(): string { + return match self { + Quit => "quit" + Key(c) => "key {c}" + Click(x, y) => "click at {x},{y}" + } + } +} + +func main() { + var e = Event.Click(3, 4) + print(e.describe()) + print(Event.Key(65), Event.Quit) +} +``` +```text +click at 3,4 +Event.Key(65) Event.Quit +``` + +- Build a value with the qualified name: `Event.Key(65)`, `Event.Quit`. +- Take it apart with [`match`](03-control-flow.md#match), which must cover every case. +- Inside an enum's methods, `self` is the whole value. +- The zero value of an enum is its first case. +- `==` works on enums whose cases carry no data. + +## Privacy + +Everything is private to its file unless you write `pub`. This matters once you split a +program into several files ([next chapters](10-modules-and-stdlib.md)). Inside one file you +never need `pub`. + +Next: [Errors and nil](07-errors-and-nil.md) diff --git a/docs/07-errors-and-nil.md b/docs/07-errors-and-nil.md new file mode 100644 index 0000000..bc59820 --- /dev/null +++ b/docs/07-errors-and-nil.md @@ -0,0 +1,123 @@ +# 7. Errors and nil + +Dino has no exceptions. A function that can fail says so in its result, and you handle it. + +## error + +`error` is a type that is either `nil` (all fine) or holds a message. + +```dino +func parse_age(s: string): (int, error) { + var n, err = s.to_int() + if err != nil { + return 0, error("'{s}' is not a number") + } + if n < 0 { + return 0, error("age can't be negative") + } + return n, nil +} + +func main() { + var a, err = parse_age("42") + print(a, err) + var b, err2 = parse_age("abc") + print(b, err2) +} +``` +```text +42 nil +0 'abc' is not a number +``` + +- `error("text {value}")` makes an error. It can use `{ }` like any string. +- `var value, err = f()` takes both results. Use `_` to ignore one. +- You can't silently ignore an error: calling a function that returns an error as a bare + statement is a compile error. + +## `?`: pass the problem up + +Writing `?` after a call means "if this failed, return the error from *my* function too". +Otherwise you get the value. + +```dino +func half(n: int): (int, error) { + if n % 2 != 0 { return 0, error("{n} is odd") } + return n / 2, nil +} + +func quarter(n: int): (int, error) { + var h = half(n)? + var q = half(h)? + return q, nil +} + +func main() { + var a, err = quarter(8) + print(a, err) + var b, err2 = quarter(6) + print(b, err2) +} +``` +```text +2 nil +0 3 is odd +``` + +`?` works in any function whose last result is `error`. `func main(): error` is allowed: +if it returns an error, the program prints `error: message` and exits with code 1. + +## Optional values: `T?` + +A `T?` is either a `T` or `nil`. It is how Dino says "this might be missing". + +```dino +func find(names: []string, wanted: string): int? { + for i, n in names { + if n == wanted { return i } + } + return nil +} + +func main() { + var names = ["ann", "bo", "cy"] + var at = find(names, "bo") + if at != nil { + print("found at", at) + } + var missing = find(names, "zed") + print(missing == nil) +} +``` +```text +found at 1 +true +``` + +The compiler **requires** the check. It knows a value is set: + +- inside `if x != nil { ... }` +- after `if x == nil { return }` (or `break` / `continue`) +- on the right side of `x != nil && ...` + +Assigning to `x` makes the compiler forget that. (And even so, using a `nil` value stops the +program with a message instead of misbehaving.) + +Maps give you optionals too: + +```dino +func main() { + var ports = {"http": 80} + var p = ports.get("https") + if p == nil { + print("no https") + } +} +``` +```text +no https +``` + +Not available yet: `T?` as a struct field or list item, and `??`. + +Next: [Memory](08-memory.md) diff --git a/docs/08-memory.md b/docs/08-memory.md new file mode 100644 index 0000000..cd3d23c --- /dev/null +++ b/docs/08-memory.md @@ -0,0 +1,140 @@ +# 8. Memory: copies, sharing, and weak pointers + +You never write `free`, `new` or `&`. Here is what the compiler does for you. + +## One owner + +Every value has exactly one owner: a variable, a field, a list slot. When the owner's block +ends, the value is freed. That is true for strings, lists, maps, structs, enums and errors, +and it also happens on an early `return`, `break`, `continue` or `?`. + +Giving a value a new owner **copies** it: + +```dino +func main() { + var a = [1, 2, 3] + var b = a // b is a real copy + b.push(4) + print(a, b) +} +``` +```text +[1, 2, 3] [1, 2, 3, 4] +``` + +You don't pay for copies you don't need: + +- If `a` is never used again, `var b = a` just *moves* the value, with no copying. +- A function that only reads a parameter gets it without a copy. +- Returning a local variable moves it out. +- Strings are immutable and counted, so copying one only bumps a counter. + +All tests of the compiler run under a memory checker, so leaks and double frees are caught. + +## shared: one value, several owners + +Sometimes you really do want several owners of **one** value (a texture used by many +sprites, a window and its buttons). Write `shared`: + +```dino +struct Texture { + name: string + uses: int + + func touch() { uses += 1 } +} + +struct Sprite { + tex: shared Texture + x: int +} + +func main() { + var tex = shared Texture{"grass", 0} + var a = Sprite{tex, 1} + var b = Sprite{tex, 2} + a.tex.touch() + b.tex.touch() + print(tex.uses, a.tex.uses) +} +``` +```text +2 2 +``` + +- `shared Texture{...}` creates the shared value. The type is written `shared Texture`. +- `var b = a`, passing it to a function, or putting it in a list or field gives another + **owner** instead of copying the value. +- A change through any owner is seen by all. +- The value is freed when the **last** owner goes away. +- Use it just like the value inside: `tex.name`, `tex.touch()`. +- Only structs and enums can be shared for now. + +## weak: pointing without owning + +If a window owns its buttons and each button owns its window, neither would ever be freed. +Make one side `weak`: it points at the value without keeping it alive. + +```dino +struct Window { + title: string + buttons: []shared Button + + func add(b: shared Button) { buttons.push(b) } +} + +struct Button { + label: string + window: weak Window + + func click() { + if window != nil { + print(label, "clicked in", window.title) + } else { + print(label, "clicked, window is gone") + } + } +} + +func main() { + var win = shared Window{"Main", []} + var ok = shared Button{"OK", weak win} + win.add(ok) + ok.click() + + win = shared Window{"Other", []} // the first window is freed + ok.click() +} +``` +```text +OK clicked in Main +OK clicked, window is gone +``` + +- `weak x` makes a weak pointer to the shared value `x`. +- A weak pointer is **nilable**: `w == nil` tells you the value is gone. `weak T` and + `weak T?` mean the same thing, and it starts as `nil`. +- Using a gone weak pointer stops the program with a clear message. +- To use the value as an owner, call `w.upgrade()`. It gives a `shared T?`: the value + (now with one more owner, so it can't vanish while you hold it) or `nil`. + +```dino +struct Node { name: string } + +func main() { + var n = shared Node{"root"} + var w = weak n + var again = w.upgrade() + if again != nil { + print(again.name) + } +} +``` +```text +root +``` + +**Rule of thumb:** the owner of something holds it `shared`; anything that merely points +back at it (a child to its parent, a button to its window) holds it `weak`. + +Next: [Generics and interfaces](09-generics-and-interfaces.md) diff --git a/docs/09-generics-and-interfaces.md b/docs/09-generics-and-interfaces.md new file mode 100644 index 0000000..144af8f --- /dev/null +++ b/docs/09-generics-and-interfaces.md @@ -0,0 +1,184 @@ +# 9. Generics and interfaces + +## Generic functions + +Put the type names in `[ ]` after the function name. The compiler works the real types out +from the arguments. + +```dino +func first[T](xs: []T): T { + return xs[0] +} + +func biggest[T: Ordered](a, b: T): T { + if a > b { return a } + return b +} + +func main() { + print(first([7, 8, 9]), first(["x", "y"])) + print(biggest(3, 9), biggest("pear", "apple"), biggest(1.5, 0.5)) +} +``` +```text +7 x +9 pear 1.5 +``` + +After the colon you can name a **rule** for the type: + +| Rule | Meaning | +|------|---------| +| `Ordered` | can be compared with `<` `>` (numbers and strings) | +| `Number` | numbers only | +| an interface | has certain methods (see below) | +| *(none)* | anything | + +Generics cost nothing at run time: every set of types gets its own compiled copy. + +A generic function can take functions too, which makes `map` and `filter` easy to write: + +```dino +func map[T, U](xs: []T, f: func(T): U): []U { + var out: []U + for x in xs { out.push(f(x)) } + return out +} + +func main() { + print(map([1, 2, 3], x => x * 10)) + print(map(["a", "bb"], s => s.len())) +} +``` +```text +[10, 20, 30] +[1, 2] +``` + +## Generic types + +Structs and enums can take types too. When you **use** one, write the types out. + +```dino +struct Stack[T] { + items: []T + + func push_item(x: T) { items.push(x) } + func pop_item(): T { return items.pop() } + func size(): int { return items.len() } +} + +enum Maybe[T] { + Some(value: T) + None +} + +func main() { + var s = Stack[int]{} + s.push_item(1) + s.push_item(2) + print(s.pop_item(), s.size()) + + var m = Maybe[string].Some("hi") + print(m) +} +``` +```text +2 1 +Maybe[string].Some(hi) +``` + +## Interfaces + +An interface lists methods. A type follows it just by having those methods, with no +`implements` keyword. + +```dino +interface Shape { + area(): float + name(): string +} + +struct Square { + side: float + func area(): float { return side * side } + func name(): string { return "square" } +} + +struct Circle { + r: float + func area(): float { return 3.0 * r * r } + func name(): string { return "circle" } +} +``` + +### As a rule for generics + +```dino +interface Shape { + area(): float +} + +struct Square { + side: float + func area(): float { return side * side } +} + +func report[T: Shape](s: T) { + print("area is", s.area()) +} + +func main() { + report(Square{3.0}) +} +``` +```text +area is 9.0 +``` + +### As a type (dynamic dispatch) + +An interface is also a type of its own. A variable of that type can hold any value that has +the methods, and the right method runs at run time. This is how you build a mixed list. + +```dino +interface Shape { + area(): float + name(): string +} + +struct Square { + side: float + func area(): float { return side * side } + func name(): string { return "square" } +} + +struct Circle { + r: float + func area(): float { return 3.0 * r * r } + func name(): string { return "circle" } +} + +func main() { + var shapes: []Shape = [Square{2.0}, Circle{1.0}] + for s in shapes { + print(s.name(), s.area()) + } + var one: Shape = Circle{2.0} + print(one.area()) +} +``` +```text +square 4.0 +circle 3.0 +12.0 +``` + +Details: + +- A struct or enum value converts to the interface automatically. The value is copied in. +- `nil` is the empty interface value; calling a method on it stops the program. +- Not yet: turning an interface value back into its original type, and interfaces that take + their own type parameters. + +Next: [Files and the standard library](10-modules-and-stdlib.md) diff --git a/docs/10-modules-and-stdlib.md b/docs/10-modules-and-stdlib.md new file mode 100644 index 0000000..2e0dece --- /dev/null +++ b/docs/10-modules-and-stdlib.md @@ -0,0 +1,123 @@ +# 10. Files and the standard library + +## Splitting a program into files + +`import utils` brings in the file `utils.dino` that sits next to your main file. Use its +contents with the module name in front. Only things marked `pub` can be used from outside. + +`utils.dino`: + +```dino +pub const SCALE = 10 + +pub struct Vec2 { + pub x, y: float + pub func scaled(k: float): Vec2 { return Vec2{x * k, y * k} } +} + +pub func double(n: int): int { return n * 2 } + +func secret() {} // private: only this file can call it +``` + +`main.dino`: + +```dino +import utils + +func main() { + print(utils.double(4), utils.SCALE) + var v = utils.Vec2{1.0, 2.0} + print(v.scaled(3.0)) +} +``` + +- Folders work too: `import tools.fs` is the file `tools/fs.dino`, used as `fs.name`. +- Fields are private as well unless written `pub x: int`. +- Every file has its own names, so two files can both define `helper`. +- Import loops and missing files are errors. An error message names the file it is in. + +## Built-in modules + +Import them like your own files: `import os`. + +### os + +| | | +|--|--| +| `os.args` | list of command-line words (`args[0]` is the program) | +| `os.read_file(path)` | `(string, error)` | +| `os.write_file(path, text)` | `error` | +| `os.append_file(path, text)` | `error` | +| `os.exists(path)` | `bool` | +| `os.read_line()` | next line typed on the keyboard, or `nil` at the end | +| `os.env(name)` | the environment variable, or `nil` | +| `os.exit(code)` | stop the program | + +### math + +`sqrt pow floor ceil round trunc sin cos tan asin acos atan atan2 exp log log10 hypot +min max abs`, and the values `PI` and `E`. + +```dino +import math + +func main() { + print(math.sqrt(16), math.pow(2, 10), math.max(3, 9), math.PI) +} +``` +```text +4.0 1024.0 9 3.14159265358979 +``` + +### time + +`time.now()` gives seconds as a float. `time.sleep(seconds)` waits. + +### Strings + +`upper lower trim split lines fields replace repeat contains starts_with ends_with +index_of to_int to_float`, plus `len()` and slicing. + +```dino +func main() { + var csv = " a, b ,c " + for part in csv.split(",") { + print("[" + part.trim() + "]") + } + print("x".repeat(3), "Hello".lower(), "a-b".replace("-", "+")) + print("hello".index_of("l"), "hello".index_of("z")) +} +``` +```text +[a] +[b] +[c] +xxx hello a+b +2 -1 +``` + +A list of strings can be joined: `["a", "b"].join("-")`. + +## A program that reads input + +```dino +import os + +func main() { + var total = 0 + for { + var line = os.read_line() + if line == nil { break } + var n, err = line.trim().to_int() + if err != nil { + print("skipping:", line) + continue + } + total += n + } + print("total", total) +} +``` + +Next: [Cheatsheet](11-cheatsheet.md) diff --git a/docs/11-cheatsheet.md b/docs/11-cheatsheet.md new file mode 100644 index 0000000..c60c7c2 --- /dev/null +++ b/docs/11-cheatsheet.md @@ -0,0 +1,127 @@ +# 11. Cheatsheet + +## Basics +```dino +var x = 1 // changeable; type inferred +var y: float = 2 // with a type +const MAX = 100 // fixed +x += 1 // also -= *= /= %= +print("x is {x}", y) // interpolation with { } +``` + +## Types +`int float bool char string error` · sized: `i8 i16 i32 u8 u16 u32 u64 f32` · `[]T` list · +`map[K, V]` map · `T?` optional · `shared T` · `weak T` · `func(A, B): R` function. +Convert numbers with `float(x)`, `int(x)`, `u8(x)`. + +## Control flow +```dino +if a > b { } else if a == b { } else { } +for i in 0..10 { } // 0..9 +for i in 0..10 step 2 { } +for x in list { } +for i, x in list { } +for k, v in map { } +for cond { } +for { break } +match v { 1 => a, _ => b } +``` + +## Functions +```dino +func add(a, b: int): int { return a + b } +func f(): (int, error) { return 1, nil } +var n, err = f() +var n = f()? // inside a function that returns error +var g = func(x: int): int { return x * 2 } +var h = x => x * 2 // where the type is known +``` + +## Lists +```dino +var xs = [1, 2, 3] var e: []int +xs.push(4) xs.pop() xs.len() +xs[0] xs[0] = 9 xs[1..3] +``` + +## Maps +```dino +var m = {"a": 1} var e: map[string, int] = {} +var e2 = map[string, int]{} +m["b"] = 2 // add or replace +m["b"] += 1 // missing key counts as zero +m["a"] // value; stops if missing +m.has("a") m.get("a") // bool, value-or-nil +m.get_or("z", 0) m.len() m.keys() m.values() m.remove("a") +``` + +## Strings +```dino +s.len() s.upper() s.lower() s.trim() s.split(",") s.lines() s.fields() +s.replace(a, b) s.repeat(n) s.contains(x) s.starts_with(x) s.ends_with(x) +s.index_of(x) s.to_int() s.to_float() s[1..4] s[2..] s[..3] +["a", "b"].join("-") +``` + +## Characters +```dino +var c = 'a' '\n' 'é' // one character, single quotes +c == 'a' c < 'z' // compare +c.is_digit() c.is_letter() c.is_alnum() c.is_space() c.is_upper() c.is_lower() +c.upper() c.lower() +int(c) char(97) string(c) "{c}" // number <-> char, char -> text +for c in s { } for i, c in s { } // walk a string; i = byte position +s[i] // the char starting at byte i +``` + +## Structs and enums +```dino +struct P { + name: string + x, y: float + func move(dx: float) { x += dx } // mutation is inferred +} +var p = P{name: "a"} // or positional: P{"a", 0.0, 0.0} + +enum E { + A + B(n: int) +} +E.B(3) match e { A => 1, B(n) => n } +``` + +## Errors and nil +```dino +error("message {x}") err != nil return 0, nil +var v: int? = nil if v != nil { v } if v == nil { return } +``` + +## Sharing +```dino +var a = shared T{...} var b = a // same value, two owners +var w = weak a w.upgrade() // shared T? (nil if gone) +``` + +## Generics and interfaces +```dino +func first[T](xs: []T): T { return xs[0] } +func big[T: Ordered](a, b: T): T { ... } // Ordered, Number, or an interface +struct Stack[T] { items: []T } Stack[int]{} +interface Shape { area(): float } // follow it by having the methods +var s: Shape = Circle{1.0} +``` + +## Files +```dino +import utils // utils.dino next to this file +import tools.fs // tools/fs.dino, used as fs.name +import os // built-in: os math time +pub func f() {} // visible to other files +``` + +## Commands +`dino run f.dino args…` · `dino build f.dino -o out` · `dino check f.dino` · `dino emit-c f.dino` · `dino fmt path` (`--check` to only report) · `dino lsp` (editor server) + +## Things that stop the program (exit code 2) +Divide by zero · list index/slice out of range · `pop` on an empty list · reading a missing +map key with `m[k]` · using a `nil` or gone `weak` value · calling a `nil` function or interface. diff --git a/docs/12-calculator-walkthrough.md b/docs/12-calculator-walkthrough.md new file mode 100644 index 0000000..1bb2f4c --- /dev/null +++ b/docs/12-calculator-walkthrough.md @@ -0,0 +1,99 @@ +# 12. A real program: the calculator + +The folder `examples/calc/` holds a calculator split over five files. It uses almost +everything from these notes. Run it: + +```text +cargo run -q -- run examples/calc/main.dino examples/calc/session.txt +``` + +or type calculations interactively by leaving off the file name. + +```text +1 + 2 * 3 = 7.0 +2 ^ 3 ^ 2 = 512.0 +x = 4 +x * 2 + pi = 11.1415926535898 +sqrt(16) + max(1, 5) +``` + +## The pieces + +| File | Job | Features it shows | +|------|-----|-------------------| +| `token.dino` | Turns text into tokens | enums, structs, `char`, string slicing, errors | +| `ast.dino` | The shape of a calculation | an enum that holds other nodes | +| `parser.dino` | Builds the tree | a struct with methods, `?`, recursion | +| `eval.dino` | Computes the answer | `match`, maps, function values, closures | +| `main.dino` | Reads lines and prints | `import`, `os`, `shared` | + +## 1. Tokens + +`"2 * (3 + 4)"` becomes a list of small pieces: a number, an operator, an open +parenthesis and so on. Each has a kind (an enum), its text and its position, so errors can +say "at column 5". + +The tokenizer looks at one character at a time (`var c = src[i]`) and decides what +kind of token starts there: `c.is_digit()` starts a number, `c.is_letter()` a name, and so on. +`src[start..i]` cuts the finished piece out of the text. + +## 2. The tree + +```dino +pub enum Node { + Num(value: float) + Var(name: string) + Neg(inner: []Node) + Bin(op: string, sides: []Node) + Call(name: string, args: []Node) + Assign(name: string, value: []Node) +} +``` + +An enum can't contain itself directly, so the parts that hold other nodes use a list +(`[]Node`) with one or more items. + +## 3. The parser + +One method per level of precedence: `sum` calls `product`, which calls `unary`, which calls +`power`, which calls `atom`. Each returns `(Node, error)` and the callers pass problems up +with `?`. + +## 4. The evaluator + +The scope that remembers variables and functions is two maps: + +```dino +pub struct Scope { + variables: map[string, float] + builtins: map[string, Builtin] + ... +} +``` + +A built-in function is a struct holding its name, how many arguments it takes, and a +function value: + +```dino +s.builtins["sqrt"] = Builtin{"sqrt", 1, a => math.sqrt(a[0])} +``` + +`eval` is one big `match` on the node. Looking up a variable is `variables.get(name)`, +which gives `nil` when it isn't there, turned into a friendly error. + +## 5. The main file + +The scope is created once as `shared`, so every line sees the variables set by the +lines before it, with no need to pass the scope back and forth: + +```dino +var scope = shared eval.new_scope() +... +run_line(scope, line) +``` + +## Try changing it + +- Add a function `sin(x)` next to `sqrt`. +- Add `%` as an operator in the tokenizer, parser and `apply`. +- Make `sum` accept any number of arguments. diff --git a/docs/README.md b/docs/README.md new file mode 100644 index 0000000..265662e --- /dev/null +++ b/docs/README.md @@ -0,0 +1,50 @@ +# Dino: learn the language + +Dino is a small, fast, modern language. It looks like Go, runs like C, and has no +garbage collector, no pointers, and nothing low-level to write. You write plain code; +the compiler works out the memory. + +These notes teach it from zero. Read them in order, or jump to the +[cheatsheet](11-cheatsheet.md) when you just need to look something up. + +| # | Note | What you learn | +|---|------|----------------| +| 1 | [First steps](01-first-steps.md) | Running a program, `print`, `main` | +| 2 | [Values and types](02-values-and-types.md) | `var`, `const`, numbers, strings | +| 3 | [Control flow](03-control-flow.md) | `if`, `for`, `match` | +| 4 | [Functions](04-functions.md) | Parameters, results, closures | +| 5 | [Lists and maps](05-lists-and-maps.md) | Collections | +| 6 | [Structs and enums](06-structs-and-enums.md) | Your own types, methods | +| 7 | [Errors and nil](07-errors-and-nil.md) | `error`, `?`, `T?` | +| 8 | [Memory](08-memory.md) | Copies, `shared`, `weak` | +| 9 | [Generics and interfaces](09-generics-and-interfaces.md) | Code that works for many types | +| 10 | [Files and the standard library](10-modules-and-stdlib.md) | `import`, `os`, `math`, `time` | +| 11 | [Cheatsheet](11-cheatsheet.md) | Everything on one page | +| 12 | [A real program](12-calculator-walkthrough.md) | A calculator, split over five files | + +## How to read the examples + +Every `dino` block that has a `func main` is a complete program you can save and run. +The block right after it (marked `text`) shows what it prints. + +```` +```dino +func main() { + print("hello") +} +``` +```text +hello +``` +```` + +## The four ideas behind Dino + +1. **Values.** `var b = a` makes a real copy. Change `b` and `a` stays the same. + There are no hidden aliases, which makes programs easy to reason about. +2. **The compiler infers.** You don't write `mut`, lifetimes or `&`. A method that changes + its struct is detected automatically; a parameter that is only read is passed without a copy. +3. **Errors are values.** Functions return `(value, error)` and you pass problems up with `?`. + Missing values are `T?` and the compiler makes you check for `nil`. +4. **Sharing is explicit.** When you really want one value with several owners, you + write `shared`. Everything else has exactly one owner and is freed at the end of its block. diff --git a/editors/README.md b/editors/README.md new file mode 100644 index 0000000..b0ac73f --- /dev/null +++ b/editors/README.md @@ -0,0 +1,111 @@ +# Editor support for Dino + +Everything here talks to the compiler itself, so what you see in the editor is exactly what +`dino run` would tell you. + +| You get | How | +|---------|-----| +| Syntax colours | a grammar file (VS Code, Neovim, Vim) | +| Errors underlined as you type | the language server, `dino lsp` | +| Format the file (and on save) | the same server, using `dino fmt` | +| Outline, go to definition, hover, completion, parameter hints | the same server (reads the file's names; no type checking) | + +First put the `dino` program on your `PATH`: + +```text +cargo build --release +cp target/release/dino ~/.local/bin/ # or anywhere on your PATH +``` + +## VS Code + +The extension is in `editors/vscode/`. + +```text +cd editors/vscode +npm install # downloads the small language-client library +npx @vscode/vsce package # makes dino-0.1.0.vsix +code --install-extension dino-0.1.0.vsix +``` + +To try it without installing, open the `editors/vscode` folder in VS Code and press **F5**. + +If `dino` is not on your `PATH`, set **Dino: Server Path** in the settings to the full path. +Format-on-save and 4-space indents are switched on for `.dino` files by default. + +## Neovim + +Put the `vim/` folder on your runtime path to get file detection and colours, for example +(with a plugin manager that takes a local path), or copy the two files: + +```text +mkdir -p ~/.config/nvim/syntax ~/.config/nvim/ftdetect +cp vim/syntax/dino.vim ~/.config/nvim/syntax/ +cp vim/ftdetect/dino.vim ~/.config/nvim/ftdetect/ +``` + +Then in `init.lua` (Neovim 0.8 or newer): + +```lua +vim.api.nvim_create_autocmd("FileType", { + pattern = "dino", + callback = function(args) + vim.lsp.start({ + name = "dino", + cmd = { "dino", "lsp" }, + root_dir = vim.fs.dirname(args.file), + }) + vim.bo.shiftwidth = 4 + vim.bo.expandtab = true + end, +}) +-- format when saving +vim.api.nvim_create_autocmd("BufWritePre", { + pattern = "*.dino", + callback = function() vim.lsp.buf.format({ async = false }) end, +}) +``` + +Plain Vim gets the colours from the same two files (no language server). + +## Helix + +Add this to `~/.config/helix/languages.toml`: + +```toml +[language-server.dino] +command = "dino" +args = ["lsp"] + +[[language]] +name = "dino" +scope = "source.dino" +file-types = ["dino"] +comment-token = "//" +indent = { tab-width = 4, unit = " " } +language-servers = ["dino"] +auto-format = true +``` + +## Any other editor + +Anything that can run a language server works: start `dino lsp` (it speaks the standard +protocol over stdin/stdout) for files ending in `.dino`. It supports diagnostics and +whole-file formatting. The grammar for colours is the TextMate file +`vscode/syntaxes/dino.tmLanguage.json`, which Sublime Text, Zed and many others can read. + +## What the server does (and doesn't) do yet + +- Checks the file every time you type, including the files it imports (from disk, or from + unsaved changes in other open files). The compiler stops at the first error, so you see + one underline at a time. +- An error inside an imported file is shown at the top of the file that imports it. +- Formats the whole file; a file that doesn't parse is left alone. +- Not yet: hover, go to definition, completion, rename. + +## Opcode + +Opcode (the C++ editor in `~/Desktop/Porn/Linux/Clean/opcode`) knows Dino out of the box: colours, +`//` comments, snippets, the outline, format with `dino fmt -`, and the language server +(`dino lsp`) for errors, go to definition, hover, completion and parameter hints. It only needs +`dino` on your `PATH` (see the top of this page). diff --git a/editors/vim/ftdetect/dino.vim b/editors/vim/ftdetect/dino.vim new file mode 100644 index 0000000..d956566 --- /dev/null +++ b/editors/vim/ftdetect/dino.vim @@ -0,0 +1 @@ +autocmd BufRead,BufNewFile *.dino setfiletype dino diff --git a/editors/vim/syntax/dino.vim b/editors/vim/syntax/dino.vim new file mode 100644 index 0000000..5bbad55 --- /dev/null +++ b/editors/vim/syntax/dino.vim @@ -0,0 +1,37 @@ +" Vim / Neovim syntax highlighting for Dino +if exists("b:current_syntax") + finish +endif + +syn keyword dinoControl if else for in step match return break continue +syn keyword dinoDeclare func var const import struct enum interface pub +syn keyword dinoModifier shared weak +syn keyword dinoConstant true false nil +syn keyword dinoType int float bool char string error map i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 +syn match dinoTypeName "\<[A-Z][A-Za-z0-9_]*\>" +syn match dinoNumber "\<[0-9][0-9_]*\(\.[0-9][0-9_]*\)\?\>" +syn match dinoCall "\<[a-z_][A-Za-z0-9_]*\ze\s*(" +syn match dinoOperator "=>\|\.\.\|==\|!=\|<=\|>=\|&&\|||\|[-+*/%]=\?\|[<>!?=]" +syn match dinoEscape "\\." contained +syn region dinoInterp matchgroup=dinoInterpEdge start="{" end="}" contained contains=TOP +syn region dinoString start=+"+ skip=+\\\\\|\\"+ end=+"+ contains=dinoEscape,dinoInterp +syn match dinoChar "'\(\\.\|[^'\\]\)'" +syn region dinoComment start="/\*" end="\*/" +syn match dinoComment "//.*$" + +hi def link dinoControl Conditional +hi def link dinoDeclare Keyword +hi def link dinoModifier StorageClass +hi def link dinoConstant Constant +hi def link dinoType Type +hi def link dinoTypeName Type +hi def link dinoNumber Number +hi def link dinoCall Function +hi def link dinoOperator Operator +hi def link dinoEscape SpecialChar +hi def link dinoInterpEdge Delimiter +hi def link dinoString String +hi def link dinoChar Character +hi def link dinoComment Comment + +let b:current_syntax = "dino" diff --git a/editors/vscode/extension.js b/editors/vscode/extension.js new file mode 100644 index 0000000..90b7ae4 --- /dev/null +++ b/editors/vscode/extension.js @@ -0,0 +1,26 @@ +// Starts `dino lsp` and connects the editor to it. All the work is done by the compiler. +const vscode = require('vscode'); +const { LanguageClient } = require('vscode-languageclient/node'); + +let client; + +function activate() { + const command = vscode.workspace.getConfiguration('dino').get('serverPath') || 'dino'; + client = new LanguageClient( + 'dino', + 'Dino Language Server', + { command, args: ['lsp'] }, + { documentSelector: [{ scheme: 'file', language: 'dino' }] } + ); + client.start().catch((err) => { + vscode.window.showErrorMessage( + `Could not start "${command} lsp": ${err.message}. Put the dino program on your PATH or set "dino.serverPath".` + ); + }); +} + +function deactivate() { + return client ? client.stop() : undefined; +} + +module.exports = { activate, deactivate }; diff --git a/editors/vscode/language-configuration.json b/editors/vscode/language-configuration.json new file mode 100644 index 0000000..5851d0c --- /dev/null +++ b/editors/vscode/language-configuration.json @@ -0,0 +1,19 @@ +{ + "comments": { "lineComment": "//", "blockComment": ["/*", "*/"] }, + "brackets": [["{", "}"], ["[", "]"], ["(", ")"]], + "autoClosingPairs": [ + { "open": "{", "close": "}" }, + { "open": "[", "close": "]" }, + { "open": "(", "close": ")" }, + { "open": "\"", "close": "\"", "notIn": ["string"] }, + { "open": "'", "close": "'", "notIn": ["string", "comment"] } + ], + "surroundingPairs": [["{", "}"], ["[", "]"], ["(", ")"], ["\"", "\""], ["'", "'"]], + "indentationRules": { + "increaseIndentPattern": "[\\{\\[\\(]\\s*(//.*)?$", + "decreaseIndentPattern": "^\\s*[\\}\\]\\)]" + }, + "onEnterRules": [ + { "beforeText": "^\\s*//.*$", "action": { "indent": "none", "appendText": "// " } } + ] +} diff --git a/editors/vscode/package.json b/editors/vscode/package.json new file mode 100644 index 0000000..b543d06 --- /dev/null +++ b/editors/vscode/package.json @@ -0,0 +1,47 @@ +{ + "name": "dino", + "displayName": "Dino", + "description": "Syntax highlighting, error checking and formatting for the Dino language", + "version": "0.1.0", + "publisher": "dino-lang", + "license": "MIT", + "engines": { "vscode": "^1.75.0" }, + "categories": ["Programming Languages", "Formatters"], + "activationEvents": ["onLanguage:dino"], + "main": "./extension.js", + "dependencies": { "vscode-languageclient": "^9.0.1" }, + "contributes": { + "languages": [ + { + "id": "dino", + "aliases": ["Dino", "dino"], + "extensions": [".dino"], + "configuration": "./language-configuration.json" + } + ], + "grammars": [ + { + "language": "dino", + "scopeName": "source.dino", + "path": "./syntaxes/dino.tmLanguage.json" + } + ], + "configuration": { + "title": "Dino", + "properties": { + "dino.serverPath": { + "type": "string", + "default": "dino", + "description": "The `dino` program that provides the language server (it is started as `dino lsp`)." + } + } + }, + "configurationDefaults": { + "[dino]": { + "editor.insertSpaces": true, + "editor.tabSize": 4, + "editor.formatOnSave": true + } + } + } +} diff --git a/editors/vscode/syntaxes/dino.tmLanguage.json b/editors/vscode/syntaxes/dino.tmLanguage.json new file mode 100644 index 0000000..73953b0 --- /dev/null +++ b/editors/vscode/syntaxes/dino.tmLanguage.json @@ -0,0 +1,103 @@ +{ + "$schema": "https://raw.githubusercontent.com/martinring/tmlanguage/master/tmlanguage.json", + "name": "Dino", + "scopeName": "source.dino", + "patterns": [ + { "include": "#comments" }, + { "include": "#strings" }, + { "include": "#chars" }, + { "include": "#declarations" }, + { "include": "#keywords" }, + { "include": "#constants" }, + { "include": "#numbers" }, + { "include": "#types" }, + { "include": "#calls" }, + { "include": "#operators" } + ], + "repository": { + "comments": { + "patterns": [ + { "name": "comment.line.double-slash.dino", "match": "//.*$" }, + { "name": "comment.block.dino", "begin": "/\\*", "end": "\\*/" } + ] + }, + "strings": { + "name": "string.quoted.double.dino", + "begin": "\"", + "end": "\"", + "patterns": [ + { "name": "constant.character.escape.dino", "match": "\\\\." }, + { + "name": "meta.interpolation.dino", + "begin": "\\{", + "end": "\\}", + "beginCaptures": { "0": { "name": "punctuation.section.interpolation.begin.dino" } }, + "endCaptures": { "0": { "name": "punctuation.section.interpolation.end.dino" } }, + "patterns": [{ "include": "$self" }] + } + ] + }, + "chars": { + "name": "string.quoted.single.dino", + "match": "'(?:\\\\.|[^'\\\\])'", + "captures": {} + }, + "declarations": { + "patterns": [ + { + "match": "\\b(func)\\s+([A-Za-z_][A-Za-z0-9_]*)", + "captures": { + "1": { "name": "keyword.declaration.function.dino" }, + "2": { "name": "entity.name.function.dino" } + } + }, + { + "match": "\\b(struct|enum|interface)\\s+([A-Za-z_][A-Za-z0-9_]*)", + "captures": { + "1": { "name": "keyword.declaration.type.dino" }, + "2": { "name": "entity.name.type.dino" } + } + } + ] + }, + "keywords": { + "patterns": [ + { "name": "keyword.control.dino", "match": "\\b(if|else|for|in|step|match|return|break|continue)\\b" }, + { "name": "keyword.declaration.dino", "match": "\\b(func|var|const|import|struct|enum|interface|pub)\\b" }, + { "name": "storage.modifier.dino", "match": "\\b(shared|weak)\\b" } + ] + }, + "constants": { + "name": "constant.language.dino", + "match": "\\b(true|false|nil)\\b" + }, + "numbers": { + "name": "constant.numeric.dino", + "match": "\\b[0-9][0-9_]*(?:\\.[0-9][0-9_]*)?\\b" + }, + "types": { + "patterns": [ + { + "name": "support.type.builtin.dino", + "match": "\\b(int|float|bool|char|string|error|map|i8|i16|i32|i64|u8|u16|u32|u64|f32|f64)\\b" + }, + { "name": "entity.name.type.dino", "match": "\\b[A-Z][A-Za-z0-9_]*\\b" } + ] + }, + "calls": { + "name": "entity.name.function.call.dino", + "match": "\\b[a-z_][A-Za-z0-9_]*(?=\\s*\\()" + }, + "operators": { + "patterns": [ + { "name": "keyword.operator.arrow.dino", "match": "=>" }, + { "name": "keyword.operator.range.dino", "match": "\\.\\." }, + { "name": "keyword.operator.comparison.dino", "match": "==|!=|<=|>=|<|>" }, + { "name": "keyword.operator.logical.dino", "match": "&&|\\|\\||!" }, + { "name": "keyword.operator.assignment.dino", "match": "[-+*/%]?=" }, + { "name": "keyword.operator.arithmetic.dino", "match": "[-+*/%]" }, + { "name": "keyword.operator.optional.dino", "match": "\\?" } + ] + } + } +} diff --git a/examples/01_hello_tool.dino b/examples/01_hello_tool.dino new file mode 100644 index 0000000..3027f3c --- /dev/null +++ b/examples/01_hello_tool.dino @@ -0,0 +1,29 @@ +// A small CLI tool: count lines and words in files. +import os + +struct Stats { + lines: int + words: int +} + +func count(path: string): (Stats, error) { + var text = os.read_file(path)? // ? returns the error to the caller + var stats = Stats{} + for line in text.lines() { + stats.lines += 1 + stats.words += line.fields().len() + } + return stats, nil +} + +func main() { + var args = os.args + for path in args[1..] { + var s, err = count(path) + if err != nil { + print("{path}: error: {err}") + continue + } + print("{path}: {s.lines} lines, {s.words} words") + } +} diff --git a/examples/02_game_loop.dino b/examples/02_game_loop.dino new file mode 100644 index 0000000..64adc17 --- /dev/null +++ b/examples/02_game_loop.dino @@ -0,0 +1,33 @@ +// A game: bouncing square. Structs are values, no heap, no GC. +import gfx + +struct Vec2 { + x, y: f32 + + func add(b: Vec2): Vec2 { return Vec2{x + b.x, y + b.y} } + func scale(k: f32): Vec2 { return Vec2{x * k, y * k} } +} + +struct Player { + pos: Vec2 + vel: Vec2 + size: f32 + + func update(dt: f32) { // writes pos/vel: inferred as mutating + pos = pos.add(vel.scale(dt)) + if pos.x < 0 || pos.x > 800 { vel.x = -vel.x } + if pos.y < 0 || pos.y > 600 { vel.y = -vel.y } + } +} + +func main() { + var win = gfx.open("Dino", 800, 600) + var player = Player{pos: Vec2{100, 100}, vel: Vec2{200, 150}, size: 32} + + for win.is_open() { + player.update(win.dt()) + win.clear(gfx.BLACK) + win.rect(player.pos.x, player.pos.y, player.size, player.size, gfx.GREEN) + win.present() + } +} diff --git a/examples/03_shared_app.dino b/examples/03_shared_app.dino new file mode 100644 index 0000000..23f717a --- /dev/null +++ b/examples/03_shared_app.dino @@ -0,0 +1,32 @@ +// A desktop-style app: shared state, weak back-references, tasks. +import ui +import task + +struct Document { + title: string + text: string +} + +struct Editor { + doc: shared Document // many views can share one document + parent: weak Window? // weak: no ownership cycle +} + +func main() { + var doc = shared Document{title: "Notes", text: ""} + + var win = ui.window("Dino Notes", 640, 480) + var editor = Editor{doc: doc} + + win.on_key(func(k: ui.Key) { + editor.doc.text += k.char + }) + + var ch = task.channel[string]() + task.spawn(func() { // background autosave + for text in ch.recv() { save(text) } + }) + + win.on_close(func() { ch.send(doc.text) }) + win.run() +} diff --git a/examples/04_types.dino b/examples/04_types.dino new file mode 100644 index 0000000..9cb3a8b --- /dev/null +++ b/examples/04_types.dino @@ -0,0 +1,23 @@ +// Enums with data, generics, interfaces. +enum Shape { + Circle(r: f64) + Rect(w, h: f64) +} + +func area(s: Shape): f64 { + return match s { + Circle(r) => 3.14159 * r * r + Rect(w, h) => w * h + } +} + +interface Drawable { draw() } + +func draw_all[T: Drawable](items: []T) { + for item in items { item.draw() } +} + +func max[T: Ordered](a, b: T): T { + if a > b { return a } + return b +} diff --git a/examples/basics/chars.dino b/examples/basics/chars.dino new file mode 100644 index 0000000..5c2f28b --- /dev/null +++ b/examples/basics/chars.dino @@ -0,0 +1,37 @@ +struct Tok { first: char } + +func kind(c: char): string { + return match c { + '+' => "plus" + '\n' => "newline" + ' ' => "space" + _ => "other" + } +} + +func main() { + var c = 'a' + print(c, c == 'a', c < 'b', int(c), char(66), string(c) + "!") + var s = "héllo wörld 42" + var letters = 0 + var digits = 0 + for ch in s { + if ch.is_letter() { letters += 1 } + if ch.is_digit() { digits += 1 } + } + print(letters, digits, s.len()) + for i, ch in "aé😀" { print(i, ch) } + print(s[0], s[1], s[3], s[s.len() - 1]) + var up = "" + for ch in "dino" { up += string(ch.upper()) } + print(up, 'Q'.lower(), kind('+'), kind('\n'), kind('z')) + var counts: map[char, int] = {} + for ch in "banana" { counts[ch] += 1 } + print(counts) + var w = "" + for ch in "a b c" { + if ch == ' ' { continue } + w += string(ch) + } + print(w, 'a'.is_alnum(), ' '.is_space(), 'A'.is_upper()) +} diff --git a/examples/basics/chars.out b/examples/basics/chars.out new file mode 100644 index 0000000..73f48ca --- /dev/null +++ b/examples/basics/chars.out @@ -0,0 +1,9 @@ +a true true 97 B a! +10 2 16 +0 a +1 é +3 😀 +h é l 2 +DINO q plus newline other +{b: 1, a: 3, n: 2} +abc true true true diff --git a/examples/basics/closures.dino b/examples/basics/closures.dino new file mode 100644 index 0000000..d78d551 --- /dev/null +++ b/examples/basics/closures.dino @@ -0,0 +1,74 @@ +struct Button { + label: string + on_click: func(string) + clicks: int + + func click() { + clicks += 1 + on_click(label) + } +} + +func apply(f: func(int): int, x: int): int { + return f(x) +} + +func twice(f: func(int): int): func(int): int { + return func(x: int): int { return f(f(x)) } +} + +func make_counter(): func(): int { + var n = 0 + return func(): int { + n += 1 + return n + } +} + +func double(x: int): int { return x * 2 } + +func map[T, U](xs: []T, f: func(T): U): []U { + var out: []U + for x in xs { out.push(f(x)) } + return out +} + +func filter[T](xs: []T, keep: func(T): bool): []T { + var out: []T + for x in xs { + if keep(x) { out.push(x) } + } + return out +} + +func main() { + var offset = 10 + var add = func(x: int): int { return x + offset } + offset = 99 // the closure kept its own copy + print(add(1), apply(add, 5), apply(double, 4)) + print(apply(x => x * x, 7), twice(double)(3), twice(x => x + 1)(0)) + + var next = make_counter() + var other = make_counter() + print(next(), next(), next(), other()) + var snapshot = next // copies the closure, with its state + print(next(), snapshot()) + + var nums = [1, 2, 3, 4, 5, 6] + print(map(nums, x => x * 10)) + print(map(nums, x => "n{x}")) + print(filter(nums, n => n % 2 == 0)) + var total = 0 + for n in nums { total += apply(double, n) } + print(total) + + var log = "" + var b = Button{"OK", func(name: string) { print("clicked", name) }, 0} + b.click() + b.click() + print(b.clicks) + var handlers: []func(int): int + handlers.push(double) + handlers.push(x => x - 1) + for h in handlers { print(h(10)) } +} diff --git a/examples/basics/closures.out b/examples/basics/closures.out new file mode 100644 index 0000000..1c7acec --- /dev/null +++ b/examples/basics/closures.out @@ -0,0 +1,13 @@ +11 15 8 +49 12 2 +1 2 3 1 +4 4 +[10, 20, 30, 40, 50, 60] +[n1, n2, n3, n4, n5, n6] +[2, 4, 6] +42 +clicked OK +clicked OK +2 +20 +9 diff --git a/examples/basics/enum_data.dino b/examples/basics/enum_data.dino new file mode 100644 index 0000000..b9c9641 --- /dev/null +++ b/examples/basics/enum_data.dino @@ -0,0 +1,101 @@ +// A tree: a node holds a list of child trees (a list can hold the enum it is inside). +enum Tree { + Leaf(value: int) + Node(children: []Tree) + + func sum(): int { + return match self { + Leaf(v) => v + Node(kids) => sum_all(kids) + } + } + + func depth(): int { + return match self { + Leaf(_) => 1 + Node(kids) => 1 + deepest(kids) + } + } +} + +func sum_all(ts: []Tree): int { + var total = 0 + for t in ts { + total += t.sum() + } + return total +} + +func deepest(ts: []Tree): int { + var best = 0 + for t in ts { + var d = t.depth() + if d > best { + best = d + } + } + return best +} + +// Error handling with a plain enum. +enum Result { + Ok(value: int) + Err(msg: string) +} + +func divide(a, b: int): Result { + if b == 0 { + return Result.Err("divide by zero") + } + return Result.Ok(a / b) +} + +func show(r: Result) { + match r { + Ok(v) => print("ok:", v) + Err(m) => print("error:", m) + } +} + +// Data inside an enum is a value like everything else. +enum Msg { + Quit + Say(text: string) + Batch(items: []string) +} + +struct Inbox { + last: Msg + count: int +} + +func main() { + var tree = Tree.Node([Tree.Leaf(1), Tree.Node([Tree.Leaf(2), Tree.Leaf(3)]), Tree.Leaf(4)]) + print("sum", tree.sum(), "depth", tree.depth()) + + show(divide(10, 2)) + show(divide(1, 0)) + + var m = Msg.Batch(["a", "b"]) + var copy = m + match copy { + Batch(items) => { + items.push("c") + print("copy has", items) + } + _ => print("not a batch") + } + print("original still", m) + + var inbox = Inbox{Msg.Say("hi"), 1} + var other = inbox + other.last = Msg.Quit + other.count += 1 + print(inbox, other) + + match inbox.last { + Say(t) => print("said", t) + Quit => print("quit") + Batch(_) => print("batch") + } +} diff --git a/examples/basics/enum_data.out b/examples/basics/enum_data.out new file mode 100644 index 0000000..a4d9faa --- /dev/null +++ b/examples/basics/enum_data.out @@ -0,0 +1,7 @@ +sum 10 depth 3 +ok: 5 +error: divide by zero +copy has [a, b, c] +original still Msg.Batch([a, b]) +Inbox{last: Msg.Say(hi), count: 1} Inbox{last: Msg.Quit, count: 2} +said hi diff --git a/examples/basics/enums.dino b/examples/basics/enums.dino new file mode 100644 index 0000000..ba15bde --- /dev/null +++ b/examples/basics/enums.dino @@ -0,0 +1,93 @@ +enum Shape { + Circle(r: float) + Rect(w, h: float) + Point + + func area(): float { + return match self { + Circle(r) => 3.14159 * r * r + Rect(w, h) => w * h + Point => 0 + } + } + + func name(): string { + return match self { + Circle(_) => "circle" + Rect(_, _) => "rectangle" + Point => "point" + } + } +} + +enum Color { + Red + Green + Blue +} + +func describe(c: Color): string { + return match c { + Color.Red => "warm" + Color.Green => "natural" + Color.Blue => "cool" + } +} + +func classify(n: int): string { + return match n { + 0 => "zero" + 1 => "one" + _ => "many" + } +} + +func main() { + var shapes = [Shape.Circle(1.5), Shape.Rect(2, 3), Shape.Point] + for s in shapes { + print(s.name(), s.area()) + } + print(shapes) + + var c = Color.Green + print(c, describe(c), c == Color.Green, c != Color.Blue) + + for i in 0..4 { + print(i, classify(i)) + } + + // match as a statement can use blocks and loop control + var total = 0.0 + for s in shapes { + match s { + Circle(r) => { + total += r + } + Rect(w, _) => { + if w > 100 { + break + } + total += w + } + _ => continue + } + print("so far", total) + } + + // bool and string matches + var flag = true + print(match flag { + true => "yes" + false => "no" + }) + var cmd = "stop" + match cmd { + "go" => print("going") + "stop" => print("stopping") + other => print("unknown:", other) + } + + // the zero value of an enum is its first case + var z: Shape + print(z, Color.Red == (Color.Red)) +} diff --git a/examples/basics/enums.out b/examples/basics/enums.out new file mode 100644 index 0000000..efbb7b0 --- /dev/null +++ b/examples/basics/enums.out @@ -0,0 +1,14 @@ +circle 7.0685775 +rectangle 6.0 +point 0.0 +[Shape.Circle(1.5), Shape.Rect(2.0, 3.0), Shape.Point] +Color.Green natural true true +0 zero +1 one +2 many +3 many +so far 1.5 +so far 3.5 +yes +stopping +Shape.Circle(0.0) true diff --git a/examples/basics/errors.dino b/examples/basics/errors.dino new file mode 100644 index 0000000..4aba95c --- /dev/null +++ b/examples/basics/errors.dino @@ -0,0 +1,48 @@ +func parse_digit(c: string): (int, error) { + if c == "0" { return 0, nil } + if c == "1" { return 1, nil } + if c == "2" { return 2, nil } + return error("not a digit: {c}") +} + +func add_digits(a: string, b: string): (int, error) { + var x = parse_digit(a)? + var y = parse_digit(b)? + return x + y, nil +} + +func check(n: int): error { + if n < 0 { + return error("negative: {n}") + } + return nil +} + +func run(): error { + check(5)? + print("5 is fine") + check(-3)? + print("never printed") + return nil +} + +func main(): error { + var sum, err = add_digits("1", "2") + if err != nil { + print("unexpected:", err) + } else { + print("sum =", sum) + } + + var _, bad = add_digits("1", "x") + if bad != nil { + print("failed:", bad) + } + + var ok: error = nil + print("ok is", ok) + + run()? + print("not reached") + return nil +} diff --git a/examples/basics/errors.out b/examples/basics/errors.out new file mode 100644 index 0000000..fbeb691 --- /dev/null +++ b/examples/basics/errors.out @@ -0,0 +1,4 @@ +sum = 3 +failed: not a digit: x +ok is nil +5 is fine diff --git a/examples/basics/fib.dino b/examples/basics/fib.dino new file mode 100644 index 0000000..a6b8386 --- /dev/null +++ b/examples/basics/fib.dino @@ -0,0 +1,12 @@ +func fib(n: int): int { + if n < 2 { + return n + } + return fib(n - 1) + fib(n - 2) +} + +func main() { + for i in 0..15 { + print("fib({i}) = {fib(i)}") + } +} diff --git a/examples/basics/fib.out b/examples/basics/fib.out new file mode 100644 index 0000000..ad0fb78 --- /dev/null +++ b/examples/basics/fib.out @@ -0,0 +1,15 @@ +fib(0) = 0 +fib(1) = 1 +fib(2) = 1 +fib(3) = 2 +fib(4) = 3 +fib(5) = 5 +fib(6) = 8 +fib(7) = 13 +fib(8) = 21 +fib(9) = 34 +fib(10) = 55 +fib(11) = 89 +fib(12) = 144 +fib(13) = 233 +fib(14) = 377 diff --git a/examples/basics/generic_types.dino b/examples/basics/generic_types.dino new file mode 100644 index 0000000..3db3b91 --- /dev/null +++ b/examples/basics/generic_types.dino @@ -0,0 +1,53 @@ +struct Stack[T] { + items: []T + + func push_item(x: T) { items.push(x) } + func pop_item(): T { return items.pop() } + func size(): int { return items.len() } + func peek(): T { return items[items.len() - 1] } +} + +enum Maybe[T] { + Some(value: T) + None + + func or_else(fallback: T): T { + return match self { + Some(v) => v + None => fallback + } + } +} + +struct Pair[A, B] { + first: A + second: B +} + +struct Tree[T] { + value: T + kids: []Tree[T] +} + +func find(xs: []int, x: int): Maybe[int] { + for v in xs { + if v == x { return Maybe[int].Some(v) } + } + return Maybe[int].None +} + +func main() { + var s = Stack[int]{} + s.push_item(1) + s.push_item(2) + print(s.size(), s.peek(), s.pop_item(), s.size()) + var names: Stack[string] + names.push_item("a") + names.push_item("b") + print(names.peek(), names) + print(find([1, 2, 3], 2).or_else(0), find([1], 9).or_else(-1)) + var p = Pair[string, float]{"pi", 3.14} + print(p.first, p.second, p) + var t = Tree[int]{1, [Tree[int]{2, []}]} + print(t.kids.len(), t.kids[0].value) +} diff --git a/examples/basics/generic_types.out b/examples/basics/generic_types.out new file mode 100644 index 0000000..55dea7f --- /dev/null +++ b/examples/basics/generic_types.out @@ -0,0 +1,5 @@ +2 2 2 1 +b Stack[string]{items: [a, b]} +2 -1 +pi 3.14 Pair[string, float]{first: pi, second: 3.14} +1 2 diff --git a/examples/basics/hello.dino b/examples/basics/hello.dino new file mode 100644 index 0000000..204702f --- /dev/null +++ b/examples/basics/hello.dino @@ -0,0 +1,3 @@ +func main() { + print("Hello, Dino!") +} diff --git a/examples/basics/hello.out b/examples/basics/hello.out new file mode 100644 index 0000000..dfcfd3d --- /dev/null +++ b/examples/basics/hello.out @@ -0,0 +1 @@ +Hello, Dino! diff --git a/examples/basics/interface_values.dino b/examples/basics/interface_values.dino new file mode 100644 index 0000000..5a732d4 --- /dev/null +++ b/examples/basics/interface_values.dino @@ -0,0 +1,79 @@ +interface Shape { + area(): float + name(): string + scale(k: float) +} + +struct Circle { + r: float + func area(): float { return 3.0 * r * r } + func name(): string { return "circle" } + func scale(k: float) { r = r * k } +} + +struct Rect { + w, h: float + tag: string + func area(): float { return w * h } + func name(): string { return "rect " + tag } + func scale(k: float) { + w = w * k + h = h * k + } +} + +enum Dot { + Small + Big + func area(): float { return 1.0 } + func name(): string { return "dot" } + func scale(k: float) {} +} + +struct Scene { + shapes: []Shape + main_shape: Shape +} + +func describe(s: Shape): string { + return "{s.name()}: {s.area()}" +} + +func biggest(shapes: []Shape): Shape { + var best = shapes[0] + for s in shapes { + if s.area() > best.area() { best = s } + } + return best +} + +func total[T: Shape](xs: []T): float { + var sum = 0.0 + for x in xs { sum += x.area() } + return sum +} + +func main() { + var shapes: []Shape = [Circle{1.0}, Rect{2.0, 3.0, "a"}, Dot.Big] + shapes.push(Circle{2.0}) + for s in shapes { print(describe(s)) } + print(biggest(shapes).name()) + + // interface values are copies: changing one doesn't change the original + var c = Circle{1.0} + var s: Shape = c + s.scale(10.0) + print(c.r, s.area()) + var t = s + t.scale(2.0) + print(s.area(), t.area()) + + var scene = Scene{shapes, Rect{1.0, 1.0, "main"}} + scene.main_shape.scale(3.0) + print(scene.main_shape.area(), scene.shapes.len()) + print(scene.main_shape) + + var none: Shape + print(none == nil, s == nil) + print(total(shapes)) +} diff --git a/examples/basics/interface_values.out b/examples/basics/interface_values.out new file mode 100644 index 0000000..4d3280e --- /dev/null +++ b/examples/basics/interface_values.out @@ -0,0 +1,11 @@ +circle: 3.0 +rect a: 6.0 +dot: 1.0 +circle: 12.0 +circle +1.0 300.0 +300.0 1200.0 +9.0 4 +Rect{w: 3.0, h: 3.0, tag: main} +true false +22.0 diff --git a/examples/basics/interfaces.dino b/examples/basics/interfaces.dino new file mode 100644 index 0000000..567ad5d --- /dev/null +++ b/examples/basics/interfaces.dino @@ -0,0 +1,72 @@ +interface Shape { + area(): float + name(): string +} + +interface Resizable { + scale(k: float) +} + +struct Circle { + r: float + + func area(): float { return 3.0 * r * r } + func name(): string { return "circle" } + func scale(k: float) { r = r * k } +} + +struct Rect { + w, h: float + + func area(): float { return w * h } + func name(): string { return "rect" } +} + +enum Dot { + Small + Big + + func area(): float { return 1.0 } + func name(): string { return "dot" } +} + +func describe[T: Shape](s: T): string { + return "{s.name()} with area {s.area()}" +} + +func total_area[T: Shape](shapes: []T): float { + var sum = 0.0 + for s in shapes { sum += s.area() } + return sum +} + +func grow[T: Resizable](s: T): T { + var copy = s + copy.scale(2.0) + return copy +} + +struct Gallery[T: Shape] { + items: []T + + func add(x: T) { items.push(x) } + func biggest(): float { + var best = 0.0 + for it in items { + if it.area() > best { best = it.area() } + } + return best + } +} + +func main() { + print(describe(Circle{2.0})) + print(describe(Rect{2.0, 5.0})) + print(describe(Dot.Big)) + print(total_area([Circle{1.0}, Circle{2.0}])) + print(grow(Circle{1.5})) + var g = Gallery[Rect]{} + g.add(Rect{1.0, 2.0}) + g.add(Rect{3.0, 4.0}) + print(g.biggest()) +} diff --git a/examples/basics/interfaces.out b/examples/basics/interfaces.out new file mode 100644 index 0000000..aaa795b --- /dev/null +++ b/examples/basics/interfaces.out @@ -0,0 +1,6 @@ +circle with area 12.0 +rect with area 10.0 +dot with area 1.0 +15.0 +Circle{r: 3.0} +12.0 diff --git a/examples/basics/list_values.dino b/examples/basics/list_values.dino new file mode 100644 index 0000000..34a48b5 --- /dev/null +++ b/examples/basics/list_values.dino @@ -0,0 +1,68 @@ +// Lists are values: assigning or passing copies them. +struct Inventory { + items: []string + gold: int + + func add(item: string) { + items.push(item) + } + + func count(): int { + return items.len() + } +} + +func add_one(xs: []int): []int { + xs.push(1) + return xs +} + +func first(xs: []int): int { + return xs[0] +} + +func main() { + var a = [1, 2, 3] + var b = a + b[0] = 99 + b.push(4) + print(a, b) + + // a function that changes its parameter works on its own copy + var c = add_one(a) + print(a, c) + print(first(a), first(c)) + + // structs holding lists copy deeply + var inv = Inventory{[], 10} + inv.add("sword") + var saved = inv + inv.add("shield") + saved.gold = 0 + print(inv) + print(saved) + print(inv.count(), saved.count()) + + // nested lists + var grid = [[1, 2], [3, 4]] + var snapshot = grid + grid[0][1] = 20 + grid[1].push(5) + print(grid) + print(snapshot) + + // looping over a list while changing it walks the original + var xs = [1, 2, 3] + for x in xs { + xs.push(x * 10) + } + print(xs) + + // the loop variable is a copy too + var ys = [1, 2, 3] + for y in ys { + y += 100 + print(y) + } + print(ys) +} diff --git a/examples/basics/list_values.out b/examples/basics/list_values.out new file mode 100644 index 0000000..a110647 --- /dev/null +++ b/examples/basics/list_values.out @@ -0,0 +1,13 @@ +[1, 2, 3] [99, 2, 3, 4] +[1, 2, 3] [1, 2, 3, 1] +1 1 +Inventory{items: [sword, shield], gold: 10} +Inventory{items: [sword], gold: 0} +2 1 +[[1, 20], [3, 4, 5]] +[[1, 2], [3, 4]] +[1, 2, 3, 10, 20, 30] +101 +102 +103 +[1, 2, 3] diff --git a/examples/basics/lists.dino b/examples/basics/lists.dino new file mode 100644 index 0000000..8923d54 --- /dev/null +++ b/examples/basics/lists.dino @@ -0,0 +1,60 @@ +func sum(xs: []int): int { + var total = 0 + for x in xs { + total += x + } + return total +} + +func squares(n: int): []int { + var out: []int + for i in 0..n { + out.push(i * i) + } + return out +} + +func largest(xs: []int): int { + var best = xs[0] + for x in xs { + if x > best { + best = x + } + } + return best +} + +func main() { + var nums = [5, 3, 8, 1] + print(nums, nums.len()) + print("sum:", sum(nums), "largest:", largest(nums)) + + nums.push(10) + nums[0] = 50 + nums[1] += 7 + print(nums) + print("popped", nums.pop(), "left", nums.len()) + + var sq = squares(6) + print(sq) + print(sq[1..4], sq[..2], sq[4..]) + + for i, x in sq { + if i % 2 == 0 { + print("sq[{i}] = {x}") + } + } + + // an empty list needs a type + var empty: []string + print(empty, empty.len()) + empty.push("a") + empty.push("b") + print(empty, "{empty.len()} items") + + var floats: []float = [1, 2.5, 3] + print(floats) + + var words = ["dino", "lang"] + print(words[0] + "-" + words[1], words[0].len()) +} diff --git a/examples/basics/lists.out b/examples/basics/lists.out new file mode 100644 index 0000000..d5751c7 --- /dev/null +++ b/examples/basics/lists.out @@ -0,0 +1,13 @@ +[5, 3, 8, 1] 4 +sum: 17 largest: 8 +[50, 10, 8, 1, 10] +popped 10 left 4 +[0, 1, 4, 9, 16, 25] +[1, 4, 9] [0, 1] [16, 25] +sq[0] = 0 +sq[2] = 4 +sq[4] = 16 +[] 0 +[a, b] 2 items +[1.0, 2.5, 3.0] +dino-lang 4 diff --git a/examples/basics/loops.dino b/examples/basics/loops.dino new file mode 100644 index 0000000..3473d5d --- /dev/null +++ b/examples/basics/loops.dino @@ -0,0 +1,51 @@ +const LIMIT = 20 + +func is_prime(n: int): bool { + if n < 2 { + return false + } + var d = 2 + for d * d <= n { + if n % d == 0 { + return false + } + d += 1 + } + return true +} + +func main() { + var sum = 0 + for i in 1..11 { + sum += i + } + print("sum 1..10 =", sum) + + print("evens:") + for i in 0..LIMIT step 2 { + print(i) + } + + print("countdown:") + for i in 5..0 step -1 { + print(i) + } + + var primes = "" + for n in 0..LIMIT { + if !is_prime(n) { + continue + } + primes += "{n} " + } + print("primes:", primes) + + var k = 0 + for { + k += 1 + if k == 3 { + break + } + } + print("k =", k) +} diff --git a/examples/basics/loops.out b/examples/basics/loops.out new file mode 100644 index 0000000..fae0666 --- /dev/null +++ b/examples/basics/loops.out @@ -0,0 +1,20 @@ +sum 1..10 = 55 +evens: +0 +2 +4 +6 +8 +10 +12 +14 +16 +18 +countdown: +5 +4 +3 +2 +1 +primes: 2 3 5 7 11 13 17 19 +k = 3 diff --git a/examples/basics/maps.dino b/examples/basics/maps.dino new file mode 100644 index 0000000..632c13e --- /dev/null +++ b/examples/basics/maps.dino @@ -0,0 +1,48 @@ +struct Team { + name: string + scores: map[string, int] +} + +func total(m: map[string, int]): int { + var sum = 0 + for k, v in m { sum += v } + return sum +} + +func main() { + var ages = {"alice": 30, "bob": 25} + ages["carol"] = 41 + ages["bob"] = 26 + print(ages["bob"], ages.len(), ages.has("dave"), ages) + print(ages.get("alice"), ages.get("zed"), ages.get_or("zed", -1)) + var counts: map[string, int] = {} + for w in "a b a c b a".split(" ") { + counts[w] += 1 + } + print(counts) + for k, v in counts { print(k, "->", v) } + for k in counts.keys() { print("key", k) } + print(counts.values(), total(counts)) + counts.remove("b") + print(counts, counts.len()) + var copy = counts + copy["z"] = 9 + print(counts.len(), copy.len()) + var nums = map[int, []string]{} + nums[1] = ["x", "y"] + nums[2] = [] + nums[1].push("z") + print(nums) + var t = Team{name: "red", scores: {"a": 1}} + t.scores["b"] = 2 + var t2 = t + t2.scores["c"] = 3 + print(t.scores.len(), t2.scores.len()) + var big = map[int, int]{} + for i in 0..2000 { big[i * 7] = i } + var s = 0 + for i in 0..2000 { s += big[i * 7] } + print(s, big.len()) + for i in 0..1000 { big.remove(i * 7) } + print(big.len()) +} diff --git a/examples/basics/maps.out b/examples/basics/maps.out new file mode 100644 index 0000000..07fa6e1 --- /dev/null +++ b/examples/basics/maps.out @@ -0,0 +1,16 @@ +26 3 false {alice: 30, bob: 26, carol: 41} +30 nil -1 +{a: 3, b: 2, c: 1} +a -> 3 +b -> 2 +c -> 1 +key a +key b +key c +[3, 2, 1] 6 +{a: 3, c: 1} 2 +2 3 +{1: [x, y, z], 2: []} +2 3 +1999000 2000 +1000 diff --git a/examples/basics/numbers.dino b/examples/basics/numbers.dino new file mode 100644 index 0000000..0753722 --- /dev/null +++ b/examples/basics/numbers.dino @@ -0,0 +1,23 @@ +const PI = 3.14159 + +func area(r: float): float { + return PI * r * r +} + +func average(a, b: int): float { + return float(a + b) / 2 +} + +func main() { + print("area:", area(2)) + print("avg:", average(3, 4)) + print(10 / 3, 10 % 3, 10.0 / 4) + print(int(7.9), float(3)) + var x: float = 1 + x *= 2.5 + print(x, x > 2, -x) + print("whole float:", 2.0 * 3) + var big = 9223372036854775807 + big += 1 + print("wraps:", big) +} diff --git a/examples/basics/numbers.out b/examples/basics/numbers.out new file mode 100644 index 0000000..47d24a4 --- /dev/null +++ b/examples/basics/numbers.out @@ -0,0 +1,7 @@ +area: 12.56636 +avg: 3.5 +3 1 2.5 +7 3.0 +2.5 true -2.5 +whole float: 6.0 +wraps: -9223372036854775808 diff --git a/examples/basics/optionals.dino b/examples/basics/optionals.dino new file mode 100644 index 0000000..f626311 --- /dev/null +++ b/examples/basics/optionals.dino @@ -0,0 +1,53 @@ +struct User { + name: string + age: int +} + +func find(id: int): User? { + if id == 1 { + return User{name: "Ann", age: 30} + } + return nil +} + +func first_even(xs: []int): int? { + for x in xs { + if x % 2 == 0 { return x } + } + return nil +} + +func describe(id: int): string { + var u = find(id) + if u == nil { + return "nobody" + } + return "{u.name} is {u.age}" +} + +func main() { + print(describe(1)) + print(describe(2)) + + var n = first_even([1, 3, 4, 5]) + if n != nil { + print("first even:", n + 1) + } + var m: int? = first_even([1, 3]) + print("m is", m) + print("n is", n) + + var count: int? = 5 + if count != nil && count > 3 { + print("big count") + } + count = nil + if count == nil || count > 100 { + print("no count") + } + + var list: []int? = nil + if list == nil { + print("no list") + } +} diff --git a/examples/basics/optionals.out b/examples/basics/optionals.out new file mode 100644 index 0000000..231efa5 --- /dev/null +++ b/examples/basics/optionals.out @@ -0,0 +1,8 @@ +Ann is 30 +nobody +first even: 5 +m is nil +n is 4 +big count +no count +no list diff --git a/examples/basics/order.dino b/examples/basics/order.dino new file mode 100644 index 0000000..86d18d6 --- /dev/null +++ b/examples/basics/order.dino @@ -0,0 +1,33 @@ +func say(msg: string, v: int): int { + print(msg) + return v +} + +func add(a, b, c: int): int { + return a + b + c +} + +func yes(msg: string): bool { + print(msg) + return true +} + +func no(msg: string): bool { + print(msg) + return false +} + +func main() { + print("arguments run left to right:") + print(add(say("first", 1), say("second", 2), say("third", 3))) + + print("&& and || short-circuit:") + if no("A") && yes("B") { + print("unreachable") + } + if yes("C") || yes("D") { + print("done") + } + var ok = no("E") || yes("F") && yes("G") + print(ok) +} diff --git a/examples/basics/order.out b/examples/basics/order.out new file mode 100644 index 0000000..b6ee430 --- /dev/null +++ b/examples/basics/order.out @@ -0,0 +1,13 @@ +arguments run left to right: +first +second +third +6 +&& and || short-circuit: +A +C +done +E +F +G +true diff --git a/examples/basics/own_flows.dino b/examples/basics/own_flows.dino new file mode 100644 index 0000000..f7f3f86 --- /dev/null +++ b/examples/basics/own_flows.dino @@ -0,0 +1,101 @@ +enum Tok { + Word(w: string) + Num(n: int) + Many(parts: []string) + End +} + +func name_of(t: Tok): string { + return match t { + Word(w) => w + Num(n) => "n{n}" + Many(ps) => ps[0] + "..." + End => "end" + } +} + +func scan(ts: []Tok): (string, error) { + var acc = "" + for t in ts { + match t { + Word(w) => { + if w == "stop" { break } + if w == "skip" { continue } + acc += w + " " + } + Num(n) => { + if n < 0 { return error("negative " + name_of(t)) } + acc += "{n} " + } + Many(ps) => { + for p in ps { + if p == "x" { continue } + acc += p + } + } + End => { return acc, nil } + } + } + return acc + "|", nil +} + +func grid(n: int): [][]string { + var g: [][]string + for i in 0..n { + var row: []string + for j in 0..n { row.push("{i}{j}") } + g.push(row) + } + return g +} + +func longest(g: [][]string): string { + var best = "" + for row in g { + for cell in row { + if cell.len() >= best.len() { best = cell } + } + } + return best +} + +struct Counter { + seen: []string + func note(s: string): int { + seen.push(s + "") + return seen.len() + } +} + +func main() { + var ts = [Tok.Word("a"), Tok.Num(3), Tok.Many(["p", "x", "q"]), Tok.Word("skip"), Tok.Word("b")] + var r0, e0 = scan(ts) + print(r0, e0) + ts.push(Tok.Word("stop")) + ts.push(Tok.Word("never")) + var r, e = scan(ts) + print(r, e) + var bad = [Tok.Word("a"), Tok.Num(-4), Tok.End] + var r2, e2 = scan(bad) + print(r2, e2) + var ok = [Tok.Word("a"), Tok.End, Tok.Word("b")] + var r3, e3 = scan(ok) + print(r3, e3) + var g = grid(3) + g[1][1] = "mid" + "dle" + var h = g + h[0][0] = "changed" + print(g, longest(g), longest(h)) + var c = Counter{} + var n = 0 + for i in 0..3 { n += c.note("s{i}") } + print(n, c.seen) + var total = 0 + for i in 0..50 { + var tmp = "item" + "{i}" + total += tmp.len() + if i % 7 == 0 { continue } + if i > 40 { break } + } + print(total) +} diff --git a/examples/basics/own_flows.out b/examples/basics/own_flows.out new file mode 100644 index 0000000..9c244f2 --- /dev/null +++ b/examples/basics/own_flows.out @@ -0,0 +1,7 @@ +a 3 pqb | nil +a 3 pqb | nil + negative n-4 +a nil +[[00, 01, 02], [10, middle, 12], [20, 21, 22]] middle changed +6 [s0, s1, s2] +242 diff --git a/examples/basics/own_strings.dino b/examples/basics/own_strings.dino new file mode 100644 index 0000000..ceba151 --- /dev/null +++ b/examples/basics/own_strings.dino @@ -0,0 +1,101 @@ +struct Person { + name: string + tags: []string + func greet(): string { return "hi " + name + "!" } + func rename(n: string) { name = n } + func add_tag(t: string) { tags.push(t) } +} + +enum Msg { + Text(body: string) + Pair(a: string, items: []string) + Quit +} + +func describe(m: Msg): string { + return match m { + Text(b) => "text:" + b + Pair(a, items) => "pair:" + a + "/" + "{items.len()}" + Quit => "quit" + } +} + +func join(xs: []string, sep: string): string { + var out = "" + var first = true + for x in xs { + if !first { out += sep } + out += x + first = false + } + return out +} + +func find(xs: []string, key: string): string? { + for x in xs { + if x == key { return x } + } + return nil +} + +func fails(n: int): (string, error) { + if n > 2 { return error("too big: {n}") } + return "ok {n}", nil +} + +func chain(): (string, error) { + var a = fails(1)? + var b = fails(2)? + var c = fails(3)? + return a + b + c, nil +} + +func early(xs: []string): string { + var tmp = "tmp" + "1" + for x in xs { + var local = x + "!" + if local == "b!" { return local + tmp } + if local == "a!" { continue } + if local == "z!" { break } + } + return tmp +} + +func main() { + var p = Person{"ann", ["x", "y"]} + print(p.greet()) + p.rename("bob" + "by") + p.add_tag("z" + "z") + print(p.greet(), p.tags) + var q = p + q.rename("changed") + print(p.name, q.name) + + var ms = [Msg.Text("hello"), Msg.Pair("k", ["1", "2"]), Msg.Quit] + for m in ms { print(describe(m)) } + + print(join(["a", "b", "c"], ", ")) + var words: []string + for i in 0..5 { words.push("w{i}") } + print(words, find(words, "w3"), find(words, "nope")) + var f = find(words, "w2") + if f != nil { print("found", f + "?") } + + var s = "x" + for i in 0..4 { s = s + s } + print(s.len()) + + var v, e = fails(5) + print(v, e) + var w, e2 = chain() + print(w, e2) + print(early(["a", "c"]), early(["q", "b"]), early(["z"])) + + var popped = words.pop() + print(popped, words.len()) + words[0] = "first" + "!" + print(words) + var copy = words[1..3] + copy.push("end") + print(copy, words) +} diff --git a/examples/basics/own_strings.out b/examples/basics/own_strings.out new file mode 100644 index 0000000..92ec8bd --- /dev/null +++ b/examples/basics/own_strings.out @@ -0,0 +1,16 @@ +hi ann! +hi bobby! [x, y, zz] +bobby changed +text:hello +pair:k/2 +quit +a, b, c +[w0, w1, w2, w3, w4] w3 nil +found w2? +16 + too big: 5 + too big: 3 +tmp1 b!tmp1 tmp1 +w4 4 +[first!, w1, w2, w3] +[w1, w2, end] [first!, w1, w2, w3] diff --git a/examples/basics/own_structs.dino b/examples/basics/own_structs.dino new file mode 100644 index 0000000..372dfa9 --- /dev/null +++ b/examples/basics/own_structs.dino @@ -0,0 +1,82 @@ +struct Item { + name: string + qty: int +} +struct Box { + items: []Item + label: string + func summary(): string { return label + ":" + "{items.len()}" } + func first(): Item? { + if items.len() == 0 { return nil } + return items[0] + } +} + +func mk(n: string, q: int): Item { return Item{n + "_", q} } +func label_of(i: Item): string { return i.name + "#" + "{i.qty}" } +func fresh_box(l: string): Box { return Box{[mk("a", 1), mk("b", 2)], l + l} } +func pick(b: Box, k: int): (Item, error) { + if k >= b.items.len() { return error("no item {k} in " + b.label) } + return b.items[k], nil +} +func total(b: Box): (int, error) { + var t = 0 + for i in 0..3 { + var it = pick(b, i)? + t += it.qty + } + return t, nil +} +func rec(n: int): string { + if n == 0 { return "." } + return "{n}" + rec(n - 1) +} +func cmp(a: string, b: string): bool { return a + "x" == b + "x" } +func check(s: string): error { + if s == "bad" + "!" { return error("bad input: " + s) } + return nil +} +func run(): error { + check("fine")? + check("bad" + "!")? + return nil +} + +func main() { + print(label_of(mk("z", 9)), fresh_box("q").summary(), fresh_box("r").label) + var b = fresh_box("L") + b.label = b.label + "2" + b.items[0].name = "changed" + "!" + b.items.push(Item{"c", 3}) + print(b.summary(), b.items) + var f = b.first() + if f != nil && f.name != "" { print(f.name) } + var it, err = pick(b, 7) + print(it, err) + var t, err2 = total(b) + print(t, err2) + print(rec(3), cmp("a", "a"), cmp("a" + "b", "a")) + var i = 0 + for i < 3 && "{i}" != "2" { i += 1 } + print(i) + var msg = match i { + 2 => "two" + "!" + _ => "other" + } + print(msg) + print(run()) + var e = run() + if e != nil { print("failed:", e) } + var names: []string = ["m" + "1", "m" + "2"] + var out: []string + for n in names { out.push(n + n) } + names = out + print(names, out) + var opt: Item? = mk("o", 1) + if opt != nil { print(opt.name) } + opt = nil + print(opt) + var s = "a" + s += "b" + s + print(s) +} diff --git a/examples/basics/own_structs.out b/examples/basics/own_structs.out new file mode 100644 index 0000000..11b00ed --- /dev/null +++ b/examples/basics/own_structs.out @@ -0,0 +1,14 @@ +z_#9 qq:2 rr +LL2:3 [Item{name: changed!, qty: 1}, Item{name: b_, qty: 2}, Item{name: c, qty: 3}] +changed! +Item{name: , qty: 0} no item 7 in LL2 +6 nil +321. true false +2 +two! +bad input: bad! +failed: bad input: bad! +[m1m1, m2m2] [m1m1, m2m2] +o_ +nil +aba diff --git a/examples/basics/shared.dino b/examples/basics/shared.dino new file mode 100644 index 0000000..63068c0 --- /dev/null +++ b/examples/basics/shared.dino @@ -0,0 +1,69 @@ +struct Texture { + name: string + uses: int + + func touch() { uses += 1 } +} + +struct Sprite { + tex: shared Texture + x: int +} + +struct Window { + title: string + buttons: []shared Button + + func add(b: shared Button) { buttons.push(b) } +} + +struct Button { + label: string + window: weak Window? + clicks: int + + func click() { + clicks += 1 + if window != nil { + print(label, "clicked in", window.title) + } else { + print(label, "clicked, window is gone") + } + } +} + +func use_texture(t: shared Texture) { + t.touch() +} + +func main() { + // several owners of one value: a change through one is seen by all + var tex = shared Texture{"grass", 0} + var a = Sprite{tex, 1} + var b = Sprite{tex, 2} + a.tex.touch() + b.tex.touch() + use_texture(tex) + print(tex.name, tex.uses, a.tex.uses, b.tex.uses) + print(a) + + // a window and its buttons point at each other: the button's side is weak + var win = shared Window{"Main", []} + var ok = shared Button{"OK", weak win, 0} + win.add(ok) + ok.click() + win.buttons[0].click() + print(ok.clicks) + + var cancel = shared Button{label: "Cancel"} + print(cancel.window == nil) + cancel.window = weak win + print(cancel.window == nil) + cancel.click() + + // once the window is gone, the weak pointers know + var saved = ok + win = shared Window{"Other", []} + saved.click() + cancel.click() +} diff --git a/examples/basics/shared.out b/examples/basics/shared.out new file mode 100644 index 0000000..b6fe598 --- /dev/null +++ b/examples/basics/shared.out @@ -0,0 +1,10 @@ +grass 3 3 3 +Sprite{tex: Texture{name: grass, uses: 3}, x: 1} +OK clicked in Main +OK clicked in Main +2 +true +false +Cancel clicked in Main +OK clicked, window is gone +Cancel clicked, window is gone diff --git a/examples/basics/sieve.dino b/examples/basics/sieve.dino new file mode 100644 index 0000000..a65b0eb --- /dev/null +++ b/examples/basics/sieve.dino @@ -0,0 +1,45 @@ +func primes_up_to(n: int): []int { + var is_prime: []bool + for i in 0..n + 1 { + is_prime.push(true) + } + is_prime[0] = false + is_prime[1] = false + for i in 2..n + 1 { + if is_prime[i] { + for j in i * i..n + 1 step i { + is_prime[j] = false + } + } + } + var out: []int + for i, p in is_prime { + if p { + out.push(i) + } + } + return out +} + +func bubble_sort(xs: []int): []int { + var n = xs.len() + for i in 0..n { + for j in 0..n - i - 1 { + if xs[j] > xs[j + 1] { + var tmp = xs[j] + xs[j] = xs[j + 1] + xs[j + 1] = tmp + } + } + } + return xs +} + +func main() { + var ps = primes_up_to(60) + print(ps) + print("count:", ps.len()) + var unsorted = [9, 4, 7, 1, 8, 2] + var sorted = bubble_sort(unsorted) + print(unsorted, "->", sorted) +} diff --git a/examples/basics/sieve.out b/examples/basics/sieve.out new file mode 100644 index 0000000..fd0eb88 --- /dev/null +++ b/examples/basics/sieve.out @@ -0,0 +1,3 @@ +[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59] +count: 17 +[9, 4, 7, 1, 8, 2] -> [1, 2, 4, 7, 8, 9] diff --git a/examples/basics/sized.dino b/examples/basics/sized.dino new file mode 100644 index 0000000..28a582c --- /dev/null +++ b/examples/basics/sized.dino @@ -0,0 +1,50 @@ +struct Vec2 { + x, y: f32 + + func add(b: Vec2): Vec2 { return Vec2{x + b.x, y + b.y} } + func scale(k: f32): Vec2 { return Vec2{x * k, y * k} } +} + +func average(a: u8, b: u8): u8 { + return u8((int(a) + int(b)) / 2) +} + +func main() { + // literals take the type that is asked for + var small: u8 = 250 + small += 10 // wraps around: 260 becomes 4 + print(small) + + var id: i8 = -128 + print(id, id - 1) + + var big: u64 = 5 + big = big * 1000000000000 + print(big, big / 7, big % 7) + + var n: i32 = 2147483647 + print(n + 1) + + // f32 prints the shortest text that reads back the same + var speed: f32 = 1.1 + print(speed, speed * 2, speed / 3) + + var v = Vec2{1.5, 2} + print(v.add(Vec2{0.25, 0.25}).scale(2)) + + // conversions are explicit + var f = 3.99 + print(int(f), u8(f), i8(-200.5), u8(300.7), f32(f)) + print(u8(300), i16(70000), u32(-1), int(small) + 1) + print(average(200, 100)) + print(u8(7) == 7, small < 10) + + var xs: []u8 = [1, 2, 255] + xs[2] += 1 + print(xs) + + match id { + -128 => print("lowest i8") + _ => print("other") + } +} diff --git a/examples/basics/sized.out b/examples/basics/sized.out new file mode 100644 index 0000000..823d538 --- /dev/null +++ b/examples/basics/sized.out @@ -0,0 +1,12 @@ +4 +-128 127 +5000000000000 714285714285 5 +-2147483648 +1.1 2.2 0.36666667 +Vec2{x: 3.5, y: 4.5} +3 3 -128 255 3.99 +44 4464 4294967295 5 +150 +true true +[1, 2, 0] +lowest i8 diff --git a/examples/basics/stdlib.dino b/examples/basics/stdlib.dino new file mode 100644 index 0000000..a0e688f --- /dev/null +++ b/examples/basics/stdlib.dino @@ -0,0 +1,50 @@ +import os +import math +import time + +func count(path: string): (int, int, error) { + var text = os.read_file(path)? + var words = 0 + var lines = 0 + for line in text.lines() { + lines += 1 + words += line.fields().len() + } + return lines, words, nil +} + +func main() { + var csv = " alpha, beta ,gamma,, delta " + var parts = csv.split(",") + for p in parts { print("[" + p.trim() + "]") } + print(parts.len(), parts.join("|")) + print("Hello".upper(), "Hello".lower(), "a-b-c".replace("-", "+"), "ab".repeat(3)) + print("hello".contains("ell"), "hello".starts_with("he"), "hello".ends_with("x"), "hello".index_of("l"), "hello".index_of("z")) + var n, err = "42".to_int() + print(n, err) + var bad, err2 = "4x2".to_int() + print(bad, err2) + var f, err3 = "2.5".to_float() + print(f * 2, err3) + + print(math.sqrt(16), math.pow(2, 10), math.floor(2.7), math.round(2.5), math.PI) + print(math.min(3, 9), math.max(2.5, 1), math.abs(-7), math.abs(-2.5)) + var small: u8 = 7 + print(math.min(small, 200), math.max(small, 3)) + + var t0 = time.now() + time.sleep(0.01) + print(time.now() - t0 >= 0.01) + + var err4 = os.write_file("/tmp/dino_std_test.txt", "one two\nthree\n\nfour five six\n") + print(err4) + var lines, words, e5 = count("/tmp/dino_std_test.txt") + print(lines, words, e5) + var l2, w2, e6 = count("/tmp/definitely/not/here.txt") + print(l2, w2, e6) + print(os.exists("/tmp/dino_std_test.txt"), os.exists("/nope")) + print(os.args.len(), os.env("HOME_NOPE_X") == nil) + var home = os.env("PATH") + if home != nil { print(home.len() > 0) } + os.exit(3) +} diff --git a/examples/basics/stdlib.out b/examples/basics/stdlib.out new file mode 100644 index 0000000..82b87eb --- /dev/null +++ b/examples/basics/stdlib.out @@ -0,0 +1,21 @@ +[alpha] +[beta] +[gamma] +[] +[delta] +5 alpha| beta |gamma|| delta +HELLO hello a+b+c ababab +true true false 2 -1 +42 nil +0 "4x2" is not a whole number +5.0 nil +4.0 1024.0 2.0 3.0 3.14159265358979 +3 2.5 7 2.5 +7 7 +true +nil +4 6 nil +0 0 cannot read "/tmp/definitely/not/here.txt": No such file or directory +true false +1 true +true diff --git a/examples/basics/strings.dino b/examples/basics/strings.dino new file mode 100644 index 0000000..f00ecce --- /dev/null +++ b/examples/basics/strings.dino @@ -0,0 +1,19 @@ +func greet(name: string): string { + return "Hello, " + name + "!" +} + +func main() { + var name = "Dino" + print(greet(name)) + var n = 3 + print("n is {n}, doubled {n * 2}, big? {n > 2}") + print("pi is about {3.5 + 0.25}") + print("a" < "b", "abc" == "abc", "x" != "x") + print("tab\there \"quoted\" \{braces}") + print("café ünï") + var s = "" + for i in 0..5 { + s += "{i}," + } + print(s) +} diff --git a/examples/basics/strings.out b/examples/basics/strings.out new file mode 100644 index 0000000..73b095f --- /dev/null +++ b/examples/basics/strings.out @@ -0,0 +1,7 @@ +Hello, Dino! +n is 3, doubled 6, big? true +pi is about 3.75 +true true false +tab here "quoted" {braces} +café ünï +0,1,2,3,4, diff --git a/examples/basics/structs.dino b/examples/basics/structs.dino new file mode 100644 index 0000000..7bfe819 --- /dev/null +++ b/examples/basics/structs.dino @@ -0,0 +1,80 @@ +struct Vec2 { + x, y: float + + func add(b: Vec2): Vec2 { + return Vec2{x + b.x, y + b.y} + } + + func scale(k: float): Vec2 { + return Vec2{x * k, y * k} + } + + func length_squared(): float { + return x * x + y * y + } +} + +struct Player { + name: string + pos: Vec2 + vel: Vec2 + hp: int + + // changes pos and vel, so the compiler treats it as a mutating method + func update(dt: float) { + pos = pos.add(vel.scale(dt)) + if pos.x < 0 || pos.x > 100 { + vel.x = -vel.x + } + if pos.y < 0 || pos.y > 100 { + vel.y = -vel.y + } + } + + // calls a mutating method, so it mutates too + func step_twice(dt: float) { + update(dt) + update(dt) + } + + func hurt(amount: int) { + hp -= amount + if hp < 0 { + hp = 0 + } + } + + func is_alive(): bool { + return hp > 0 + } + + func describe(): string { + return "{name} at ({pos.x}, {pos.y}) hp={hp}" + } +} + +func main() { + var p = Player{name: "Ana", pos: Vec2{90, 50}, vel: Vec2{10, 5}, hp: 20} + print(p.describe()) + + p.update(1) + print(p.describe()) + p.update(1) + print(p.describe()) + + p.step_twice(0.5) + print(p.pos) + + // omitted fields start at zero + var q = Player{name: "Zed"} + print(q) + print(q.is_alive()) + + p.hurt(15) + p.hurt(15) + print(p.hp, p.is_alive()) + + var origin: Vec2 + print(origin, origin.length_squared()) + print(Vec2{3, 4}.length_squared()) +} diff --git a/examples/basics/structs.out b/examples/basics/structs.out new file mode 100644 index 0000000..7c247f9 --- /dev/null +++ b/examples/basics/structs.out @@ -0,0 +1,9 @@ +Ana at (90.0, 50.0) hp=20 +Ana at (100.0, 55.0) hp=20 +Ana at (110.0, 60.0) hp=20 +Vec2{x: 110.0, y: 65.0} +Player{name: Zed, pos: Vec2{x: 0.0, y: 0.0}, vel: Vec2{x: 0.0, y: 0.0}, hp: 0} +false +0 false +Vec2{x: 0.0, y: 0.0} 0.0 +25.0 diff --git a/examples/basics/value_semantics.dino b/examples/basics/value_semantics.dino new file mode 100644 index 0000000..d435240 --- /dev/null +++ b/examples/basics/value_semantics.dino @@ -0,0 +1,49 @@ +struct Counter { + n: int + + func bump(): int { + n += 1 + return n + } +} + +struct Pair { + left: Counter + right: Counter +} + +func bump_copy(c: Counter): int { + return c.bump() +} + +func main() { + // assignment copies + var a = Counter{1} + var b = a + b.n = 100 + print(a.n, b.n) + + // passing to a function copies + print(bump_copy(a), a.n) + + // methods change the variable they are called on + a.bump() + a.bump() + print(a.n) + + // nested structs are copied as a whole + var p = Pair{Counter{10}, Counter{20}} + var q = p + q.left.bump() + p.right.bump() + p.right.bump() + print(p.left.n, p.right.n, q.left.n, q.right.n) + + // reading before a later call in the same expression: the earlier read wins + var c = Counter{0} + print(c.n, c.bump(), c.n) + + // constants can be read and used with non-mutating code + const k = Counter{7} + print(k.n) +} diff --git a/examples/basics/value_semantics.out b/examples/basics/value_semantics.out new file mode 100644 index 0000000..2c12bb4 --- /dev/null +++ b/examples/basics/value_semantics.out @@ -0,0 +1,6 @@ +1 100 +2 1 +3 +10 22 11 20 +0 1 1 +7 diff --git a/examples/calc/ast.dino b/examples/calc/ast.dino new file mode 100644 index 0000000..8aab487 --- /dev/null +++ b/examples/calc/ast.dino @@ -0,0 +1,15 @@ +// The shape of a parsed calculation. + +pub enum Node { + Num(value: float) + Var(name: string) + Neg(inner: []Node) + Bin(op: string, sides: []Node) + Call(name: string, args: []Node) + Assign(name: string, value: []Node) +} + +// Enums can't hold themselves directly, so children live in a list of one or two. +pub func neg(n: Node): Node { return Node.Neg([n]) } +pub func bin(op: string, l, r: Node): Node { return Node.Bin(op, [l, r]) } +pub func assign(name: string, v: Node): Node { return Node.Assign(name, [v]) } diff --git a/examples/calc/eval.dino b/examples/calc/eval.dino new file mode 100644 index 0000000..364a20e --- /dev/null +++ b/examples/calc/eval.dino @@ -0,0 +1,83 @@ +// Evaluates a parsed calculation. Variables and functions live in `Scope`. +import ast +import math + +pub struct Builtin { + pub name: string + pub arity: int + pub run: func([]float): float +} + +pub struct Scope { + variables: map[string, float] + builtins: map[string, Builtin] + + pub func set(name: string, v: float) { + variables[name] = v + } + + pub func eval(n: ast.Node): (float, error) { + match n { + Num(v) => return v, nil + Var(name) => { + var v = variables.get(name) + if v == nil { return 0.0, error("unknown variable `{name}`") } + return v, nil + } + Neg(inner) => { + var x = eval(inner[0])? + return -x, nil + } + Assign(name, value) => { + var x = eval(value[0])? + set(name, x) + return x, nil + } + Bin(op, sides) => { + var a = eval(sides[0])? + var b = eval(sides[1])? + return apply(op, a, b) + } + Call(name, args) => { + if !builtins.has(name) { return 0.0, error("unknown function `{name}`") } + var b = builtins[name] + if args.len() != b.arity { + return 0.0, error("`{name}` takes {b.arity} argument(s), but {args.len()} given") + } + var vals: []float + for a in args { vals.push(eval(a)?) } + return b.run(vals), nil + } + } + } + + func apply(op: string, a, b: float): (float, error) { + match op { + "+" => return a + b, nil + "-" => return a - b, nil + "*" => return a * b, nil + "/" => { + if b == 0.0 { return 0.0, error("division by zero") } + return a / b, nil + } + "%" => { + if b == 0.0 { return 0.0, error("division by zero") } + return a - b * math.floor(a / b), nil + } + "^" => return math.pow(a, b), nil + _ => return 0.0, error("unknown operator `{op}`") + } + } +} + +pub func new_scope(): Scope { + var s = Scope{} + s.builtins["sqrt"] = Builtin{"sqrt", 1, a => math.sqrt(a[0])} + s.builtins["abs"] = Builtin{"abs", 1, a => math.abs(a[0])} + s.builtins["max"] = Builtin{"max", 2, a => math.max(a[0], a[1])} + s.builtins["min"] = Builtin{"min", 2, a => math.min(a[0], a[1])} + s.builtins["sum"] = Builtin{"sum", 3, a => a[0] + a[1] + a[2]} + s.set("pi", math.PI) + s.set("e", math.E) + return s +} diff --git a/examples/calc/main.dino b/examples/calc/main.dino new file mode 100644 index 0000000..15771c1 --- /dev/null +++ b/examples/calc/main.dino @@ -0,0 +1,43 @@ +import parser +import eval +import os + +// Evaluates one line and prints the answer or the problem. +func run_line(scope: shared eval.Scope, line: string) { + var text = line.trim() + if text == "" { return } + var tree, err = parser.parse(text) + if err != nil { + print(" error:", err) + return + } + var value, err2 = scope.eval(tree) + if err2 != nil { + print(" error:", err2) + return + } + print(" =", value) +} + +// Reads lines from the file named on the command line, or from the keyboard. +func main() { + var scope = shared eval.new_scope() + var args = os.args + if args.len() > 1 { + var text, err = os.read_file(args[1]) + if err != nil { + print("cannot read", args[1], err) + os.exit(1) + } + for line in text.lines() { + print(line) + run_line(scope, line) + } + return + } + for { + var line = os.read_line() + if line == nil { break } + run_line(scope, line) + } +} diff --git a/examples/calc/parser.dino b/examples/calc/parser.dino new file mode 100644 index 0000000..f6296d0 --- /dev/null +++ b/examples/calc/parser.dino @@ -0,0 +1,130 @@ +// Recursive descent: assignment < + - < * / % < unary minus < ^ < calls and atoms. +import token +import ast + +pub struct Parser { + tokens: []token.Token + at: int + + func peek(): token.Token { return tokens[at] } + + func next(): token.Token { + var t = tokens[at] + if at < tokens.len() - 1 { at += 1 } + return t + } + + func is_op(text: string): bool { + var t = peek() + return t.kind == token.Kind.Op && t.text == text + } + + func expect(kind: token.Kind, what: string): error { + if peek().kind != kind { + return error("expected {what} at column {peek().pos + 1}") + } + next() + return nil + } + + pub func statement(): (ast.Node, error) { + // `name = expression` + if peek().kind == token.Kind.Name && at + 1 < tokens.len() { + var after = tokens[at + 1] + if after.kind == token.Kind.Op && after.text == "=" { + var name = next().text + next() + var value = sum()? + return ast.assign(name, value), finish() + } + } + var e = sum()? + return e, finish() + } + + func finish(): error { + if peek().kind != token.Kind.End { + return error("unexpected `{peek().text}` at column {peek().pos + 1}") + } + return nil + } + + func sum(): (ast.Node, error) { + var left = product()? + for is_op("+") || is_op("-") { + var op = next().text + var right = product()? + left = ast.bin(op, left, right) + } + return left, nil + } + + func product(): (ast.Node, error) { + var left = unary()? + for is_op("*") || is_op("/") || is_op("%") { + var op = next().text + var right = unary()? + left = ast.bin(op, left, right) + } + return left, nil + } + + func unary(): (ast.Node, error) { + if is_op("-") { + next() + var inner = unary()? + return ast.neg(inner), nil + } + return power() + } + + func power(): (ast.Node, error) { + var base = atom()? + if is_op("^") { + next() + var exp = unary()? + return ast.bin("^", base, exp), nil + } + return base, nil + } + + func atom(): (ast.Node, error) { + var t = next() + match t.kind { + Num => { + var v, err = t.text.to_float() + if err != nil { return ast.Node.Num(0.0), error("bad number `{t.text}` at column {t.pos + 1}") } + return ast.Node.Num(v), nil + } + Name => { + if peek().kind == token.Kind.Open { + next() + var args: []ast.Node + if peek().kind != token.Kind.Close { + args.push(sum()?) + for is_op(",") { + next() + args.push(sum()?) + } + } + expect(token.Kind.Close, "`)`")? + return ast.Node.Call(t.text, args), nil + } + return ast.Node.Var(t.text), nil + } + Open => { + var inner = sum()? + expect(token.Kind.Close, "`)`")? + return inner, nil + } + End => return ast.Node.Num(0.0), error("the calculation ends too early (column {t.pos + 1})") + _ => return ast.Node.Num(0.0), error("unexpected `{t.text}` at column {t.pos + 1}") + } + } +} + +pub func parse(src: string): (ast.Node, error) { + var tokens = token.tokenize(src)? + var p = Parser{tokens, 0} + return p.statement() +} diff --git a/examples/calc/session.out b/examples/calc/session.out new file mode 100644 index 0000000..b1d5956 --- /dev/null +++ b/examples/calc/session.out @@ -0,0 +1,30 @@ +1 + 2 * 3 + = 7.0 +(1 + 2) * 3 + = 9.0 +-2 ^ 2 + = -4.0 +2 ^ 3 ^ 2 + = 512.0 +x = 10 / 4 + = 2.5 +x * 2 + pi + = 8.14159265358979 +sqrt(16) + abs(-3) + = 7.0 +max(2, 7) % 4 + = 3.0 +y + error: unknown variable `y` +1 / 0 + error: division by zero +foo(1) + error: unknown function `foo` +sqrt(1, 2) + error: `sqrt` takes 1 argument(s), but 2 given +3 + + error: the calculation ends too early (column 4) +4 $ 4 + error: unexpected `$` at column 3 +sum(1, 2, 3) * x + = 15.0 diff --git a/examples/calc/session.txt b/examples/calc/session.txt new file mode 100644 index 0000000..b575135 --- /dev/null +++ b/examples/calc/session.txt @@ -0,0 +1,15 @@ +1 + 2 * 3 +(1 + 2) * 3 +-2 ^ 2 +2 ^ 3 ^ 2 +x = 10 / 4 +x * 2 + pi +sqrt(16) + abs(-3) +max(2, 7) % 4 +y +1 / 0 +foo(1) +sqrt(1, 2) +3 + +4 $ 4 +sum(1, 2, 3) * x diff --git a/examples/calc/token.dino b/examples/calc/token.dino new file mode 100644 index 0000000..13ef8a3 --- /dev/null +++ b/examples/calc/token.dino @@ -0,0 +1,49 @@ +// Turns the text of a calculation into a list of tokens. + +pub enum Kind { + Num + Name + Op + Open + Close + End +} + +pub struct Token { + pub kind: Kind + pub text: string + pub pos: int +} + +pub func tokenize(src: string): ([]Token, error) { + var tokens: []Token + var i = 0 + for i < src.len() { + var c = src[i] + if c == ' ' { + i += 1 + continue + } + var start = i + if c.is_digit() || c == '.' { + for i < src.len() && (src[i].is_digit() || src[i] == '.') { i += 1 } + tokens.push(Token{Kind.Num, src[start..i], start}) + } else if c.is_letter() || c == '_' { + for i < src.len() && (src[i].is_alnum() || src[i] == '_') { i += 1 } + tokens.push(Token{Kind.Name, src[start..i], start}) + } else if "+-*/^%=,".contains(string(c)) { + tokens.push(Token{Kind.Op, string(c), start}) + i += 1 + } else if c == '(' { + tokens.push(Token{Kind.Open, "(", start}) + i += 1 + } else if c == ')' { + tokens.push(Token{Kind.Close, ")", start}) + i += 1 + } else { + return tokens, error("unexpected `{c}` at column {i + 1}") + } + } + tokens.push(Token{Kind.End, "", src.len()}) + return tokens, nil +} diff --git a/examples/modules/geometry.dino b/examples/modules/geometry.dino new file mode 100644 index 0000000..045d286 --- /dev/null +++ b/examples/modules/geometry.dino @@ -0,0 +1,13 @@ +// A module: everything marked `pub` can be used by the file that imports it. +import math + +pub const UNIT = 1.0 + +pub struct Point { + pub x, y: float + pub func dist(other: Point): float { + return math.sqrt((x - other.x) * (x - other.x) + (y - other.y) * (y - other.y)) + } +} + +pub func origin(): Point { return Point{0.0, 0.0} } diff --git a/examples/modules/main.dino b/examples/modules/main.dino new file mode 100644 index 0000000..64b8359 --- /dev/null +++ b/examples/modules/main.dino @@ -0,0 +1,6 @@ +import geometry + +func main() { + var p = geometry.Point{3.0, 4.0} + print(p.dist(geometry.origin()), geometry.UNIT) +} diff --git a/examples/modules/value_semantics.out b/examples/modules/value_semantics.out new file mode 100644 index 0000000..2c12bb4 --- /dev/null +++ b/examples/modules/value_semantics.out @@ -0,0 +1,6 @@ +1 100 +2 1 +3 +10 22 11 20 +0 1 1 +7 diff --git a/src/ast.rs b/src/ast.rs new file mode 100644 index 0000000..4bf7ec1 --- /dev/null +++ b/src/ast.rs @@ -0,0 +1,273 @@ +use std::fmt; + +#[derive(Clone, Copy, PartialEq, Eq, Default)] +pub struct Span { + pub line: u32, + pub col: u32, +} + +impl fmt::Debug for Span { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}:{}", self.line, self.col) + } +} + +#[derive(Debug, Clone, PartialEq)] +pub struct Program { + pub items: Vec, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum Item { + Import(Import), + Func(FuncDecl), + Struct(StructDecl), + Enum(EnumDecl), + Interface(InterfaceDecl), + Const(VarDecl), +} + +/// `import os`, `import a.b`, or `import c "SDL2/SDL.h"`. +#[derive(Debug, Clone, PartialEq)] +pub struct Import { + pub alias: Option, + pub path: String, + pub span: Span, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct TypeParam { + pub name: String, + pub constraint: Option, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct Param { + pub name: String, + pub ty: Type, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct FuncDecl { + pub is_pub: bool, + pub name: String, + pub type_params: Vec, + pub params: Vec, + pub ret: Option, + /// `None` for interface method signatures. + pub body: Option, + pub span: Span, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct Field { + pub is_pub: bool, + pub name: String, + pub ty: Type, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct StructDecl { + pub is_pub: bool, + pub name: String, + pub type_params: Vec, + pub fields: Vec, + pub methods: Vec, + pub span: Span, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct Variant { + pub name: String, + pub fields: Vec, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct EnumDecl { + pub is_pub: bool, + pub name: String, + pub type_params: Vec, + pub variants: Vec, + pub methods: Vec, + pub span: Span, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct InterfaceDecl { + pub is_pub: bool, + pub name: String, + pub type_params: Vec, + pub methods: Vec, + pub span: Span, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum Type { + /// `int`, `ui.Key`, `Stack[int]` + Named { path: Vec, args: Vec }, + /// `[]T` + Slice(Box), + /// `T?` + Optional(Box), + /// `shared T` + Shared(Box), + /// `weak T` + Weak(Box), + /// `(A, B)`, only for multiple return values + Tuple(Vec), + /// `func(A, B) R` + Func { params: Vec, ret: Option> }, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct Block { + pub stmts: Vec, + pub span: Span, +} + +/// `var x = 1`, `var a, b = f()`, `const N int = 5` +#[derive(Debug, Clone, PartialEq)] +pub struct VarDecl { + pub is_pub: bool, + pub is_const: bool, + pub names: Vec, + pub ty: Option, + pub value: Option, + pub span: Span, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum AssignOp { + Set, + Add, + Sub, + Mul, + Div, + Rem, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum ForKind { + /// `for { }` + Infinite, + /// `for cond { }` + While(Expr), + /// `for x in items { }`, `for i, x in items { }`, `for i in 0..10 step 2 { }` + In { vars: Vec, iter: Expr, step: Option }, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum Stmt { + Var(VarDecl), + Assign { target: Expr, op: AssignOp, value: Expr, span: Span }, + Expr(Expr), + If { cond: Expr, then: Block, else_: Option>, span: Span }, + For { kind: ForKind, body: Block, span: Span }, + Return { values: Vec, span: Span }, + Break(Span), + Continue(Span), + Block(Block), +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum UnOp { + Neg, + Not, +} + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BinOp { + Or, + And, + Eq, + Ne, + Lt, + Gt, + Le, + Ge, + Add, + Sub, + Mul, + Div, + Rem, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum StrPart { + Text(String), + Expr(Expr), +} + +#[derive(Debug, Clone, PartialEq)] +pub struct FieldInit { + /// `None` for positional initializers like `Vec2{1, 2}`. + pub name: Option, + pub value: Expr, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum Pattern { + Wildcard, + Int(i64), + Char(char), + Str(String), + Bool(bool), + Nil, + /// A bare name: binds a value, or names a variant without data. + Binding(String), + /// `ok(s)`, `Circle(r)` + Variant { name: String, args: Vec }, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum ArmBody { + Expr(Expr), + Block(Block), +} + +#[derive(Debug, Clone, PartialEq)] +pub struct MatchArm { + pub span: Span, + pub pattern: Pattern, + pub body: ArmBody, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct Expr { + pub kind: ExprKind, + pub span: Span, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum ExprKind { + Int(i64), + Float(f64), + Char(char), + Str(Vec), + Bool(bool), + Nil, + Ident(String), + Array(Vec), + /// `{"a": 1}` or `map[string, int]{}`: key/value pairs, with the types written out or left to inference + MapLit { types: Option<(Type, Type)>, entries: Vec<(Expr, Expr)> }, + StructLit { ty: Box, fields: Vec }, + Unary { op: UnOp, expr: Box }, + Binary { op: BinOp, lhs: Box, rhs: Box }, + /// `a..b`, `a..`, `..b` + Range { start: Option>, end: Option> }, + Call { callee: Box, args: Vec }, + Index { expr: Box, index: Box }, + /// A generic type with its types written out, before `{` or `.`: `Stack[int]{}`, `Option[int].None` + Generic { expr: Box, args: Vec }, + Field { expr: Box, name: String }, + /// `expr?`: return the error to the caller + Try(Box), + /// `shared expr` + Shared(Box), + /// `weak x`: a non-owning pointer to a shared value + Weak(Box), + Closure { params: Vec, ret: Option, body: Block }, + /// `x => x * 2`, `(a, b) => a + b`: the types come from where it is used + Lambda { params: Vec, body: Box }, + Match { scrutinee: Box, arms: Vec }, +} diff --git a/src/check.rs b/src/check.rs new file mode 100644 index 0000000..28c2c97 --- /dev/null +++ b/src/check.rs @@ -0,0 +1,3823 @@ +//! Type checker: resolves names, infers types, and lowers the syntax tree to HIR. + +use std::collections::{HashMap, HashSet}; + +use crate::ast::*; +use crate::error::{Diag, PResult}; +use crate::hir::*; +use crate::stdlib::{self, ModuleValue}; +use crate::types::{Num, Ty}; + +pub fn check(p: &Program) -> PResult { + Checker::new().program(p, &[]) +} + +/// The main file plus the files it imports (each after the files it imports). +pub fn check_project(main: &Program, modules: &[(String, Program)]) -> PResult { + Checker::new().program(main, modules) +} + +/// What an `import` name stands for. +#[derive(Clone, Debug)] +enum Target { + Builtin(&'static str), + /// A file of the user's; the full import path (`utils`, `a.b`). + User(String), +} + +const BUILTINS: &[&str] = &[ + "print", "int", "float", "char", "string", "error", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64", +]; + +/// The number type a conversion like `u8(x)` produces. +fn conversion_target(name: &str) -> Option { + match name { + "int" | "i64" => Some(Ty::Int), + "float" | "f64" => Some(Ty::Float), + "char" => Some(Ty::Char), + n => Num::ALL.iter().find(|k| k.name() == n).map(|k| Ty::Num(*k)), + } +} + +/// A generic function, checked again for every set of types it is used with. +#[derive(Clone)] +struct GenericFn { + decl: FuncDecl, + module: String, +} + +/// One use of a generic function that still needs its body checked. +struct Instance { + decl: FuncDecl, + module: String, + types: HashMap, + /// `Some(type)` for a method of a generic type. + owner: Option, +} + +/// A generic struct or enum, made into a real type for every list of types it is used with. +#[derive(Clone)] +enum GenericType { + Struct(StructDecl, String), + Enum(EnumDecl, String), +} + +/// The module part of a table name: `utils.Stack[m.P]` -> `utils`, `Stack[m.P]` -> ``. +fn module_of(key: &str) -> &str { + let head = key.split('[').next().unwrap(); + head.rsplit_once('.').map_or("", |x| x.0) +} + +#[derive(Clone)] +struct FuncSig { + params: Vec, + ret: Ty, +} + +/// Which cases of a `match` have been handled so far. +#[derive(Default)] +struct Cover { + variants: HashSet, + catch_all: bool, + t: bool, + f: bool, + ints: HashSet, + strs: HashSet, +} + +struct VariantInfo { + name: String, + fields: Vec<(String, Ty)>, +} + +struct EnumInfo { + name: &'static str, + variants: Vec, + methods: HashMap, + span: Span, +} + +struct StructInfo { + name: &'static str, + fields: Vec<(String, Ty)>, + methods: HashMap, + span: Span, +} + +struct Local { + id: u32, + ty: Ty, + is_const: bool, +} + +struct Checker { + funcs: HashMap, + /// Top-level constants: uses are replaced by their (call-free) value. + consts: HashMap, + structs: HashMap, + enums: HashMap, + /// The struct or enum whose method is being checked, if any. + cur_struct: Option<&'static str>, + /// Variables of the current function that can't be moved out by `return` + /// because they only borrow someone else's data: `self`, parameters, loop elements. + no_move: HashSet, + scopes: Vec>, + next_id: u32, + ret: Ty, + loops: u32, + /// Optional variables known not to be `nil` at this point (after `if x != nil`). + narrowed: HashSet, + /// `import os`: the built-in modules this file may use. + imports: HashMap, + /// The module being checked (`""` for the main file). Names of items in other + /// modules are `module.name`, in the tables above and in the HIR. + cur_mod: String, + all_imports: HashMap>, + /// Modules that exist as files. + known_modules: HashSet, + /// Items (`mod.name`) and methods (`mod.Type.name`) declared without `pub`. + private: HashSet, + generics: HashMap, + gtypes: HashMap, + ifaces: HashMap, + iface_decls: HashMap, + /// The rules of the type parameters of each generic function/type (by table name). + bounds_of: HashMap>>, + /// While checking a generic function: what its type names (`T`) stand for. + tsubst: HashMap, + inst_queue: Vec, +} + +// ---------- names used inside a function body (to find what a closure captures) ---------- + +fn idents_expr(e: &Expr, out: &mut HashSet) { + match &e.kind { + ExprKind::Ident(n) => { + out.insert(n.clone()); + } + ExprKind::Int(_) | ExprKind::Float(_) | ExprKind::Bool(_) | ExprKind::Char(_) | ExprKind::Nil => {} + ExprKind::Str(parts) => { + for p in parts { + if let StrPart::Expr(x) = p { + idents_expr(x, out); + } + } + } + ExprKind::Array(xs) => xs.iter().for_each(|x| idents_expr(x, out)), + ExprKind::MapLit { entries, .. } => entries.iter().for_each(|(k, v)| { + idents_expr(k, out); + idents_expr(v, out); + }), + ExprKind::StructLit { ty, fields } => { + idents_expr(ty, out); + fields.iter().for_each(|f| idents_expr(&f.value, out)); + } + ExprKind::Unary { expr, .. } + | ExprKind::Field { expr, .. } + | ExprKind::Generic { expr, .. } + | ExprKind::Try(expr) + | ExprKind::Shared(expr) + | ExprKind::Weak(expr) => idents_expr(expr, out), + ExprKind::Binary { lhs, rhs, .. } => { + idents_expr(lhs, out); + idents_expr(rhs, out); + } + ExprKind::Range { start, end } => { + start.iter().chain(end.iter()).for_each(|x| idents_expr(x, out)); + } + ExprKind::Call { callee, args } => { + idents_expr(callee, out); + args.iter().for_each(|x| idents_expr(x, out)); + } + ExprKind::Index { expr, index } => { + idents_expr(expr, out); + idents_expr(index, out); + } + ExprKind::Closure { body, .. } => idents_block(body, out), + ExprKind::Lambda { body, .. } => idents_expr(body, out), + ExprKind::Match { scrutinee, arms } => { + idents_expr(scrutinee, out); + for a in arms { + match &a.body { + ArmBody::Expr(x) => idents_expr(x, out), + ArmBody::Block(b) => idents_block(b, out), + } + } + } + } +} + +fn idents_block(b: &Block, out: &mut HashSet) { + b.stmts.iter().for_each(|s| idents_stmt(s, out)); +} + +fn idents_stmt(s: &Stmt, out: &mut HashSet) { + match s { + Stmt::Var(v) => { + if let Some(x) = &v.value { + idents_expr(x, out); + } + } + Stmt::Assign { target, value, .. } => { + idents_expr(target, out); + idents_expr(value, out); + } + Stmt::Expr(e) => idents_expr(e, out), + Stmt::If { cond, then, else_, .. } => { + idents_expr(cond, out); + idents_block(then, out); + if let Some(e) = else_ { + idents_stmt(e, out); + } + } + Stmt::For { kind, body, .. } => { + match kind { + ForKind::Infinite => {} + ForKind::While(c) => idents_expr(c, out), + ForKind::In { iter, step, .. } => { + idents_expr(iter, out); + step.iter().for_each(|x| idents_expr(x, out)); + } + } + idents_block(body, out); + } + Stmt::Return { values, .. } => values.iter().for_each(|x| idents_expr(x, out)), + Stmt::Break(_) | Stmt::Continue(_) => {} + Stmt::Block(b) => idents_block(b, out), + } +} + +enum ClosureBody<'a> { + Block(&'a Block), + Expr(&'a Expr), +} + +/// Using a `shared`/`weak` value as the thing inside it (`w.name`, `w.close()`, `match w { }`). +fn auto_deref(h: HExpr) -> HExpr { + match h.ty { + Ty::Shared(t) | Ty::Weak(t) => mk(HExprKind::Deref(Box::new(h)), *t), + _ => h, + } +} + +/// Does this place go through a shared value (so changing it doesn't need a changeable variable)? +fn through_shared(e: &HExpr) -> bool { + match &e.kind { + HExprKind::Deref(_) => true, + HExprKind::Field { base, .. } | HExprKind::Index { base, .. } | HExprKind::Unwrap(base) => through_shared(base), + _ => false, + } +} + +const BOUNDS: &[&str] = &["Ordered", "Number"]; + +/// A rule a type parameter must follow: `[T: Ordered]`. +#[derive(Clone, Debug)] +enum Bound { + Ordered, + Number, + /// A user `interface` (its table name). + Iface(String), +} + +/// An `interface`: the methods a type needs to have. +#[derive(Clone, Default)] +struct IfaceInfo { + /// (name, parameter types, result type) + methods: Vec<(String, Vec, Ty)>, +} + +fn mentions_type_param(t: &Type, tps: &[TypeParam]) -> bool { + match t { + Type::Named { path, args } => { + (path.len() == 1 && tps.iter().any(|p| p.name == path[0])) || args.iter().any(|a| mentions_type_param(a, tps)) + } + Type::Slice(x) | Type::Optional(x) | Type::Shared(x) | Type::Weak(x) => mentions_type_param(x, tps), + Type::Tuple(xs) => xs.iter().any(|x| mentions_type_param(x, tps)), + Type::Func { params, ret } => { + params.iter().any(|x| mentions_type_param(x, tps)) || ret.as_ref().map_or(false, |r| mentions_type_param(r, tps)) + } + } +} + +/// Match the written parameter type `pat` against the type of an argument, noting what each type parameter is. +fn unify(pat: &Type, ty: Ty, tps: &[TypeParam], found: &mut HashMap) -> Result<(), String> { + match (pat, ty) { + (Type::Named { path, args }, _) if path.len() == 1 && args.is_empty() && tps.iter().any(|p| p.name == path[0]) => { + match found.get(&path[0]) { + Some(prev) if *prev != ty => Err(format!( + "`{}` is `{prev}` from an earlier argument, but this one is `{ty}`", + path[0] + )), + Some(_) => Ok(()), + None => { + found.insert(path[0].clone(), ty); + Ok(()) + } + } + } + (Type::Slice(inner), Ty::List(e)) => unify(inner, *e, tps, found), + (Type::Optional(inner), Ty::Opt(e)) => unify(inner, *e, tps, found), + (Type::Func { params, ret }, Ty::Func(ft)) => { + for (p, t) in params.iter().zip(&ft.params) { + unify(p, *t, tps, found)?; + } + match ret { + Some(r) => unify(r, ft.ret, tps, found), + None => Ok(()), + } + } + _ => Ok(()), + } +} + +fn unsupported(span: Span, what: &str) -> PResult { + Err(Diag::new(format!("{what} is not supported yet"), span)) +} + +fn op_str(op: BinOp) -> &'static str { + match op { + BinOp::Or => "||", + BinOp::And => "&&", + BinOp::Eq => "==", + BinOp::Ne => "!=", + BinOp::Lt => "<", + BinOp::Gt => ">", + BinOp::Le => "<=", + BinOp::Ge => ">=", + BinOp::Add => "+", + BinOp::Sub => "-", + BinOp::Mul => "*", + BinOp::Div => "/", + BinOp::Rem => "%", + } +} + +fn mk(kind: HExprKind, ty: Ty) -> HExpr { + HExpr { kind, ty } +} + +/// An integer literal (possibly negated) that has not been given a type yet. +fn is_int_lit(h: &HExpr) -> bool { + h.ty == Ty::Int + && match &h.kind { + HExprKind::Int(_) => true, + HExprKind::Unary { op: UnOp::Neg, expr } => is_int_lit(expr), + _ => false, + } +} + +/// A float literal (possibly negated) that has not been given a type yet. +fn is_float_lit(h: &HExpr) -> bool { + h.ty == Ty::Float + && match &h.kind { + HExprKind::Float(_) => true, + HExprKind::Unary { op: UnOp::Neg, expr } => is_float_lit(expr), + _ => false, + } +} + +/// Give a number literal the type the context asks for (`var x: u8 = 200`, `f * 2`). +fn retype_lit(h: HExpr, want: Ty, span: Span, negated: bool) -> PResult { + match h.kind { + HExprKind::Int(n) => { + if want.is_float() { + return Ok(mk(HExprKind::Float(n as f64), want)); + } + let v = if negated { -(n as i128) } else { n as i128 }; + let (lo, hi) = want.int_range(); + if v < lo || v > hi { + return Err(Diag::new(format!("{v} doesn't fit in a `{want}` (it holds {lo} to {hi})"), span)); + } + Ok(mk(HExprKind::Int(n), want)) + } + HExprKind::Float(x) => Ok(mk(HExprKind::Float(x), want)), + HExprKind::Unary { op, expr } => { + let inner = retype_lit(*expr, want, span, true)?; + Ok(mk(HExprKind::Unary { op, expr: Box::new(inner) }, want)) + } + _ => Ok(h), + } +} + +/// True if the expression calls user code or prints (not allowed in a `const`). +fn has_call(h: &HExpr) -> bool { + match &h.kind { + HExprKind::Call { .. } + | HExprKind::MethodCall { .. } + | HExprKind::Print(_) + | HExprKind::ListPush { .. } + | HExprKind::ListPop(_) + | HExprKind::MapCall { .. } + | HExprKind::Builtin { .. } + | HExprKind::CallValue { .. } + | HExprKind::IfaceCall { .. } + | HExprKind::Closure { .. } + | HExprKind::Try(_) => true, + HExprKind::Copy(e) + | HExprKind::Move(e) + | HExprKind::NewShared(e) + | HExprKind::Upgrade(e) + | HExprKind::Downgrade(e) + | HExprKind::Deref(e) + | HExprKind::Len(e) + | HExprKind::Wrap(e) + | HExprKind::MakeError(e) + | HExprKind::IsSet(e) + | HExprKind::Unwrap(e) => has_call(e), + HExprKind::TupleLit(items) => items.iter().any(has_call), + HExprKind::EnumLit { fields, .. } => fields.iter().any(has_call), + HExprKind::Match { .. } => true, + HExprKind::ListLit { elems } => elems.iter().any(has_call), + HExprKind::MapLit { entries } => entries.iter().any(|(k, v)| has_call(k) || has_call(v)), + HExprKind::Index { base, index } => has_call(base) || has_call(index), + HExprKind::Slice { base, lo, hi } => { + has_call(base) || lo.as_deref().map_or(false, has_call) || hi.as_deref().map_or(false, has_call) + } + HExprKind::Unary { expr, .. } | HExprKind::Convert(expr) => has_call(expr), + HExprKind::Field { base, .. } => has_call(base), + HExprKind::StructLit { fields, .. } => fields.iter().any(|(_, f)| has_call(f)), + HExprKind::Binary { lhs, rhs, .. } => has_call(lhs) || has_call(rhs), + HExprKind::Interp(parts) => parts.iter().any(|p| match p { + HInterp::Value(e) => has_call(e), + HInterp::Text(_) => false, + }), + _ => false, + } +} + +/// Does every path through these statements end in a `return` (or loop forever)? +fn always_returns(stmts: &[HStmt]) -> bool { + match stmts.last() { + Some(HStmt::Return(_)) => true, + Some(HStmt::If { then, else_, .. }) => always_returns(then) && always_returns(else_), + Some(HStmt::Block(b)) => always_returns(b), + Some(HStmt::Loop(body)) => !has_break(body), + // a `match` is exhaustive, so if every arm returns, so does the match + Some(HStmt::Expr(HExpr { kind: HExprKind::Match { arms, .. }, .. })) => arms.iter().all(|a| match &a.body { + HArmBody::Block(b) => always_returns(b), + HArmBody::Expr(_) => false, + }), + _ => false, + } +} + +/// Does control never reach the statement after these (they end in return/break/continue)? +fn diverges(stmts: &[HStmt]) -> bool { + match stmts.last() { + Some(HStmt::Return(_) | HStmt::Break | HStmt::Continue) => true, + Some(HStmt::If { then, else_, .. }) => diverges(then) && diverges(else_), + Some(HStmt::Block(b)) => diverges(b), + _ => always_returns(stmts), + } +} + +/// Is there a `break` that belongs to the loop whose body this is? +fn has_break(stmts: &[HStmt]) -> bool { + stmts.iter().any(|s| match s { + HStmt::Break => true, + HStmt::If { then, else_, .. } => has_break(then) || has_break(else_), + HStmt::Block(b) => has_break(b), + HStmt::Expr(HExpr { kind: HExprKind::Match { arms, .. }, .. }) => arms.iter().any(|a| match &a.body { + HArmBody::Block(b) => has_break(b), + HArmBody::Expr(_) => false, + }), + // breaks inside nested loops belong to those loops + _ => false, + }) +} + + +impl Checker { + fn new() -> Checker { + Checker { + funcs: HashMap::new(), + consts: HashMap::new(), + structs: HashMap::new(), + enums: HashMap::new(), + cur_struct: None, + no_move: HashSet::new(), + scopes: Vec::new(), + next_id: 0, + ret: Ty::Unit, + loops: 0, + narrowed: HashSet::new(), + imports: HashMap::new(), + cur_mod: String::new(), + all_imports: HashMap::new(), + known_modules: HashSet::new(), + private: HashSet::new(), + generics: HashMap::new(), + gtypes: HashMap::new(), + ifaces: HashMap::new(), + iface_decls: HashMap::new(), + bounds_of: HashMap::new(), + tsubst: HashMap::new(), + inst_queue: Vec::new(), + } + } + + // ---------- program ---------- + + fn program(&mut self, main: &Program, modules: &[(String, Program)]) -> PResult { + let mut mods: Vec<(&str, &Program)> = modules.iter().map(|(k, p)| (k.as_str(), p)).collect(); + mods.push(("", main)); + self.known_modules = modules.iter().map(|(k, _)| k.clone()).collect(); + + for (key, p) in &mods { + self.enter(key); + self.phase_imports(p).map_err(|d| d.in_module(key))?; + } + for (key, p) in &mods { + self.enter(key); + self.phase_type_names(p).map_err(|d| d.in_module(key))?; + } + for (key, p) in &mods { + self.enter(key); + self.phase_type_fields(p).map_err(|d| d.in_module(key))?; + } + for (key, p) in &mods { + self.enter(key); + self.phase_ifaces(p).map_err(|d| d.in_module(key))?; + } + for (key, p) in &mods { + self.enter(key); + self.phase_signatures(p).map_err(|d| d.in_module(key))?; + } + self.enter(""); + self.check_main(main)?; + for (key, p) in &mods { + self.enter(key); + self.phase_consts(p).map_err(|d| d.in_module(key))?; + } + let mut funcs = Vec::new(); + for (key, p) in &mods { + self.enter(key); + self.phase_bodies(p, &mut funcs).map_err(|d| d.in_module(key))?; + } + + // generic functions, once for each set of types they were used with + while let Some(inst) = self.inst_queue.pop() { + self.enter(&inst.module); + self.tsubst = inst.types; + let r = self.func(&inst.decl, inst.owner.as_deref()); + self.tsubst.clear(); + funcs.push(r.map_err(|d| d.in_module(&inst.module))?); + } + + let type_order = self.type_order()?; + + // which methods change `self`? and are they only called where that is allowed? + infer_mutation(&mut funcs); + check_mutating_calls(&funcs)?; + for f in &mut funcs { + crate::moves::apply(&mut f.body); + } + + let types = type_order + .into_iter() + .map(|name| match self.structs.get(name) { + Some(info) => HTypeDef::Struct(HStruct { name: name.to_string(), fields: info.fields.clone() }), + None => HTypeDef::Enum(HEnum { + name: name.to_string(), + variants: self.enums[name] + .variants + .iter() + .map(|v| HVariant { name: v.name.clone(), fields: v.fields.clone() }) + .collect(), + }), + }) + .collect(); + let mut ifaces: Vec<(String, Vec<(String, Vec, Ty)>)> = + self.ifaces.iter().map(|(k, i)| (k.clone(), i.methods.clone())).collect(); + ifaces.sort_by(|a, b| a.0.cmp(&b.0)); + Ok(HProgram { types, funcs, ifaces }) + } + + /// Start checking the module `key`. + fn enter(&mut self, key: &str) { + self.cur_mod = key.to_string(); + self.imports = self.all_imports.get(key).cloned().unwrap_or_default(); + } + + /// The table name of a top-level item written as `name` in the current module. + fn q(&self, name: &str) -> String { + if self.cur_mod.is_empty() { + name.to_string() + } else { + format!("{}.{}", self.cur_mod, name) + } + } + + fn mark_private(&mut self, key: &str, is_pub: bool) { + if !is_pub { + self.private.insert(key.to_string()); + } + } + + /// Using `module.thing` from another module needs `pub`. + fn check_visible(&self, key: &str, span: Span) -> PResult<()> { + let module = module_of(key); + if module != self.cur_mod && self.private.contains(key) { + let short = key.rsplit('.').next().unwrap(); + return Err(Diag::new( + format!("`{short}` is private to `{module}`; write `pub` in front of it in {module}.dino to share it"), + span, + )); + } + Ok(()) + } + + fn phase_imports(&mut self, p: &Program) -> PResult<()> { + for item in &p.items { + match item { + Item::Import(i) => self.import(i)?, + _ => {} + } + } + self.all_imports.insert(self.cur_mod.clone(), self.imports.clone()); + Ok(()) + } + + /// Type names, so structs and enums can mention each other in any order. + fn phase_type_names(&mut self, p: &Program) -> PResult<()> { + for item in &p.items { + if let Item::Interface(i) = item { + if !i.type_params.is_empty() { + return unsupported(i.span, "generic interfaces"); + } + self.check_type_name_free(&i.name, i.span)?; + let key = self.q(&i.name); + self.mark_private(&key, i.is_pub); + self.ifaces.insert(key.clone(), IfaceInfo::default()); + self.iface_decls.insert(key, (i.clone(), self.cur_mod.clone())); + } + } + for item in &p.items { + if let Item::Enum(e) = item { + if !e.type_params.is_empty() { + self.declare_generic_type(&e.name, e.is_pub, e.span, &e.type_params)?; + let key = self.q(&e.name); + self.gtypes.insert(key, GenericType::Enum(e.clone(), self.cur_mod.clone())); + continue; + } + if e.variants.is_empty() { + return Err(Diag::new(format!("enum `{}` needs at least one case", e.name), e.span)); + } + self.check_type_name_free(&e.name, e.span)?; + let key = self.q(&e.name); + self.mark_private(&key, e.is_pub); + let name: &'static str = Box::leak(key.clone().into_boxed_str()); + self.enums.insert(key, EnumInfo { name, variants: Vec::new(), methods: HashMap::new(), span: e.span }); + } + if let Item::Struct(s) = item { + if !s.type_params.is_empty() { + self.declare_generic_type(&s.name, s.is_pub, s.span, &s.type_params)?; + let key = self.q(&s.name); + self.gtypes.insert(key, GenericType::Struct(s.clone(), self.cur_mod.clone())); + continue; + } + self.check_type_name_free(&s.name, s.span)?; + let key = self.q(&s.name); + self.mark_private(&key, s.is_pub); + let name: &'static str = Box::leak(key.clone().into_boxed_str()); + self.structs.insert(key, StructInfo { name, fields: Vec::new(), methods: HashMap::new(), span: s.span }); + } + } + Ok(()) + } + + /// Fields of structs, data of enum cases. + fn phase_type_fields(&mut self, p: &Program) -> PResult<()> { + for item in &p.items { + if let Item::Enum(e) = item { + if !e.type_params.is_empty() { + continue; + } + let mut variants: Vec = Vec::new(); + for v in &e.variants { + if variants.iter().any(|x| x.name == v.name) { + return Err(Diag::new( + format!("enum `{}` has two cases named `{}`", e.name, v.name), + e.span, + )); + } + let mut fields: Vec<(String, Ty)> = Vec::new(); + for f in &v.fields { + if fields.iter().any(|(n, _)| *n == f.name) { + return Err(Diag::new( + format!("case `{}.{}` has two values named `{}`", e.name, v.name, f.name), + e.span, + )); + } + let fty = self.resolve_type(&f.ty, e.span)?; + if matches!(fty, Ty::Opt(_)) { + return unsupported(e.span, "an optional (`T?`) as the data of an enum case"); + } + fields.push((f.name.clone(), fty)); + } + variants.push(VariantInfo { name: v.name.clone(), fields }); + } + { let k = self.q(&e.name); self.enums.get_mut(&k).unwrap() }.variants = variants; + } + if let Item::Struct(s) = item { + if !s.type_params.is_empty() { + continue; + } + let mut fields: Vec<(String, Ty)> = Vec::new(); + for f in &s.fields { + if fields.iter().any(|(n, _)| *n == f.name) { + return Err(Diag::new( + format!("struct `{}` has two fields named `{}`", s.name, f.name), + s.span, + )); + } + let fty = self.resolve_type(&f.ty, s.span)?; + if matches!(fty, Ty::Opt(_)) { + return unsupported(s.span, "an optional (`T?`) as a struct field"); + } + let fkey = format!("{}.{}", self.q(&s.name), f.name); + self.mark_private(&fkey, f.is_pub); + fields.push((f.name.clone(), fty)); + } + { let k = self.q(&s.name); self.structs.get_mut(&k).unwrap() }.fields = fields; + } + } + Ok(()) + } + + /// Function and method signatures, so everything can call everything. + fn phase_signatures(&mut self, p: &Program) -> PResult<()> { + for item in &p.items { + match item { + Item::Struct(s) if s.type_params.is_empty() => { + let key = self.q(&s.name); + self.declare_methods(&key, &s.methods)? + } + Item::Enum(e) if e.type_params.is_empty() => { + let key = self.q(&e.name); + self.declare_methods(&key, &e.methods)? + } + _ => {} + } + } + for item in &p.items { + if let Item::Func(f) = item { + self.declare_func(f)?; + } + } + Ok(()) + } + + /// The methods each `interface` asks for. + fn phase_ifaces(&mut self, p: &Program) -> PResult<()> { + for item in &p.items { + let Item::Interface(i) = item else { continue }; + let mut methods: Vec<(String, Vec, Ty)> = Vec::new(); + for m in &i.methods { + if methods.iter().any(|(n, _, _)| *n == m.name) { + return Err(Diag::new(format!("interface `{}` lists `{}` twice", i.name, m.name), m.span)); + } + let sig = self.sig_of(m)?; + methods.push((m.name.clone(), sig.params, sig.ret)); + } + let key = self.q(&i.name); + self.ifaces.insert(key, IfaceInfo { methods }); + } + Ok(()) + } + + /// What a rule written after `T:` means. + fn resolve_bound(&self, c: &Type, who: &str, span: Span) -> PResult { + if let Type::Named { path, args } = c { + if args.is_empty() && path.len() == 1 { + match path[0].as_str() { + "Ordered" => return Ok(Bound::Ordered), + "Number" => return Ok(Bound::Number), + n => { + let key = self.q(n); + if self.ifaces.contains_key(&key) { + return Ok(Bound::Iface(key)); + } + } + } + } + if args.is_empty() && path.len() == 2 { + if let Some(Target::User(m)) = self.imports.get(&path[0]) { + let key = format!("{m}.{}", path[1]); + if self.ifaces.contains_key(&key) { + self.check_visible(&key, span)?; + return Ok(Bound::Iface(key)); + } + } + } + } + Err(Diag::new( + format!("unknown rule for `{who}`; the rules are: {} or an `interface` you declared", BOUNDS.join(", ")), + span, + )) + } + + fn resolve_bounds(&self, tps: &[TypeParam], span: Span) -> PResult>> { + tps.iter() + .map(|tp| tp.constraint.as_ref().map(|c| self.resolve_bound(c, &tp.name, span)).transpose()) + .collect() + } + + /// `Some((what is needed, why this type doesn't have it))` if `ty` breaks the rule. + fn bound_failure(&self, b: &Bound, ty: Ty) -> Option<(String, String)> { + match b { + Bound::Ordered if !(ty.is_number() || ty == Ty::Str || ty == Ty::Char) => { + Some(("a number or a string (something with an order)".into(), format!("it is `{ty}`"))) + } + Bound::Number if !ty.is_number() => Some(("a number".into(), format!("it is `{ty}`"))), + Bound::Iface(key) => { + let short = key.rsplit('.').next().unwrap(); + let what = format!("something with the methods of `{short}`"); + if let Ty::Iface(k2) = ty { + for (name, params, ret) in &self.ifaces[key].methods { + let ok = self.ifaces[k2].methods.iter().any(|(n, p, r)| n == name && p == params && r == ret); + if !ok { + return Some((what, format!("`{ty}` has no method `{name}` like that"))); + } + } + return None; + } + let methods = match ty { + Ty::Struct(n) | Ty::Enum(n) => self.methods_of(n), + _ => None, + }; + for (name, params, ret) in &self.ifaces[key].methods { + let sig = methods.and_then(|m| m.get(name)); + match sig { + None => return Some((what, format!("`{ty}` has no method `{name}`"))), + Some(sig) if sig.params != *params || sig.ret != *ret => { + let want = format!( + "{name}({}){}", + params.iter().map(|t| t.to_string()).collect::>().join(", "), + if *ret == Ty::Unit { String::new() } else { format!(": {ret}") } + ); + let has = format!( + "{name}({}){}", + sig.params.iter().map(|t| t.to_string()).collect::>().join(", "), + if sig.ret == Ty::Unit { String::new() } else { format!(": {}", sig.ret) } + ); + return Some((what, format!("`{ty}` has `{has}`, but `{short}` needs `{want}`"))); + } + _ => {} + } + } + None + } + _ => None, + } + } + + fn check_main(&self, p: &Program) -> PResult<()> { + match self.funcs.get("main") { + None => { + return Err(Diag::new( + "there is no `main` function; a program starts at `func main() { }`", + Span { line: 1, col: 1 }, + )) + } + Some(sig) if !sig.params.is_empty() || !matches!(sig.ret, Ty::Unit | Ty::Error) => { + let span = p + .items + .iter() + .find_map(|i| match i { + Item::Func(f) if f.name == "main" => Some(f.span), + _ => None, + }) + .unwrap(); + return Err(Diag::new("`main` must take no parameters and return nothing (or just an `error`)", span)); + } + _ => {} + } + Ok(()) + } + + /// Top-level constants, in order. + fn phase_consts(&mut self, p: &Program) -> PResult<()> { + for item in &p.items { + if let Item::Const(v) = item { + self.top_const(v)?; + } + } + Ok(()) + } + + fn phase_bodies(&mut self, p: &Program, funcs: &mut Vec) -> PResult<()> { + for item in &p.items { + match item { + Item::Func(f) if !f.type_params.is_empty() => {} + Item::Func(f) => funcs.push(self.func(f, None)?), + Item::Struct(s) if s.type_params.is_empty() => { + let key = self.q(&s.name); + for m in &s.methods { + funcs.push(self.func(m, Some(&key))?); + } + } + Item::Enum(e) if e.type_params.is_empty() => { + let key = self.q(&e.name); + for m in &e.methods { + funcs.push(self.func(m, Some(&key))?); + } + } + _ => {} + } + } + Ok(()) + } + + /// Structs and enums sorted so that every type comes after the types it contains by value. + fn type_order(&self) -> PResult> { + #[derive(Clone, Copy, PartialEq)] + enum Mark { + Visiting, + Done, + } + fn deps(c: &Checker, name: &str) -> Vec<&'static str> { + let tys: Vec = match c.structs.get(name) { + Some(s) => s.fields.iter().map(|(_, t)| *t).collect(), + None => c.enums[name].variants.iter().flat_map(|v| v.fields.iter().map(|(_, t)| *t)).collect(), + }; + tys.into_iter() + .filter_map(|t| match t { + Ty::Struct(n) | Ty::Enum(n) => Some(n), + _ => None, + }) + .collect() + } + fn span_of(c: &Checker, name: &str) -> Span { + c.structs.get(name).map(|s| s.span).unwrap_or_else(|| c.enums[name].span) + } + fn visit( + c: &Checker, + name: &'static str, + marks: &mut HashMap<&'static str, Mark>, + out: &mut Vec<&'static str>, + ) -> PResult<()> { + match marks.get(name) { + Some(Mark::Done) => return Ok(()), + Some(Mark::Visiting) => { + let kind = if c.structs.contains_key(name) { "struct" } else { "enum" }; + return Err(Diag::new( + format!( + "{kind} `{name}` contains itself, so it would be infinitely big \ + (a list like `[]{name}` can hold itself; `shared {name}` will too, once it exists)" + ), + span_of(c, name), + )); + } + None => {} + } + marks.insert(name, Mark::Visiting); + for dep in deps(c, name) { + visit(c, dep, marks, out)?; + } + marks.insert(name, Mark::Done); + out.push(name); + Ok(()) + } + let mut all: Vec<(Span, &'static str)> = Vec::new(); + all.extend(self.structs.values().map(|i| (i.span, i.name))); + all.extend(self.enums.values().map(|i| (i.span, i.name))); + all.sort_by_key(|(sp, name)| (sp.line, sp.col, *name)); + let mut marks = HashMap::new(); + let mut out = Vec::new(); + for (_, name) in all { + visit(self, name, &mut marks, &mut out) + .map_err(|d| d.in_module(module_of(name)))?; + } + Ok(out) + } + + fn check_type_name_free(&self, name: &str, span: Span) -> PResult<()> { + if matches!(name, "bool" | "string") || conversion_target(name).is_some() { + return Err(Diag::new(format!("`{name}` is a built-in type and can't be redefined"), span)); + } + let key = self.q(name); + if self.structs.contains_key(&key) + || self.ifaces.contains_key(&key) + || self.enums.contains_key(&key) + || self.funcs.contains_key(&key) + || self.generics.contains_key(&key) + || self.gtypes.contains_key(&key) + || self.consts.contains_key(&key) + { + return Err(Diag::new(format!("`{name}` is already defined"), span)); + } + Ok(()) + } + + fn import(&mut self, i: &Import) -> PResult<()> { + if i.alias.is_some() { + return unsupported(i.span, "importing a C library"); + } + let alias = i.path.rsplit('.').next().unwrap().to_string(); + if self.imports.contains_key(&alias) { + return Err(Diag::new(format!("`{alias}` is imported twice"), i.span)); + } + let target = match stdlib::MODULES.iter().find(|m| **m == i.path) { + Some(m) => Target::Builtin(m), + None if self.known_modules.contains(&i.path) => Target::User(i.path.clone()), + None => { + return Err(Diag::new( + format!("there is no module `{}` (built-in modules: {})", i.path, stdlib::MODULES.join(", ")), + i.span, + )) + } + }; + self.imports.insert(alias, target); + Ok(()) + } + + /// Is `e` the name of an imported module, not hidden by a variable? + fn alias_target(&self, e: &Expr) -> Option<&Target> { + let ExprKind::Ident(n) = &e.kind else { return None }; + if self.lookup(n).is_some() || self.implicit_field(n).is_some() || self.consts.contains_key(&self.q(n)) { + return None; + } + self.imports.get(n) + } + + /// If `e` is the name of an imported file of the user's, its import path. + fn user_alias(&self, e: &Expr) -> Option { + match self.alias_target(e)? { + Target::User(p) => Some(p.clone()), + Target::Builtin(_) => None, + } + } + + /// The table name for a type or function written as `name` or `module.name`. + fn global_key(&self, e: &Expr) -> Option { + match &e.kind { + ExprKind::Ident(n) => Some(self.q(n)), + ExprKind::Field { expr, name } => Some(format!("{}.{}", self.user_alias(expr)?, name)), + _ => None, + } + } + + /// If `e` is the name of an imported built-in module (and no variable hides it), that module. + fn module_alias(&self, e: &Expr) -> Option<&'static str> { + match self.alias_target(e)? { + Target::Builtin(m) => Some(m), + Target::User(_) => None, + } + } + + fn module_value(&mut self, module: &'static str, name: &str, span: Span) -> PResult { + match stdlib::module_value(module, name) { + Some(ModuleValue::Float(x)) => Ok(mk(HExprKind::Float(x), Ty::Float)), + Some(ModuleValue::Call(sig)) => { + Ok(mk(HExprKind::Builtin { c: sig.c, shape: sig.shape, args: Vec::new() }, sig.ret)) + } + None if stdlib::module_fn(module, name).is_some() => { + Err(Diag::new(format!("`{module}.{name}` is a function; call it like `{module}.{name}(...)`"), span)) + } + None => Err(Diag::new( + format!("module `{module}` has no `{name}` (it has: {})", stdlib::module_names(module).join(", ")), + span, + )), + } + } + + fn module_call(&mut self, module: &'static str, name: &str, args: &[Expr], span: Span, name_span: Span) -> PResult { + let Some(sig) = stdlib::module_fn(module, name) else { + return Err(Diag::new( + format!("module `{module}` has no function `{name}` (it has: {})", stdlib::module_names(module).join(", ")), + name_span, + )); + }; + let label = format!("{module}.{name}"); + use crate::hir::BuiltinShape::*; + match sig.shape { + Min | Max => { + if args.len() != 2 { + return Err(Diag::new(format!("`{label}` takes 2 arguments, but {} were given", args.len()), span)); + } + let mut l = self.expr(&args[0], None)?; + let mut r = self.expr(&args[1], None)?; + if l.ty != r.ty { + if is_int_lit(&l) && r.ty.is_number() || is_float_lit(&l) && r.ty.is_float() { + l = retype_lit(l, r.ty, span, false)?; + } else if is_int_lit(&r) && l.ty.is_number() || is_float_lit(&r) && r.ty.is_float() { + r = retype_lit(r, l.ty, span, false)?; + } + } + if l.ty != r.ty || !l.ty.is_number() { + return Err(Diag::new( + format!("`{label}` needs two numbers of the same type, found `{}` and `{}`", l.ty, r.ty), + span, + )); + } + let ty = l.ty; + Ok(mk(HExprKind::Builtin { c: "", shape: sig.shape, args: vec![l, r] }, ty)) + } + Abs => { + if args.len() != 1 { + return Err(Diag::new(format!("`{label}` takes 1 argument, but {} were given", args.len()), span)); + } + let x = self.expr(&args[0], None)?; + if !x.ty.is_number() || !x.ty.is_signed() { + return Err(Diag::new(format!("`{label}` needs a signed number, found `{}`", x.ty), span)); + } + let ty = x.ty; + Ok(mk(HExprKind::Builtin { c: "", shape: Abs, args: vec![x] }, ty)) + } + _ => self.builtin_call(&label, sig, None, args, span), + } + } + + /// A call of a function from the runtime. `recv` is the string or list for a method. + fn builtin_call( + &mut self, + label: &str, + sig: stdlib::BuiltinFn, + recv: Option, + args: &[Expr], + span: Span, + ) -> PResult { + let skip = recv.is_some() as usize; + let want = sig.params.len() - skip; + if args.len() != want { + return Err(Diag::new( + format!( + "`{label}` takes {want} argument{}, but {} {} given", + if want == 1 { "" } else { "s" }, + args.len(), + if args.len() == 1 { "was" } else { "were" } + ), + span, + )); + } + let mut hs: Vec = recv.into_iter().collect(); + for (a, ty) in args.iter().zip(&sig.params[skip..]) { + hs.push(self.expr_as(a, *ty)?); + } + Ok(mk(HExprKind::Builtin { c: sig.c, shape: sig.shape, args: hs }, sig.ret)) + } + + fn check_name_free(&self, name: &str, span: Span) -> PResult<()> { + if self.imports.contains_key(name) { + return Err(Diag::new(format!("`{name}` is an imported module and can't be redefined"), span)); + } + if BUILTINS.contains(&name) { + return Err(Diag::new(format!("`{name}` is built in and can't be redefined"), span)); + } + let key = self.q(name); + if self.funcs.contains_key(&key) + || self.ifaces.contains_key(&key) + || self.generics.contains_key(&key) + || self.gtypes.contains_key(&key) + || self.consts.contains_key(&key) + || self.structs.contains_key(&key) + || self.enums.contains_key(&key) + { + return Err(Diag::new(format!("`{name}` is already defined"), span)); + } + Ok(()) + } + + fn sig_of(&mut self, f: &FuncDecl) -> PResult { + let mut params = Vec::new(); + for p in &f.params { + params.push(self.resolve_type(&p.ty, f.span)?); + } + let ret = match &f.ret { + None => Ty::Unit, + Some(Type::Tuple(items)) => { + let mut tys = Vec::new(); + for t in items { + let ty = self.resolve_type(t, f.span)?; + if matches!(ty, Ty::Opt(_)) { + return unsupported(f.span, "an optional (`T?`) inside several return values"); + } + tys.push(ty); + } + Ty::tuple(tys) + } + Some(t) => self.resolve_type(t, f.span)?, + }; + Ok(FuncSig { params, ret }) + } + + fn declare_func(&mut self, f: &FuncDecl) -> PResult<()> { + self.check_name_free(&f.name, f.span)?; + if !f.type_params.is_empty() { + for (i, tp) in f.type_params.iter().enumerate() { + if f.type_params[..i].iter().any(|o| o.name == tp.name) { + return Err(Diag::new(format!("two type parameters are named `{}`", tp.name), f.span)); + } + } + let bounds = self.resolve_bounds(&f.type_params, f.span)?; + self.bounds_of.insert(self.q(&f.name), bounds); + if !f.params.iter().any(|p| mentions_type_param(&p.ty, &f.type_params)) { + return Err(Diag::new( + "a generic function needs at least one parameter that uses its type (the type is worked out from the arguments)", + f.span, + )); + } + let key = self.q(&f.name); + self.mark_private(&key, f.is_pub); + self.generics.insert(key, GenericFn { decl: f.clone(), module: self.cur_mod.clone() }); + return Ok(()); + } + let sig = self.sig_of(f)?; + let key = self.q(&f.name); + self.mark_private(&key, f.is_pub); + self.funcs.insert(key, sig); + Ok(()) + } + + fn declare_methods(&mut self, owner: &str, methods: &[FuncDecl]) -> PResult<()> { + for m in methods { + let field_clash = self.structs.get(owner).map_or(false, |i| i.fields.iter().any(|(n, _)| *n == m.name)); + if field_clash { + return Err(Diag::new( + format!("`{}` is already a field of `{owner}`, so it can't also be a method", m.name), + m.span, + )); + } + if self.methods_of(owner).unwrap().contains_key(&m.name) { + return Err(Diag::new(format!("`{owner}` has two methods named `{}`", m.name), m.span)); + } + if m.params.iter().any(|p| p.name == "self") { + return Err(Diag::new("`self` is added automatically; don't list it as a parameter", m.span)); + } + let sig = self.sig_of(m)?; + self.mark_private(&format!("{owner}.{}", m.name), m.is_pub); + match self.structs.get_mut(owner) { + Some(i) => i.methods.insert(m.name.clone(), sig), + None => self.enums.get_mut(owner).unwrap().methods.insert(m.name.clone(), sig), + }; + } + Ok(()) + } + + fn methods_of(&self, owner: &str) -> Option<&HashMap> { + match self.structs.get(owner) { + Some(i) => Some(&i.methods), + None => self.enums.get(owner).map(|i| &i.methods), + } + } + + /// Interned name of a struct or enum. + fn type_name(&self, name: &str) -> Option<&'static str> { + self.structs.get(name).map(|i| i.name).or_else(|| self.enums.get(name).map(|i| i.name)) + } + + fn resolve_type(&mut self, t: &Type, span: Span) -> PResult { + match t { + Type::Named { path, args } if path.len() == 1 && args.is_empty() => match path[0].as_str() { + "int" => Ok(Ty::Int), + "float" => Ok(Ty::Float), + "bool" => Ok(Ty::Bool), + "char" => Ok(Ty::Char), + "string" => Ok(Ty::Str), + "error" => Ok(Ty::Error), + "i64" => Ok(Ty::Int), + "f64" => Ok(Ty::Float), + n if Num::ALL.iter().any(|k| k.name() == n) => Ok(conversion_target(n).unwrap()), + n if self.tsubst.contains_key(n) => Ok(self.tsubst[n]), + n => { + let key = self.q(n); + if self.ifaces.contains_key(&key) { + return Ok(Ty::Iface(Box::leak(key.into_boxed_str()))); + } + if self.gtypes.contains_key(&key) { + return Err(Diag::new(format!("`{n}` needs types: write `{n}[int]` (or whichever types you want)"), span)); + } + match (self.structs.get(&key), self.enums.get(&key)) { + (Some(info), _) => Ok(Ty::Struct(info.name)), + (_, Some(info)) => Ok(Ty::Enum(info.name)), + _ => Err(Diag::new(format!("unknown type `{n}`"), span)), + } + } + }, + Type::Named { path, args } if path.len() == 1 && path[0] == "map" && !self.gtypes.contains_key(&self.q("map")) => { + if args.len() != 2 { + return Err(Diag::new("a map type needs a key type and a value type: `map[string, int]`", span)); + } + let key = self.resolve_type(&args[0], span)?; + let val = self.resolve_type(&args[1], span)?; + if !(matches!(key, Ty::Int | Ty::Str | Ty::Bool | Ty::Char) || matches!(key, Ty::Num(n) if !n.is_float())) { + return Err(Diag::new( + format!("a map key must be an `int`, `string`, `bool`, `char` or other whole-number type, not `{key}`"), + span, + )); + } + if matches!(val, Ty::Opt(_)) { + return unsupported(span, "a map of optionals (`map[K, V?]`)"); + } + Ok(Ty::map(key, val)) + } + Type::Named { path, args } if !args.is_empty() && path.len() <= 2 => { + let key = if path.len() == 1 { + self.q(&path[0]) + } else { + let Some(Target::User(module)) = self.imports.get(&path[0]) else { + return Err(Diag::new(format!("unknown type `{}.{}` (is `{}` imported?)", path[0], path[1], path[0]), span)); + }; + format!("{module}.{}", path[1]) + }; + let shown = path.join("."); + if !self.gtypes.contains_key(&key) { + return Err(Diag::new(format!("`{shown}` is not a generic type, so it can't take types in `[...]`"), span)); + } + self.check_visible(&key, span)?; + let mut tys = Vec::new(); + for a in args { + tys.push(self.resolve_type(a, span)?); + } + self.instantiate_type(&key, &shown, tys, span) + } + Type::Named { path, args } if path.len() == 2 && args.is_empty() => { + let Some(Target::User(module)) = self.imports.get(&path[0]) else { + return Err(Diag::new(format!("unknown type `{}.{}` (is `{}` imported?)", path[0], path[1], path[0]), span)); + }; + let key = format!("{module}.{}", path[1]); + self.check_visible(&key, span)?; + if self.ifaces.contains_key(&key) { + return Ok(Ty::Iface(Box::leak(key.into_boxed_str()))); + } + match (self.structs.get(&key), self.enums.get(&key)) { + (Some(info), _) => Ok(Ty::Struct(info.name)), + (_, Some(info)) => Ok(Ty::Enum(info.name)), + _ => Err(Diag::new(format!("module `{module}` has no type `{}`", path[1]), span)), + } + } + Type::Named { .. } => unsupported(span, "this kind of type"), + Type::Slice(inner) => { + let elem = self.resolve_type(inner, span)?; + if matches!(elem, Ty::Opt(_)) { + return unsupported(span, "a list of optionals (`[]T?`)"); + } + Ok(Ty::list(elem)) + } + Type::Optional(inner) => { + let t = self.resolve_type(inner, span)?; + match t { + // a weak pointer can already be nil: `weak Window?` and `weak Window` are the same + Ty::Weak(_) => Ok(t), + Ty::Opt(_) => Err(Diag::new("`T??` doesn't make sense; one `?` is enough", span)), + Ty::Error => Err(Diag::new("`error` can already be nil, so `error?` isn't needed", span)), + _ => Ok(Ty::opt(t)), + } + } + Type::Shared(inner) | Type::Weak(inner) => { + let is_shared = matches!(t, Type::Shared(_)); + let word = if is_shared { "shared" } else { "weak" }; + let inner_ty = self.resolve_type(inner, span)?; + if !matches!(inner_ty, Ty::Struct(_) | Ty::Enum(_)) { + return unsupported(span, &format!("`{word} {inner_ty}` (only structs and enums can be shared for now)")); + } + Ok(if is_shared { Ty::shared(inner_ty) } else { Ty::weak(inner_ty) }) + } + Type::Tuple(_) => unsupported(span, "tuple types"), + Type::Func { params, ret } => { + let mut ps = Vec::new(); + for p in params { + ps.push(self.resolve_type(p, span)?); + } + let r = match ret { + None => Ty::Unit, + Some(r) => self.resolve_type(r, span)?, + }; + if matches!(r, Ty::Tuple(_)) { + return unsupported(span, "a function value with several results"); + } + Ok(Ty::func(ps, r)) + } + } + } + + fn top_const(&mut self, v: &VarDecl) -> PResult<()> { + if v.names.len() != 1 { + return unsupported(v.span, "declaring several constants at once"); + } + let name = &v.names[0]; + self.check_name_free(name, v.span)?; + self.scopes.clear(); + self.cur_struct = None; + let value = v.value.as_ref().unwrap(); + let h = match &v.ty { + Some(t) => { + let want = self.resolve_type(t, v.span)?; + self.expr_as(value, want)? + } + None => self.expr(value, None)?, + }; + if has_call(&h) { + return Err(Diag::new("a top-level `const` can't call functions", value.span)); + } + if self.holds_list(h.ty) { + return unsupported(value.span, "a top-level `const` holding a list"); + } + let key = self.q(name); + self.mark_private(&key, v.is_pub); + self.consts.insert(key, h); + Ok(()) + } + + /// Is there a list inside a value of this type? + fn holds_list(&self, ty: Ty) -> bool { + match ty { + Ty::List(_) | Ty::Map(..) => true, + Ty::Opt(inner) => self.holds_list(*inner), + Ty::Tuple(items) => items.iter().any(|t| self.holds_list(*t)), + Ty::Struct(name) => self.structs[name].fields.iter().any(|(_, t)| self.holds_list(*t)), + Ty::Enum(name) => self.enums[name] + .variants + .iter() + .any(|v| v.fields.iter().any(|(_, t)| self.holds_list(*t))), + _ => false, + } + } + + /// Does a value of this type own heap data (lists, strings built at runtime, errors)? + /// Copying such a value is a deep copy (or a reference count bump for strings), and + /// every owner must free it. + fn needs_copy(&self, ty: Ty) -> bool { + match ty { + Ty::List(_) | Ty::Map(..) | Ty::Str | Ty::Error | Ty::Shared(_) | Ty::Weak(_) | Ty::Func(_) | Ty::Iface(_) => true, + Ty::Opt(inner) => self.needs_copy(*inner), + Ty::Tuple(items) => items.iter().any(|t| self.needs_copy(*t)), + Ty::Struct(name) => self.structs[name].fields.iter().any(|(_, t)| self.needs_copy(*t)), + Ty::Enum(name) => self.enums[name] + .variants + .iter() + .any(|v| v.fields.iter().any(|(_, t)| self.needs_copy(*t))), + _ => false, + } + } + + /// A value that is about to get a new owner. Reading a variable (or part of one) + /// whose type owns heap data copies it; fresh values (call results, literals) don't. + fn own(&self, h: HExpr) -> HExpr { + // reading a variable, a field or an element gives a view of someone else's data + let view = matches!( + h.kind, + HExprKind::Local(..) | HExprKind::Field { .. } | HExprKind::Index { .. } | HExprKind::Unwrap(_) + ); + if self.needs_copy(h.ty) && view { + let ty = h.ty; + mk(HExprKind::Copy(Box::new(h)), ty) + } else { + h + } + } + + fn expr_as_owned(&mut self, e: &Expr, want: Ty) -> PResult { + let h = self.expr_as(e, want)?; + Ok(self.own(h)) + } + + /// The zero value of a type: `0`, `0.0`, `false`, `""`, or a struct of zero values. + fn zero_expr(&self, ty: Ty) -> HExpr { + let kind = match ty { + Ty::Int => HExprKind::Int(0), + Ty::Float => HExprKind::Float(0.0), + Ty::Num(n) if n.is_float() => HExprKind::Float(0.0), + Ty::Num(_) => HExprKind::Int(0), + Ty::Bool => HExprKind::Bool(false), + Ty::Char => HExprKind::Int(0), + Ty::Str => HExprKind::Str(String::new()), + Ty::Struct(name) => HExprKind::StructLit { + name: name.to_string(), + fields: self.structs[name].fields.iter().map(|(n, t)| (n.clone(), self.zero_expr(*t))).collect(), + }, + // the zero value of an enum is its first case, with zero data + Ty::Enum(name) => HExprKind::EnumLit { + enum_name: name.to_string(), + variant: 0, + fields: self.enums[name].variants[0].fields.iter().map(|(_, t)| self.zero_expr(*t)).collect(), + }, + Ty::List(_) => HExprKind::ListLit { elems: Vec::new() }, + Ty::Map(..) => HExprKind::MapLit { entries: Vec::new() }, + Ty::Error | Ty::Opt(_) | Ty::Weak(_) | Ty::Shared(_) | Ty::Func(_) | Ty::Iface(_) => HExprKind::Nil, + Ty::Tuple(items) => HExprKind::TupleLit(items.iter().map(|t| self.zero_expr(*t)).collect()), + Ty::Unit => unreachable!("nothing has no zero value"), + }; + mk(kind, ty) + } + + // ---------- functions and statements ---------- + + fn func(&mut self, f: &FuncDecl, owner: Option<&str>) -> PResult { + let sig = match owner { + None => self.funcs[&self.q(&f.name)].clone(), + Some(o) => self.methods_of(o).unwrap()[&f.name].clone(), + }; + self.scopes = vec![HashMap::new()]; + self.next_id = 0; + self.ret = sig.ret; + self.loops = 0; + self.narrowed.clear(); + self.cur_struct = owner.and_then(|o| self.type_name(o)); + + // `self` is declared first, so it always has id 0 + let self_param = match self.cur_struct { + Some(name) => { + let ty = if self.enums.contains_key(name) { Ty::Enum(name) } else { Ty::Struct(name) }; + Some(self.declare("self", ty, false, f.span)?) + } + None => None, + }; + self.no_move.clear(); + if let Some(me) = &self_param { + self.no_move.insert(me.id); + } + let mut params = Vec::new(); + for (p, ty) in f.params.iter().zip(&sig.params) { + let local = self.declare(&p.name, *ty, false, f.span)?; + self.no_move.insert(local.id); + params.push(local); + } + let body_ast = f.body.as_ref().expect("functions have bodies"); + let body = self.stmts(&body_ast.stmts)?; + if sig.ret != Ty::Unit && !always_returns(&body) { + return Err(Diag::new( + format!("function `{}` can reach its end without returning a `{}`", f.name, sig.ret), + body_ast.span, + )); + } + self.cur_struct = None; + Ok(HFunc { + name: if owner.is_none() { self.q(&f.name) } else { f.name.clone() }, + owner: owner.map(|o| o.to_string()), + self_param, + mutates: false, + params, + ret: sig.ret, + body, + }) + } + + fn declare(&mut self, name: &str, ty: Ty, is_const: bool, span: Span) -> PResult { + let scope = self.scopes.last_mut().unwrap(); + if scope.contains_key(name) { + return Err(Diag::new(format!("`{name}` is already defined in this scope"), span)); + } + let id = self.next_id; + self.next_id += 1; + scope.insert(name.to_string(), Local { id, ty, is_const }); + Ok(HLocal { id, name: name.to_string(), ty, is_const }) + } + + fn lookup(&self, name: &str) -> Option<&Local> { + self.scopes.iter().rev().find_map(|s| s.get(name)) + } + + fn stmts(&mut self, stmts: &[Stmt]) -> PResult> { + stmts.iter().map(|s| self.stmt(s)).collect() + } + + fn block(&mut self, b: &Block) -> PResult> { + self.scopes.push(HashMap::new()); + let saved = self.narrowed.clone(); + let r = self.stmts(&b.stmts); + self.narrowed = saved; + self.scopes.pop(); + r + } + + fn stmt(&mut self, s: &Stmt) -> PResult { + match s { + Stmt::Var(v) => self.var_stmt(v), + Stmt::Assign { target, op, value, span } => self.assign(target, *op, value, *span), + Stmt::Expr(e) => { + if let ExprKind::Match { scrutinee, arms } = &e.kind { + return Ok(HStmt::Expr(self.match_expr(scrutinee, arms, None, e.span, true)?)); + } + if !matches!(e.kind, ExprKind::Call { .. } | ExprKind::Try(_)) { + return Err(Diag::new("this expression's value is never used", e.span)); + } + let h = self.expr_raw(e, None)?; + let can_fail = match h.ty { + Ty::Error => true, + Ty::Tuple(items) => items.contains(&Ty::Error), + _ => false, + }; + if can_fail { + return Err(Diag::new( + "this call can fail, but its error is ignored; add `?` after it, or keep the result with `var value, err = ...`", + e.span, + )); + } + Ok(HStmt::Expr(h)) + } + Stmt::If { cond, then, else_, .. } => { + let cond_h = self.expr_as(cond, Ty::Bool)?; + let (when_true, when_false) = self.facts(cond); + let saved = self.narrowed.clone(); + self.narrowed.extend(when_true.iter().copied()); + let then = self.block(then)?; + self.narrowed = saved.clone(); + self.narrowed.extend(when_false.iter().copied()); + let else_ = match else_ { + None => Vec::new(), + Some(s) => match &**s { + Stmt::Block(b) => self.block(b)?, + other => vec![self.stmt(other)?], + }, + }; + self.narrowed = saved; + // `if x == nil { return }`: after the `if`, x is known to be set + if diverges(&then) { + self.narrowed.extend(when_false.iter().copied()); + } else if !else_.is_empty() && diverges(&else_) { + self.narrowed.extend(when_true.iter().copied()); + } + Ok(HStmt::If { cond: cond_h, then, else_ }) + } + Stmt::For { kind, body, span } => self.for_stmt(kind, body, *span), + Stmt::Return { values, span } => self.return_stmt(values, *span), + Stmt::Break(span) | Stmt::Continue(span) => { + if self.loops == 0 { + let w = if matches!(s, Stmt::Break(_)) { "break" } else { "continue" }; + return Err(Diag::new(format!("`{w}` only makes sense inside a `for` loop"), *span)); + } + Ok(if matches!(s, Stmt::Break(_)) { HStmt::Break } else { HStmt::Continue }) + } + Stmt::Block(b) => Ok(HStmt::Block(self.block(b)?)), + } + } + + /// A value being returned: a local variable moves out; anything else is copied. + fn ret_value(&mut self, e: &Expr, ty: Ty) -> PResult { + let h = self.expr_as(e, ty)?; + let moves = matches!(&h.kind, HExprKind::Local(id, _) if !self.no_move.contains(id)); + Ok(if moves { h } else { self.own(h) }) + } + + fn return_stmt(&mut self, values: &[Expr], span: Span) -> PResult { + let ret = self.ret; + match ret { + Ty::Unit => match values.first() { + None => Ok(HStmt::Return(None)), + Some(e) => Err(Diag::new("this function returns nothing, so `return` can't have a value", e.span)), + }, + Ty::Tuple(tys) => { + if values.len() == tys.len() { + let mut items = Vec::new(); + for (e, t) in values.iter().zip(tys.iter()) { + items.push(self.ret_value(e, *t)?); + } + return Ok(HStmt::Return(Some(mk(HExprKind::TupleLit(items), ret)))); + } + if values.len() == 1 { + let h = self.expr_raw(&values[0], None)?; + if h.ty == ret { + return Ok(HStmt::Return(Some(h))); + } + // `return error("x")` is short for `return 0, error("x")` + if tys.last() == Some(&Ty::Error) && h.ty == Ty::Error { + let mut items: Vec = tys[..tys.len() - 1].iter().map(|t| self.zero_expr(*t)).collect(); + let moves = matches!(&h.kind, HExprKind::Local(id, _) if !self.no_move.contains(id)); + items.push(if moves { h } else { self.own(h) }); + return Ok(HStmt::Return(Some(mk(HExprKind::TupleLit(items), ret)))); + } + } + Err(Diag::new( + format!( + "this function returns `{ret}`, so `return` needs {} values (found {})", + tys.len(), + values.len() + ), + span, + )) + } + _ => { + if values.len() > 1 { + return Err(Diag::new( + format!("this function returns one `{ret}`, but {} values were given", values.len()), + span, + )); + } + match values.first() { + None => Err(Diag::new(format!("this function must return a `{ret}`"), span)), + Some(e) => Ok(HStmt::Return(Some(self.ret_value(e, ret)?))), + } + } + } + } + + /// `var a, b = f()` + fn var_many(&mut self, v: &VarDecl) -> PResult { + let (None, Some(value)) = (&v.ty, &v.value) else { + return Err(Diag::new("to declare several variables, write `var a, b = f()`", v.span)); + }; + let h = self.expr_raw(value, None)?; + let Ty::Tuple(tys) = h.ty else { + return Err(Diag::new( + format!("{} names are listed, but this gives one `{}` value", v.names.len(), h.ty), + value.span, + )); + }; + if tys.len() != v.names.len() { + return Err(Diag::new( + format!("this gives {} values `{}`, but {} names are listed", tys.len(), h.ty, v.names.len()), + value.span, + )); + } + let mut locals = Vec::new(); + for (name, ty) in v.names.iter().zip(tys.iter()) { + // `_` throws a value away + let name = if name == "_" { format!("_{}", self.next_id) } else { name.clone() }; + locals.push(self.declare(&name, *ty, v.is_const, v.span)?); + } + Ok(HStmt::LetMany { locals, init: h }) + } + + /// What a condition tells us about optional variables: (set when true, set when false). + fn facts(&self, e: &Expr) -> (Vec, Vec) { + match &e.kind { + ExprKind::Binary { op: op @ (BinOp::Eq | BinOp::Ne), lhs, rhs } => { + let name = match (&lhs.kind, &rhs.kind) { + (ExprKind::Ident(n), ExprKind::Nil) | (ExprKind::Nil, ExprKind::Ident(n)) => n, + _ => return (vec![], vec![]), + }; + match self.lookup(name) { + Some(l) if matches!(l.ty, Ty::Opt(_)) => { + if *op == BinOp::Ne { + (vec![l.id], vec![]) + } else { + (vec![], vec![l.id]) + } + } + _ => (vec![], vec![]), + } + } + ExprKind::Binary { op: BinOp::And, lhs, rhs } => { + let (mut t, _) = self.facts(lhs); + t.extend(self.facts(rhs).0); + (t, vec![]) + } + ExprKind::Binary { op: BinOp::Or, lhs, rhs } => { + let (_, mut f) = self.facts(lhs); + f.extend(self.facts(rhs).1); + (vec![], f) + } + ExprKind::Unary { op: UnOp::Not, expr } => { + let (t, f) = self.facts(expr); + (f, t) + } + _ => (vec![], vec![]), + } + } + + /// A variable that is known not to be nil reads as its inner value. + fn narrowed_use(&self, h: HExpr) -> HExpr { + match (&h.kind, h.ty) { + (HExprKind::Local(id, _), Ty::Opt(inner)) if self.narrowed.contains(id) => { + mk(HExprKind::Unwrap(Box::new(h)), *inner) + } + _ => h, + } + } + + fn var_stmt(&mut self, v: &VarDecl) -> PResult { + if v.names.len() != 1 { + return self.var_many(v); + } + let name = &v.names[0]; + let (ty, init) = match (&v.ty, &v.value) { + (Some(t), Some(e)) => { + let ty = self.resolve_type(t, v.span)?; + (ty, self.expr_as_owned(e, ty)?) + } + (Some(t), None) => { + let ty = self.resolve_type(t, v.span)?; + (ty, self.zero_expr(ty)) + } + (None, Some(e)) => { + let h = self.expr(e, None)?; + (h.ty, self.own(h)) + } + (None, None) => unreachable!("the parser rejects this"), + }; + // declared after the initializer is checked: `var x = x` means the outer x + let local = self.declare(name, ty, v.is_const, v.span)?; + Ok(HStmt::Let { local, init }) + } + + /// Check the left side of an assignment: a variable, or fields of one. + fn place(&mut self, e: &Expr) -> PResult { + match &e.kind { + ExprKind::Ident(name) => { + if let Some(l) = self.lookup(name) { + return Ok(mk(HExprKind::Local(l.id, name.clone()), l.ty)); + } + if let Some(h) = self.implicit_field(name) { + return Ok(h); + } + if self.consts.contains_key(&self.q(name)) { + return Err(Diag::new(format!("`{name}` is a constant and can't be changed"), e.span)); + } + Err(Diag::new(format!("unknown variable `{name}`"), e.span)) + } + ExprKind::Field { expr, name } => { + let base = self.place(expr)?; + let base = self.narrowed_use(base); + self.field_of(base, name, e.span) + } + ExprKind::Index { expr, index } => { + let base = self.place(expr)?; + let base = self.narrowed_use(base); + if matches!(index.kind, ExprKind::Range { .. }) { + return Err(Diag::new("a part of a list (`a[1..3]`) is a copy and can't be assigned to", e.span)); + } + if base.ty == Ty::Str { + return Err(Diag::new("a string can't be changed in place; build a new string instead", e.span)); + } + self.index_of(base, index, e.span) + } + _ => unsupported(e.span, "assigning to this kind of expression"), + } + } + + fn assign(&mut self, target: &Expr, op: AssignOp, value: &Expr, span: Span) -> PResult { + let place = self.place(target)?; + if let Some((_, root)) = place_root(&place) { + if self.lookup(root).map_or(false, |l| l.is_const) { + let what = if matches!(place.kind, HExprKind::Local(..)) { + format!("`{root}` is a constant and can't be changed") + } else { + format!("can't change a part of `{root}` because it is a constant") + }; + return Err(Diag::new(what, target.span)); + } + } + let ty = place.ty; + // `a[f()] += 1` must call f once, so complicated indexes are computed first + let mut pre: Vec = Vec::new(); + let place = if op == AssignOp::Set { place } else { self.hoist_indexes(place, &mut pre) }; + let value = match op { + AssignOp::Set => self.expr_as_owned(value, ty)?, + _ => { + let bop = match op { + AssignOp::Add => BinOp::Add, + AssignOp::Sub => BinOp::Sub, + AssignOp::Mul => BinOp::Mul, + AssignOp::Div => BinOp::Div, + AssignOp::Rem => BinOp::Rem, + AssignOp::Set => unreachable!(), + }; + let rhs = self.expr(value, Some(ty))?; + // `counts[w] += 1` on a key that isn't there yet starts from zero + let read = match &place.kind { + HExprKind::Index { base, index } if matches!(base.ty, Ty::Map(..)) => { + mk(HExprKind::MapCall { map: base.clone(), op: MapOp::Zero, args: vec![(**index).clone()] }, ty) + } + _ => place.clone(), + }; + let combined = self.binary_typed(bop, read, rhs, span)?; + self.coerce(combined, ty, value.span)? + } + }; + if let HExprKind::Local(id, _) = &place.kind { + self.narrowed.remove(id); + } + let assign = HStmt::Assign { target: place, value }; + if pre.is_empty() { + Ok(assign) + } else { + pre.push(assign); + Ok(HStmt::Block(pre)) + } + } + + /// Replace every index that isn't a plain variable or number by a fresh variable + /// (declared in `pre`), so the place can be read and written without running it twice. + fn hoist_indexes(&mut self, e: HExpr, pre: &mut Vec) -> HExpr { + match e.kind { + HExprKind::Index { base, index } => { + let base = self.hoist_indexes(*base, pre); + let index = if matches!(index.kind, HExprKind::Local(..) | HExprKind::Int(_)) { + *index + } else { + let id = self.next_id; + self.next_id += 1; + let ity = index.ty; + let local = HLocal { id, name: "$index".to_string(), ty: ity, is_const: false }; + pre.push(HStmt::Let { local, init: *index }); + mk(HExprKind::Local(id, "$index".to_string()), ity) + }; + mk(HExprKind::Index { base: Box::new(base), index: Box::new(index) }, e.ty) + } + HExprKind::Field { base, name } => { + let base = self.hoist_indexes(*base, pre); + mk(HExprKind::Field { base: Box::new(base), name }, e.ty) + } + other => mk(other, e.ty), + } + } + + fn index_of(&mut self, base: HExpr, index: &Expr, span: Span) -> PResult { + if base.ty == Ty::Str { + // `s[i]`: the character that starts at byte `i` + let idx = self.expr_as(index, Ty::Int)?; + return Ok(mk(HExprKind::Index { base: Box::new(base), index: Box::new(idx) }, Ty::Char)); + } + if let Ty::Map(key, val) = base.ty { + let k = self.expr_as(index, *key)?; + return Ok(mk(HExprKind::Index { base: Box::new(base), index: Box::new(k) }, *val)); + } + let Ty::List(elem) = base.ty else { + let hint = ""; + return Err(Diag::new(format!("can't index a `{}`{hint}", base.ty), span)); + }; + let idx = self.expr_as(index, Ty::Int)?; + Ok(mk(HExprKind::Index { base: Box::new(base), index: Box::new(idx) }, *elem)) + } + + fn for_stmt(&mut self, kind: &ForKind, body: &Block, span: Span) -> PResult { + match kind { + ForKind::Infinite => { + self.loops += 1; + let body = self.block(body); + self.loops -= 1; + Ok(HStmt::Loop(body?)) + } + ForKind::While(cond) => { + let cond = self.expr_as(cond, Ty::Bool)?; + self.loops += 1; + let body = self.block(body); + self.loops -= 1; + Ok(HStmt::While { cond, body: body? }) + } + ForKind::In { vars, iter, step } => { + let ExprKind::Range { start: Some(start), end: Some(end) } = &iter.kind else { + return self.for_each(vars, iter, step.as_ref(), body, span); + }; + if vars.len() != 1 { + return unsupported(span, "`for i, x in ...` over a range"); + } + let start = self.expr_as(start, Ty::Int)?; + let end = self.expr_as(end, Ty::Int)?; + let step = match step { + Some(s) => Some(self.expr_as(s, Ty::Int)?), + None => None, + }; + self.scopes.push(HashMap::new()); + let var = self.declare(&vars[0], Ty::Int, false, span); + self.loops += 1; + let body = var.and_then(|var| self.block(body).map(|b| (var, b))); + self.loops -= 1; + self.scopes.pop(); + let (var, body) = body?; + Ok(HStmt::ForRange { var, start, end, step, body }) + } + } + } + + fn for_each( + &mut self, + vars: &[String], + iter: &Expr, + step: Option<&Expr>, + body: &Block, + span: Span, + ) -> PResult { + if let Some(s) = step { + return Err(Diag::new("`step` only works with ranges like `0..10`", s.span)); + } + let it = self.expr(iter, None)?; + if it.ty == Ty::Str { + // `for c in s` walks the characters; `for i, c in s` also gives the byte position + self.scopes.push(HashMap::new()); + let declared = (|| -> PResult<(Option, HLocal)> { + let (index, var) = if vars.len() == 2 { + (Some(self.declare(&vars[0], Ty::Int, false, span)?), self.declare(&vars[1], Ty::Char, false, span)?) + } else { + (None, self.declare(&vars[0], Ty::Char, false, span)?) + }; + self.no_move.insert(var.id); + Ok((index, var)) + })(); + self.loops += 1; + let body = declared.and_then(|(index, var)| self.block(body).map(|b| (index, var, b))); + self.loops -= 1; + self.scopes.pop(); + let (index, var, body) = body?; + return Ok(HStmt::ForEach { index, var, iter: it, body }); + } + if let Ty::Map(key, val) = it.ty { + self.scopes.push(HashMap::new()); + let declared = (|| -> PResult<(Option, HLocal)> { + let (index, var) = if vars.len() == 2 { + let k = self.declare(&vars[0], *key, false, span)?; + (Some(k), self.declare(&vars[1], *val, false, span)?) + } else { + (None, self.declare(&vars[0], *key, false, span)?) + }; + self.no_move.insert(var.id); + Ok((index, var)) + })(); + self.loops += 1; + let body = declared.and_then(|(index, var)| self.block(body).map(|b| (index, var, b))); + self.loops -= 1; + self.scopes.pop(); + let (index, var, body) = body?; + return Ok(HStmt::ForEach { index, var, iter: it, body }); + } + let Ty::List(elem) = it.ty else { + return Err(Diag::new( + format!("can't loop over a `{}`; use a list or a range like `0..10`", it.ty), + iter.span, + )); + }; + self.scopes.push(HashMap::new()); + let declared = (|| -> PResult<(Option, HLocal)> { + let (index, var_name) = if vars.len() == 2 { + (Some(self.declare(&vars[0], Ty::Int, false, span)?), &vars[1]) + } else { + (None, &vars[0]) + }; + let var = self.declare(var_name, *elem, false, span)?; + self.no_move.insert(var.id); + Ok((index, var)) + })(); + self.loops += 1; + let body = declared.and_then(|(index, var)| self.block(body).map(|b| (index, var, b))); + self.loops -= 1; + self.scopes.pop(); + let (index, var, body) = body?; + Ok(HStmt::ForEach { index, var, iter: it, body }) + } + + // ---------- expressions ---------- + + /// Check an expression that must produce a value. + fn expr(&mut self, e: &Expr, expect: Option) -> PResult { + let h = self.expr_raw(e, expect)?; + if h.ty == Ty::Unit { + return Err(Diag::new("this call returns nothing, so it can't be used as a value", e.span)); + } + if let Ty::Tuple(_) = h.ty { + return Err(Diag::new( + format!( + "this call returns several values `{}`; take them with `var a, b = ...` (or add `?` to pass the error up)", + h.ty + ), + e.span, + )); + } + Ok(h) + } + + /// Check an expression and require it to have type `want`. + fn expr_as(&mut self, e: &Expr, want: Ty) -> PResult { + // a `T` is accepted where a `T?` is wanted (`nil` itself is checked against the whole type) + let expect = match want { + Ty::Opt(inner) if !matches!(e.kind, ExprKind::Nil) => *inner, + _ => want, + }; + let h = self.expr(e, Some(expect))?; + if let Ty::Opt(inner) = want { + if h.ty == *inner { + let owned = self.own(h); + return Ok(mk(HExprKind::Wrap(Box::new(owned)), want)); + } + } + self.coerce(h, want, e.span) + } + + fn coerce(&self, h: HExpr, want: Ty, span: Span) -> PResult { + if h.ty == want { + return Ok(h); + } + if want.is_number() && (is_int_lit(&h) || (want.is_float() && is_float_lit(&h))) { + return retype_lit(h, want, span, false); + } + // a value whose type has the interface's methods can be used as the interface + if let Ty::Iface(key) = want { + if matches!(h.ty, Ty::Struct(_) | Ty::Enum(_)) { + return match self.bound_failure(&Bound::Iface(key.to_string()), h.ty) { + None => { + let owned = self.own(h); + Ok(mk(HExprKind::ToIface(Box::new(owned)), want)) + } + Some((_, why)) => Err(Diag::new(format!("expected `{want}`, found `{}`: {why}", h.ty), span)), + }; + } + } + let hint = if h.ty.is_number() && want.is_number() { + format!(" (convert it with `{want}(...)`)") + } else { + String::new() + }; + Err(Diag::new(format!("expected `{want}`, found `{}`{hint}", h.ty), span)) + } + + fn self_local(&self) -> HExpr { + let l = self.lookup("self").expect("only called inside methods"); + mk(HExprKind::Local(l.id, "self".to_string()), l.ty) + } + + /// Inside a method, a bare name can be a field of `self`. + fn implicit_field(&self, name: &str) -> Option { + let owner = self.cur_struct?; + let (_, ty) = self.structs.get(owner)?.fields.iter().find(|(n, _)| n == name)?; + Some(mk( + HExprKind::Field { base: Box::new(self.self_local()), name: name.to_string() }, + *ty, + )) + } + + fn check_field_visible(&self, owner: &str, field: &str, span: Span) -> PResult<()> { + let owner_mod = module_of(owner); + if owner_mod != self.cur_mod && self.private.contains(&format!("{owner}.{field}")) { + return Err(Diag::new( + format!("field `{field}` is private to `{owner_mod}`; write `pub` in front of it in {owner_mod}.dino to share it"), + span, + )); + } + Ok(()) + } + + fn field_of(&self, base: HExpr, name: &str, span: Span) -> PResult { + let base = auto_deref(base); + let Ty::Struct(sname) = base.ty else { + let hint = if matches!(base.ty, Ty::Enum(_)) { "; use `match` to get at the data of an enum" } else { "" }; + return Err(Diag::new( + format!("a `{}` has no fields (looking for `.{name}`){hint}", base.ty), + span, + )); + }; + let info = &self.structs[sname]; + match info.fields.iter().find(|(n, _)| n == name) { + Some((_, ty)) => { + self.check_field_visible(sname, name, span)?; + Ok(mk(HExprKind::Field { base: Box::new(base), name: name.to_string() }, *ty)) + } + None if info.methods.contains_key(name) => Err(Diag::new( + format!("`{name}` is a method of `{sname}`; call it like `.{name}(...)`"), + span, + )), + None => { + let mut known: Vec<&str> = info.fields.iter().map(|(n, _)| n.as_str()).collect(); + known.extend(info.methods.keys().map(|k| k.as_str())); + known.sort(); + Err(Diag::new( + format!("`{sname}` has no field or method `{name}` (it has: {})", known.join(", ")), + span, + )) + } + } + } + + fn expr_raw(&mut self, e: &Expr, expect: Option) -> PResult { + match &e.kind { + ExprKind::Int(n) => match expect { + Some(t) if t.is_number() && t != Ty::Int => retype_lit(mk(HExprKind::Int(*n), Ty::Int), t, e.span, false), + _ => Ok(mk(HExprKind::Int(*n), Ty::Int)), + }, + ExprKind::Float(x) => Ok(mk( + HExprKind::Float(*x), + if expect == Some(Ty::Num(Num::F32)) { Ty::Num(Num::F32) } else { Ty::Float }, + )), + ExprKind::Bool(b) => Ok(mk(HExprKind::Bool(*b), Ty::Bool)), + ExprKind::Str(parts) => self.string(parts), + ExprKind::Ident(name) => self.ident(name, e.span), + ExprKind::Unary { op, expr } => { + // a negated literal gets its type later, so `-128` can be an `i8` + let literal = matches!(expr.kind, ExprKind::Int(_) | ExprKind::Float(_)); + let inner = self.expr(expr, if *op == UnOp::Neg && !literal { expect } else { None })?; + match (op, inner.ty) { + (UnOp::Neg, t) if t.is_number() && !t.is_signed() => { + Err(Diag::new(format!("a `{t}` can't be negative, so it can't be negated"), e.span)) + } + (UnOp::Neg, t) if t.is_number() => { + let ty = inner.ty; + Ok(mk(HExprKind::Unary { op: *op, expr: Box::new(inner) }, ty)) + } + (UnOp::Not, Ty::Bool) => { + let ty = inner.ty; + Ok(mk(HExprKind::Unary { op: *op, expr: Box::new(inner) }, ty)) + } + (UnOp::Neg, t) => Err(Diag::new(format!("can't negate a `{t}`"), e.span)), + (UnOp::Not, t) => Err(Diag::new(format!("`!` needs a `bool`, found `{t}`"), e.span)), + } + } + ExprKind::Binary { op: op @ (BinOp::Eq | BinOp::Ne), lhs, rhs } + if matches!(lhs.kind, ExprKind::Nil) || matches!(rhs.kind, ExprKind::Nil) => + { + let other = if matches!(lhs.kind, ExprKind::Nil) { rhs } else { lhs }; + if matches!(other.kind, ExprKind::Nil) { + return Err(Diag::new("`nil == nil` is always true; compare a variable with `nil`", e.span)); + } + // look at the variable itself, not at its narrowed value + let plain = match &other.kind { + ExprKind::Ident(n) => self.lookup(n).map(|l| mk(HExprKind::Local(l.id, n.clone()), l.ty)), + _ => None, + }; + let h = match plain { + Some(h) => h, + None => self.expr(other, None)?, + }; + if !matches!(h.ty, Ty::Opt(_) | Ty::Error | Ty::Weak(_) | Ty::Iface(_)) { + return Err(Diag::new( + format!("a `{}` can never be nil, so comparing it with `nil` is pointless", h.ty), + e.span, + )); + } + let set = mk(HExprKind::IsSet(Box::new(h)), Ty::Bool); + Ok(if *op == BinOp::Ne { + set + } else { + mk(HExprKind::Unary { op: UnOp::Not, expr: Box::new(set) }, Ty::Bool) + }) + } + ExprKind::Binary { op, lhs, rhs } => { + let arith = matches!(op, BinOp::Add | BinOp::Sub | BinOp::Mul | BinOp::Div | BinOp::Rem); + let sub = if arith { expect } else { None }; + let l = self.expr(lhs, sub)?; + // in `x != nil && x.ok`, the right side knows x is set (and `||` with `==`) + let mut added: Vec = Vec::new(); + if matches!(op, BinOp::And | BinOp::Or) { + let (t, f) = self.facts(lhs); + let known = if *op == BinOp::And { t } else { f }; + added = known.into_iter().filter(|id| self.narrowed.insert(*id)).collect(); + } + let r = self.expr(rhs, sub)?; + for id in added { + self.narrowed.remove(&id); + } + self.binary_typed(*op, l, r, e.span) + } + ExprKind::Call { callee, args } => self.call(callee, args, e.span), + ExprKind::Field { expr, name } if self.module_alias(expr).is_some() => { + let m = self.module_alias(expr).unwrap(); + self.module_value(m, name, e.span) + } + ExprKind::Generic { .. } => Err(Diag::new( + "a generic type is not a value; build one with `Name[types]{...}` or pick an enum case with `Name[types].Case`", + e.span, + )), + ExprKind::Field { expr, name } if matches!(expr.kind, ExprKind::Generic { .. }) => { + let ExprKind::Generic { expr: base, args } = &expr.kind else { unreachable!() }; + let Ty::Enum(en) = self.generic_expr_type(base, args, expr.span)? else { + return Err(Diag::new("only enums have cases to pick with `.`", e.span)); + }; + self.enum_value(en, name, None, e.span) + } + ExprKind::Field { expr, name } if self.user_alias(expr).is_some() => { + let module = self.user_alias(expr).unwrap(); + let key = format!("{module}.{name}"); + self.check_visible(&key, e.span)?; + match self.consts.get(&key) { + Some(c) => Ok(c.clone()), + None if self.funcs.contains_key(&key) => Err(Diag::new( + format!("`{module}.{name}` is a function; call it like `{module}.{name}(...)`"), + e.span, + )), + None => Err(Diag::new(format!("module `{module}` has no value `{name}`"), e.span)), + } + } + ExprKind::Field { expr, name } if self.enum_named(expr).is_some() => { + let en = self.enum_named(expr).unwrap(); + self.check_visible(en, expr.span)?; + self.enum_value(en, name, None, e.span) + } + ExprKind::Field { expr, name } => { + let base = self.expr(expr, None)?; + self.field_of(base, name, e.span) + } + ExprKind::Match { scrutinee, arms } => self.match_expr(scrutinee, arms, expect, e.span, false), + ExprKind::Char(c) => Ok(mk(HExprKind::Int(*c as i64), Ty::Char)), + ExprKind::Array(items) => self.array_lit(items, expect, e.span), + ExprKind::MapLit { types, entries } => self.map_lit(types.as_ref(), entries, expect, e.span), + ExprKind::Index { expr, index } => { + let base = self.expr(expr, None)?; + if let ExprKind::Range { start, end } = &index.kind { + if !matches!(base.ty, Ty::List(_) | Ty::Str) { + return Err(Diag::new(format!("can't take a part of a `{}`", base.ty), e.span)); + } + let lo = match start { + Some(s) => Some(Box::new(self.expr_as(s, Ty::Int)?)), + None => None, + }; + let hi = match end { + Some(s) => Some(Box::new(self.expr_as(s, Ty::Int)?)), + None => None, + }; + let ty = base.ty; + return Ok(mk(HExprKind::Slice { base: Box::new(base), lo, hi }, ty)); + } + self.index_of(base, index, e.span) + } + ExprKind::StructLit { ty, fields } => self.struct_lit(ty, fields, e.span), + ExprKind::Range { .. } => Err(Diag::new( + "a range like `a..b` can only be used in `for i in a..b { }`", + e.span, + )), + ExprKind::Nil => match expect { + Some(t @ (Ty::Opt(_) | Ty::Error | Ty::Weak(_) | Ty::Iface(_))) => Ok(mk(HExprKind::Nil, t)), + _ => Err(Diag::new( + "can't tell what `nil` stands for here; it needs a type like `User?` or `error` (for example `return value, nil`)", + e.span, + )), + }, + ExprKind::Try(inner) => { + let h = self.expr_raw(inner, None)?; + let can_pass_up = match self.ret { + Ty::Error => true, + Ty::Tuple(items) => items.last() == Some(&Ty::Error), + _ => false, + }; + if !can_pass_up { + return Err(Diag::new( + "`?` passes an error up to the caller, so this function must return an `error` (for example `func load(): (int, error)`)", + e.span, + )); + } + let ty = match h.ty { + Ty::Error => Ty::Unit, + Ty::Tuple(items) if items.len() == 2 && items[1] == Ty::Error => items[0], + Ty::Tuple(_) => return unsupported(e.span, "`?` on a call with more than two results"), + Ty::Opt(_) => { + return Err(Diag::new("`?` is for calls that return an error; for an optional use `if x != nil`", e.span)) + } + t => { + return Err(Diag::new( + format!("`?` needs a call that returns an error, but this gives `{t}`"), + e.span, + )) + } + }; + Ok(mk(HExprKind::Try(Box::new(h)), ty)) + } + ExprKind::Shared(inner) => { + let want = match expect { + Some(Ty::Shared(t)) | Some(Ty::Opt(&Ty::Shared(t))) => Some(*t), + _ => None, + }; + let h = self.expr(inner, want)?; + if !matches!(h.ty, Ty::Struct(_) | Ty::Enum(_)) { + return if matches!(h.ty, Ty::Shared(_)) { + Err(Diag::new("this is already `shared`; copy it with `var b = a` to get another owner", e.span)) + } else { + unsupported(e.span, &format!("`shared` of a `{}` (only structs and enums can be shared for now)", h.ty)) + }; + } + let ty = Ty::shared(h.ty); + let owned = self.own(h); + Ok(mk(HExprKind::NewShared(Box::new(owned)), ty)) + } + ExprKind::Weak(inner) => { + let h = self.expr(inner, None)?; + let Ty::Shared(t) = h.ty else { + return Err(Diag::new( + format!("`weak` points at a `shared` value, but this is a `{}`", h.ty), + e.span, + )); + }; + Ok(mk(HExprKind::Downgrade(Box::new(h)), Ty::Weak(t))) + } + ExprKind::Closure { params, ret, body } => { + let ps: Vec<(String, Option<&Type>)> = params.iter().map(|p| (p.name.clone(), Some(&p.ty))).collect(); + let (ep, er) = Self::expected_fn(expect); + self.closure_expr(&ps, ret.as_ref(), ClosureBody::Block(body), ep, er, e.span) + } + ExprKind::Lambda { params, body } => { + let ps: Vec<(String, Option<&Type>)> = params.iter().map(|n| (n.clone(), None)).collect(); + let (ep, er) = Self::expected_fn(expect); + self.closure_expr(&ps, None, ClosureBody::Expr(body), ep, er, e.span) + } + } + } + + fn map_lit(&mut self, types: Option<&(Type, Type)>, entries: &[(Expr, Expr)], expect: Option, span: Span) -> PResult { + let (key_ty, val_ty) = match (types, expect, entries.first()) { + (Some((k, v)), _, _) => { + let t = self.resolve_type(&Type::Named { path: vec!["map".into()], args: vec![k.clone(), v.clone()] }, span)?; + let Ty::Map(k, v) = t else { unreachable!() }; + (*k, *v) + } + (None, Some(Ty::Map(k, v)), _) => (*k, *v), + (None, _, Some((k, v))) => { + let kt = self.expr(k, None)?.ty; + let vt = self.expr(v, None)?.ty; + let t = self.resolve_type_of_map(kt, vt, span)?; + let Ty::Map(k, v) = t else { unreachable!() }; + (*k, *v) + } + (None, _, None) => { + return Err(Diag::new( + "can't tell what kind of map `{}` is here; write the type, e.g. `var m: map[string, int] = {}`", + span, + )) + } + }; + let mut out = Vec::new(); + for (k, v) in entries { + out.push((self.expr_as_owned(k, key_ty)?, self.expr_as_owned(v, val_ty)?)); + } + Ok(mk(HExprKind::MapLit { entries: out }, Ty::map(key_ty, val_ty))) + } + + /// Checks that `key` and `val` can make a map, the way a written `map[K, V]` is checked. + fn resolve_type_of_map(&self, key: Ty, val: Ty, span: Span) -> PResult { + if !(matches!(key, Ty::Int | Ty::Str | Ty::Bool | Ty::Char) || matches!(key, Ty::Num(n) if !n.is_float())) { + return Err(Diag::new( + format!("a map key must be an `int`, `string`, `bool`, `char` or other whole-number type, not `{key}`"), + span, + )); + } + Ok(Ty::map(key, val)) + } + + fn array_lit(&mut self, items: &[Expr], expect: Option, span: Span) -> PResult { + let expected_elem = match expect { + Some(Ty::List(e)) => Some(*e), + _ => None, + }; + let mut elems = Vec::new(); + let elem_ty = match (expected_elem, items.first()) { + (Some(t), _) => t, + (None, None) => { + return Err(Diag::new( + "can't tell what kind of list `[]` is here; write the type, e.g. `var xs: []int`", + span, + )) + } + (None, Some(first)) => self.expr(first, None)?.ty, + }; + for item in items { + elems.push(self.expr_as_owned(item, elem_ty)?); + } + Ok(mk(HExprKind::ListLit { elems }, Ty::list(elem_ty))) + } + + fn ident(&mut self, name: &str, span: Span) -> PResult { + if let Some(l) = self.lookup(name) { + let h = mk(HExprKind::Local(l.id, name.to_string()), l.ty); + return Ok(self.narrowed_use(h)); + } + if let Some(h) = self.implicit_field(name) { + return Ok(h); + } + if let Some(owner) = self.cur_struct { + if self.methods_of(owner).map_or(false, |m| m.contains_key(name)) { + return Err(Diag::new(format!("`{name}` is a method; call it like `{name}(...)`"), span)); + } + } + let key = self.q(name); + if let Some(c) = self.consts.get(&key) { + return Ok(c.clone()); + } + if self.funcs.contains_key(&key) { + return self.func_value(&key, name, span); + } + if self.generics.contains_key(&key) || BUILTINS.contains(&name) { + return Err(Diag::new(format!("`{name}` is a function; call it like `{name}(...)`"), span)); + } + if self.structs.contains_key(&key) { + return Err(Diag::new( + format!("`{name}` is a type, not a value; build one with `{name}{{...}}`"), + span, + )); + } + if let Some(info) = self.enums.get(&key) { + return Err(Diag::new( + format!("`{name}` is a type, not a value; pick a case, like `{name}.{}`", info.variants[0].name), + span, + )); + } + Err(Diag::new(format!("unknown name `{name}`"), span)) + } + + fn struct_lit(&mut self, ty: &Expr, inits: &[FieldInit], span: Span) -> PResult { + let name = match &ty.kind { + ExprKind::Ident(n) => n, + ExprKind::Field { name, .. } => name, + ExprKind::Generic { expr, .. } => match &expr.kind { + ExprKind::Ident(n) => n, + ExprKind::Field { name, .. } => name, + _ => return unsupported(ty.span, "building values of this kind of type"), + }, + _ => return unsupported(ty.span, "building values of this kind of type"), + }; + let key = if let ExprKind::Generic { expr, args } = &ty.kind { + match self.generic_expr_type(expr, args, ty.span)? { + Ty::Struct(n) => n.to_string(), + other => return Err(Diag::new(format!("`{other}` is not a struct, so it can't be built with `{{...}}`"), ty.span)), + } + } else { + let Some(key) = self.global_key(ty) else { + return Err(Diag::new(format!("unknown type `{name}`"), ty.span)); + }; + if self.gtypes.contains_key(&key) { + return Err(Diag::new(format!("`{name}` needs types: write `{name}[int]{{...}}`"), ty.span)); + } + key + }; + let Some(info) = self.structs.get(&key) else { + return Err(Diag::new(format!("unknown type `{name}`"), ty.span)); + }; + self.check_visible(&key, ty.span)?; + let (sname, fields) = (info.name, info.fields.clone()); + let given: Vec<&str> = if inits.iter().any(|i| i.name.is_some()) { + inits.iter().filter_map(|i| i.name.as_deref()).collect() + } else { + fields.iter().take(inits.len()).map(|(n, _)| n.as_str()).collect() + }; + for f in given { + self.check_field_visible(sname, f, span)?; + } + let named = inits.iter().filter(|i| i.name.is_some()).count(); + if named != 0 && named != inits.len() { + return Err(Diag::new("give every field a name (`x: 1`) or none of them, not a mix", span)); + } + // (field name, value) in the order written + let mut values: Vec<(String, HExpr)> = Vec::new(); + if named == 0 { + if !inits.is_empty() && inits.len() != fields.len() { + return Err(Diag::new( + format!( + "`{name}` has {} field{}, but {} value{} given (or name the fields: `{name}{{x: 1}}`)", + fields.len(), + if fields.len() == 1 { "" } else { "s" }, + inits.len(), + if inits.len() == 1 { " was" } else { "s were" }, + ), + span, + )); + } + for (init, (fname, fty)) in inits.iter().zip(&fields) { + values.push((fname.clone(), self.expr_as_owned(&init.value, *fty)?)); + } + } else { + for init in inits { + let fname = init.name.as_ref().unwrap(); + let Some((_, fty)) = fields.iter().find(|(n, _)| n == fname) else { + return Err(Diag::new(format!("`{name}` has no field `{fname}`"), init.value.span)); + }; + if values.iter().any(|(n, _)| n == fname) { + return Err(Diag::new(format!("field `{fname}` is given twice"), init.value.span)); + } + values.push((fname.clone(), self.expr_as_owned(&init.value, *fty)?)); + } + } + // fields that were left out start at their zero value + for (fname, fty) in &fields { + if !values.iter().any(|(n, _)| n == fname) { + values.push((fname.clone(), self.zero_expr(*fty))); + } + } + Ok(mk(HExprKind::StructLit { name: sname.to_string(), fields: values }, Ty::Struct(sname))) + } + + // ---------- match ---------- + + fn variant_index(&self, enum_name: &str, written: &str) -> Option { + let case = written.rsplit('.').next().unwrap_or(written); + self.enums[enum_name].variants.iter().position(|v| v.name == case) + } + + fn match_expr(&mut self, scrutinee: &Expr, arms: &[MatchArm], expect: Option, span: Span, as_stmt: bool) -> PResult { + let scrut = self.expr(scrutinee, None)?; + let scrut = auto_deref(scrut); + let sty = scrut.ty; + if !(matches!(sty, Ty::Enum(_) | Ty::Bool | Ty::Str | Ty::Char) || sty.is_int()) { + return Err(Diag::new( + format!("can't `match` on a `{sty}` (matching works on enums, ints, bools, chars and strings)"), + scrutinee.span, + )); + } + let mut cover = Cover::default(); + let mut res_ty: Option = None; + let mut hir_arms = Vec::new(); + for arm in arms { + if cover.catch_all { + return Err(Diag::new( + "this case can never match, because an earlier case already matches everything", + arm.span, + )); + } + self.scopes.push(HashMap::new()); + let r = self.match_arm(arm, sty, &mut cover, &mut res_ty, expect, as_stmt); + self.scopes.pop(); + hir_arms.push(r?); + } + if !cover.catch_all { + let missing: Vec = match sty { + Ty::Enum(en) => self.enums[en] + .variants + .iter() + .enumerate() + .filter(|(i, _)| !cover.variants.contains(i)) + .map(|(_, v)| format!("{en}.{}", v.name)) + .collect(), + Ty::Bool => [(cover.t, "true"), (cover.f, "false")] + .iter() + .filter(|(seen, _)| !seen) + .map(|(_, n)| n.to_string()) + .collect(), + _ => vec!["every other value".to_string()], + }; + if !missing.is_empty() { + let how = if sty.is_int() || sty == Ty::Str || sty == Ty::Char { + "add a `_` case to cover all the other values".to_string() + } else { + "add the missing cases, or a `_` case for all the rest".to_string() + }; + return Err(Diag::new( + format!("this `match` doesn't handle {} ({how})", missing.join(", ")), + span, + )); + } + } + let ty = if as_stmt { Ty::Unit } else { res_ty.unwrap_or(Ty::Unit) }; + Ok(mk(HExprKind::Match { scrutinee: Box::new(scrut), arms: hir_arms }, ty)) + } + + /// A name in a pattern that binds data (rather than naming an enum case) is lowercase. + fn binding_name_ok(name: &str) -> bool { + !name.contains('.') && name.chars().next().map_or(false, |c| c.is_lowercase() || c == '_') + } + + fn bind(&mut self, name: &str, ty: Ty, span: Span) -> PResult { + let local = self.declare(name, ty, false, span)?; + // a binding looks at the matched value; `return x` must copy it + self.no_move.insert(local.id); + Ok(local) + } + + fn match_arm( + &mut self, + arm: &MatchArm, + sty: Ty, + cover: &mut Cover, + res_ty: &mut Option, + expect: Option, + as_stmt: bool, + ) -> PResult { + let span = arm.span; + let case_pat = |this: &mut Checker, en: &'static str, name: &str, subs: &[Pattern], cover: &mut Cover| -> PResult { + let Some(idx) = this.variant_index(en, name) else { + let cases: Vec<&str> = this.enums[en].variants.iter().map(|v| v.name.as_str()).collect(); + return Err(Diag::new( + format!("enum `{en}` has no case `{name}` (its cases: {})", cases.join(", ")), + span, + )); + }; + let fields = this.enums[en].variants[idx].fields.clone(); + let case = this.enums[en].variants[idx].name.clone(); + if subs.len() != fields.len() { + let msg = if fields.is_empty() { + format!("`{en}.{case}` has no data, so write it without parentheses") + } else { + let names: Vec = fields.iter().map(|(n, t)| format!("{n}: {t}")).collect(); + format!("`{en}.{case}` carries {}; write all of them: {case}({})", names.join(", "), vec!["_"; fields.len()].join(", ")) + }; + return Err(Diag::new(msg, span)); + } + if !cover.variants.insert(idx) { + return Err(Diag::new(format!("`{en}.{case}` is already handled by an earlier case"), span)); + } + let mut binds = Vec::new(); + for (sub, (_, fty)) in subs.iter().zip(&fields) { + binds.push(match sub { + Pattern::Wildcard => None, + Pattern::Binding(n) if Checker::binding_name_ok(n) => Some(this.bind(n, *fty, span)?), + Pattern::Binding(n) => { + return Err(Diag::new( + format!("`{n}` inside a case must be a lowercase name that takes the data (nested cases aren't supported yet)"), + span, + )) + } + Pattern::Variant { .. } => return unsupported(span, "a case inside a case"), + _ => return unsupported(span, "a literal inside a case"), + }); + } + Ok(HPat::Variant { index: idx, binds }) + }; + + let pat = match &arm.pattern { + Pattern::Wildcard => { + cover.catch_all = true; + HPat::CatchAll(None) + } + Pattern::Binding(name) => match sty { + Ty::Enum(en) if self.variant_index(en, name).is_some() => { + let idx = self.variant_index(en, name).unwrap(); + let v = &self.enums[en].variants[idx]; + if !v.fields.is_empty() { + let blanks = vec!["_"; v.fields.len()].join(", "); + return Err(Diag::new( + format!("`{en}.{}` carries data; write `{}({blanks})` to ignore it or name the pieces", v.name, v.name), + span, + )); + } + case_pat(self, en, name, &[], cover)? + } + _ if Checker::binding_name_ok(name) => { + cover.catch_all = true; + HPat::CatchAll(Some(self.bind(name, sty, span)?)) + } + Ty::Enum(en) => { + let cases: Vec<&str> = self.enums[en].variants.iter().map(|v| v.name.as_str()).collect(); + return Err(Diag::new( + format!("enum `{en}` has no case `{name}` (its cases: {})", cases.join(", ")), + span, + )); + } + _ => { + return Err(Diag::new( + format!("`{name}` isn't something a `{sty}` can be matched against; use a value or a lowercase name"), + span, + )) + } + }, + Pattern::Variant { name, args } => match sty { + Ty::Enum(en) => case_pat(self, en, name, args, cover)?, + _ => return Err(Diag::new(format!("`{name}(...)` only works when matching an enum, not a `{sty}`"), span)), + }, + Pattern::Int(n) => { + if !sty.is_int() { + return Err(Diag::new(format!("this is a number, but the value being matched is a `{sty}`"), span)); + } + if !cover.ints.insert(*n) { + return Err(Diag::new(format!("{n} is already handled by an earlier case"), span)); + } + HPat::Int(*n) + } + Pattern::Char(c) => { + if sty != Ty::Char { + return Err(Diag::new(format!("this is a character, but the value being matched is a `{sty}`"), span)); + } + if !cover.ints.insert(*c as i64) { + return Err(Diag::new(format!("'{c}' is already handled by an earlier case"), span)); + } + HPat::Int(*c as i64) + } + Pattern::Bool(b) => { + if sty != Ty::Bool { + return Err(Diag::new(format!("this is a bool, but the value being matched is a `{sty}`"), span)); + } + let seen = if *b { &mut cover.t } else { &mut cover.f }; + if *seen { + return Err(Diag::new(format!("{b} is already handled by an earlier case"), span)); + } + *seen = true; + HPat::Bool(*b) + } + Pattern::Str(t) => { + if sty != Ty::Str { + return Err(Diag::new(format!("this is a string, but the value being matched is a `{sty}`"), span)); + } + if !cover.strs.insert(t.clone()) { + return Err(Diag::new(format!("\"{t}\" is already handled by an earlier case"), span)); + } + HPat::Str(t.clone()) + } + Pattern::Nil => return unsupported(span, "`nil` in a pattern"), + }; + + let body = match (&arm.body, as_stmt) { + (ArmBody::Block(b), true) => HArmBody::Block(self.block(b)?), + (ArmBody::Expr(e), true) => HArmBody::Expr(self.expr_raw(e, None)?), + (ArmBody::Expr(e), false) => { + let h = self.expr(e, res_ty.or(expect))?; + let h = match *res_ty { + None => { + *res_ty = Some(h.ty); + h + } + Some(t) => self.coerce(h, t, e.span)?, + }; + HArmBody::Expr(self.own(h)) + } + (ArmBody::Block(b), false) => match b.stmts.as_slice() { + [st @ (Stmt::Return { .. } | Stmt::Break(_) | Stmt::Continue(_))] => { + HArmBody::Block(vec![self.stmt(st)?]) + } + _ => { + return Err(Diag::new( + "an arm of a `match` that produces a value must be a single expression \ + (call a function for more, or use the `match` as a statement)", + b.span, + )) + } + }, + }; + Ok(HArm { pat, body }) + } + + fn string(&mut self, parts: &[StrPart]) -> PResult { + if parts.iter().all(|p| matches!(p, StrPart::Text(_))) { + let s: String = parts + .iter() + .map(|p| match p { + StrPart::Text(t) => t.as_str(), + _ => "", + }) + .collect(); + return Ok(mk(HExprKind::Str(s), Ty::Str)); + } + let mut out = Vec::new(); + for p in parts { + match p { + StrPart::Text(t) => out.push(HInterp::Text(t.clone())), + StrPart::Expr(e) => { + let h = self.expr(e, None)?; + out.push(HInterp::Value(h)); + } + } + } + Ok(mk(HExprKind::Interp(out), Ty::Str)) + } + + fn binary_typed(&self, op: BinOp, mut l: HExpr, mut r: HExpr, span: Span) -> PResult { + // `x * 2` where x is a float: the literal becomes a float + if l.ty != r.ty { + if is_int_lit(&l) && r.ty.is_number() || is_float_lit(&l) && r.ty.is_float() { + l = retype_lit(l, r.ty, span, false)?; + } else if is_int_lit(&r) && l.ty.is_number() || is_float_lit(&r) && l.ty.is_float() { + r = retype_lit(r, l.ty, span, false)?; + } + } + if l.ty != r.ty { + let hint = if matches!((l.ty, r.ty), (Ty::Int, Ty::Float) | (Ty::Float, Ty::Int)) { + "; convert one side with `float(...)` or `int(...)`".to_string() + } else if l.ty.is_number() && r.ty.is_number() { + format!("; convert one side, for example `{}(...)`", l.ty) + } else { + String::new() + }; + return Err(Diag::new( + format!("can't use `{}` between `{}` and `{}`{hint}", op_str(op), l.ty, r.ty), + span, + )); + } + let t = l.ty; + let ok = match op { + BinOp::Add => t.is_number() || t == Ty::Str, + BinOp::Sub | BinOp::Mul | BinOp::Div => t.is_number(), + BinOp::Rem => t.is_int(), + BinOp::Eq | BinOp::Ne => { + t.is_number() || matches!(t, Ty::Bool | Ty::Str | Ty::Char) + || matches!(t, Ty::Enum(n) if self.enums[n].variants.iter().all(|v| v.fields.is_empty())) + } + BinOp::Lt | BinOp::Gt | BinOp::Le | BinOp::Ge => t.is_number() || t == Ty::Str || t == Ty::Char, + BinOp::And | BinOp::Or => t == Ty::Bool, + }; + if !ok { + let hint = match t { + Ty::Struct(_) if matches!(op, BinOp::Eq | BinOp::Ne) => " (compare the fields one by one)", + Ty::Enum(_) if matches!(op, BinOp::Eq | BinOp::Ne) => " (use `match` for enums that carry data)", + _ => "", + }; + return Err(Diag::new(format!("`{}` doesn't work on `{t}` values{hint}", op_str(op)), span)); + } + let ty = match op { + BinOp::Eq | BinOp::Ne | BinOp::Lt | BinOp::Gt | BinOp::Le | BinOp::Ge => Ty::Bool, + _ => t, + }; + Ok(mk(HExprKind::Binary { op, lhs: Box::new(l), rhs: Box::new(r) }, ty)) + } + + fn check_args(&mut self, name: &str, params: &[Ty], args: &[Expr], span: Span) -> PResult> { + if args.len() != params.len() { + return Err(Diag::new( + format!( + "`{name}` takes {} argument{}, but {} {} given", + params.len(), + if params.len() == 1 { "" } else { "s" }, + args.len(), + if args.len() == 1 { "was" } else { "were" }, + ), + span, + )); + } + let mut hs = Vec::new(); + for (a, ty) in args.iter().zip(params) { + hs.push(self.expr_as_owned(a, *ty)?); + } + Ok(hs) + } + + /// Methods that lists and strings have built in. + fn builtin_method(&mut self, recv: HExpr, name: &str, args: &[Expr], span: Span, name_span: Span) -> PResult { + let want_args = |n: usize| -> PResult<()> { + if args.len() != n { + return Err(Diag::new( + format!("`{name}` takes {n} argument{}, but {} {} given", if n == 1 { "" } else { "s" }, args.len(), if args.len() == 1 { "was" } else { "were" }), + span, + )); + } + Ok(()) + }; + if recv.ty == Ty::Str && name != "len" { + if let Some(sig) = stdlib::str_method(name) { + return self.builtin_call(name, sig, Some(recv), args, span); + } + } + if matches!(recv.ty, Ty::List(e) if *e == Ty::Str) { + if let Some(sig) = stdlib::str_list_method(name) { + return self.builtin_call(name, sig, Some(recv), args, span); + } + } + if let Ty::Map(key, val) = recv.ty { + return self.map_method(recv, *key, *val, name, args, span, name_span); + } + match (recv.ty, name) { + (Ty::List(_) | Ty::Str, "len") => { + want_args(0)?; + Ok(mk(HExprKind::Len(Box::new(recv)), Ty::Int)) + } + (Ty::List(elem), "push") => { + want_args(1)?; + self.require_variable(&recv, "push", name_span)?; + let value = self.expr_as_owned(&args[0], *elem)?; + Ok(mk(HExprKind::ListPush { list: Box::new(recv), value: Box::new(value) }, Ty::Unit)) + } + (Ty::List(elem), "pop") => { + want_args(0)?; + self.require_variable(&recv, "pop", name_span)?; + Ok(mk(HExprKind::ListPop(Box::new(recv)), *elem)) + } + (Ty::List(_), _) => Err(Diag::new( + format!("lists have no method `{name}` (they have: len, push, pop)"), + name_span, + )), + (Ty::Str, _) => Err(Diag::new( + format!("strings have no method `{name}` (they have: {})", stdlib::STR_METHODS), + name_span, + )), + _ => unreachable!(), + } + } + + fn map_method(&mut self, recv: HExpr, key: Ty, val: Ty, name: &str, args: &[Expr], span: Span, name_span: Span) -> PResult { + let want = |n: usize| -> PResult<()> { + if args.len() != n { + return Err(Diag::new( + format!("`{name}` takes {n} argument{}, but {} {} given", if n == 1 { "" } else { "s" }, args.len(), if args.len() == 1 { "was" } else { "were" }), + span, + )); + } + Ok(()) + }; + let call = |recv: HExpr, op: MapOp, args: Vec, ty: Ty| mk(HExprKind::MapCall { map: Box::new(recv), op, args }, ty); + match name { + "len" => { + want(0)?; + Ok(mk(HExprKind::Len(Box::new(recv)), Ty::Int)) + } + "has" => { + want(1)?; + let k = self.expr_as(&args[0], key)?; + Ok(call(recv, MapOp::Has, vec![k], Ty::Bool)) + } + "get" => { + want(1)?; + let k = self.expr_as(&args[0], key)?; + Ok(call(recv, MapOp::Get, vec![k], Ty::opt(val))) + } + "get_or" => { + want(2)?; + let k = self.expr_as(&args[0], key)?; + let d = self.expr_as_owned(&args[1], val)?; + Ok(call(recv, MapOp::GetOr, vec![k, d], val)) + } + "remove" => { + want(1)?; + self.require_variable(&recv, "remove", name_span)?; + let k = self.expr_as(&args[0], key)?; + Ok(call(recv, MapOp::Remove, vec![k], Ty::Unit)) + } + "keys" => { + want(0)?; + Ok(call(recv, MapOp::Keys, vec![], Ty::list(key))) + } + "values" => { + want(0)?; + Ok(call(recv, MapOp::Values, vec![], Ty::list(val))) + } + _ => Err(Diag::new( + format!("maps have no method `{name}` (they have: len, has, get, get_or, remove, keys, values)"), + name_span, + )), + } + } + + /// Methods that change a list need a variable to change: not a constant, not a temporary. + fn require_variable(&self, recv: &HExpr, method: &str, span: Span) -> PResult<()> { + match place_root(recv) { + None => Err(Diag::new( + format!("`{method}` changes it, so it must be called on a variable, not a temporary value"), + span, + )), + Some((_, root)) if self.lookup(root).map_or(false, |l| l.is_const) => { + Err(Diag::new(format!("`{method}` changes `{root}`, but `{root}` is a constant"), span)) + } + Some(_) => Ok(()), + } + } + + fn method_call(&mut self, recv: HExpr, name: &str, args: &[Expr], span: Span, name_span: Span) -> PResult { + if let (Ty::Weak(t), "upgrade") = (recv.ty, name) { + if !args.is_empty() { + return Err(Diag::new("`upgrade` takes no arguments", span)); + } + return Ok(mk(HExprKind::Upgrade(Box::new(recv)), Ty::opt(Ty::Shared(t)))); + } + let recv = auto_deref(recv); + if let Ty::Iface(key) = recv.ty { + let Some((_, params, ret)) = self.ifaces[key].methods.iter().find(|(n, _, _)| n == name).cloned() else { + let known: Vec<&str> = self.ifaces[key].methods.iter().map(|(n, _, _)| n.as_str()).collect(); + return Err(Diag::new( + format!("`{key}` has no method `{name}` (it has: {})", known.join(", ")), + name_span, + )); + }; + let hs = self.check_args(name, ¶ms, args, span)?; + return Ok(mk(HExprKind::IfaceCall { recv: Box::new(recv), method: name.to_string(), args: hs }, ret)); + } + if recv.ty == Ty::Char { + return match stdlib::char_method(name) { + Some(sig) => self.builtin_call(name, sig, Some(recv), args, span), + None => Err(Diag::new( + format!("characters have no method `{name}` (they have: {})", stdlib::CHAR_METHODS), + name_span, + )), + }; + } + if matches!(recv.ty, Ty::List(_) | Ty::Str | Ty::Map(..)) { + return self.builtin_method(recv, name, args, span, name_span); + } + let (Ty::Struct(owner) | Ty::Enum(owner)) = recv.ty else { + return Err(Diag::new(format!("a `{}` has no methods (looking for `.{name}()`)", recv.ty), name_span)); + }; + let Some(sig) = self.methods_of(owner).and_then(|m| m.get(name)).cloned() else { + if let Ty::Enum(_) = recv.ty { + return Err(Diag::new(format!("enum `{owner}` has no method `{name}`"), name_span)); + } + // reuse the field error for a good "did you mean" list + return match self.field_of(recv, name, name_span) { + Ok(h) if matches!(h.ty, Ty::Func(_)) => self.call_value(h, name, args, span), + Ok(_) => Err(Diag::new(format!("`{name}` is a field, not a method"), name_span)), + Err(d) => Err(d), + }; + }; + let owner_mod = module_of(owner); + // generic code may call the methods an `interface` asks for on the types it was given + let via_rule = self.tsubst.values().any(|t| matches!(t, Ty::Struct(n) | Ty::Enum(n) if *n == owner)) + && self.ifaces.values().any(|i| i.methods.iter().any(|(n, _, _)| n == name)); + if owner_mod != self.cur_mod && !via_rule && self.private.contains(&format!("{owner}.{name}")) { + return Err(Diag::new( + format!("method `{name}` is private to `{owner_mod}`; write `pub` in front of it in {owner_mod}.dino to share it"), + name_span, + )); + } + let hs = self.check_args(name, &sig.params, args, span)?; + Ok(mk( + HExprKind::MethodCall { + owner: owner.to_string(), + method: name.to_string(), + recv: Box::new(recv), + args: hs, + span, + }, + sig.ret, + )) + } + + /// If `e` is just the name of an enum (not shadowed by a variable), that enum. + fn enum_named(&self, e: &Expr) -> Option<&'static str> { + if let ExprKind::Ident(n) = &e.kind { + if self.lookup(n).is_some() || self.implicit_field(n).is_some() || self.consts.contains_key(&self.q(n)) { + return None; + } + } + let key = self.global_key(e)?; + self.enums.get(&key).map(|i| i.name) + } + + /// `Shape.Circle(2.0)` or `Shape.Point`. + fn enum_value(&mut self, enum_name: &'static str, case: &str, args: Option<&[Expr]>, span: Span) -> PResult { + let info = &self.enums[enum_name]; + let Some(idx) = info.variants.iter().position(|v| v.name == case) else { + let cases: Vec<&str> = info.variants.iter().map(|v| v.name.as_str()).collect(); + return Err(Diag::new( + format!("enum `{enum_name}` has no case `{case}` (its cases: {})", cases.join(", ")), + span, + )); + }; + let fields = info.variants[idx].fields.clone(); + let args = args.unwrap_or(&[]); + if args.len() != fields.len() { + let what = if fields.is_empty() { + format!("`{enum_name}.{case}` has no data, so write it without parentheses") + } else { + let names: Vec = fields.iter().map(|(n, t)| format!("{n}: {t}")).collect(); + format!("`{enum_name}.{case}` needs {} value{}: ({})", fields.len(), if fields.len() == 1 { "" } else { "s" }, names.join(", ")) + }; + return Err(Diag::new(what, span)); + } + let mut values = Vec::new(); + for (a, (_, ty)) in args.iter().zip(&fields) { + values.push(self.expr_as_owned(a, *ty)?); + } + Ok(mk( + HExprKind::EnumLit { enum_name: enum_name.to_string(), variant: idx, fields: values }, + Ty::Enum(enum_name), + )) + } + + fn call(&mut self, callee: &Expr, args: &[Expr], span: Span) -> PResult { + match &callee.kind { + ExprKind::Field { expr, name } if self.module_alias(expr).is_some() => { + let m = self.module_alias(expr).unwrap(); + self.module_call(m, name, args, span, callee.span) + } + ExprKind::Field { expr, name } if matches!(expr.kind, ExprKind::Generic { .. }) => { + let ExprKind::Generic { expr: base, args: targs } = &expr.kind else { unreachable!() }; + let Ty::Enum(en) = self.generic_expr_type(base, targs, expr.span)? else { + return Err(Diag::new("only enums have cases to pick with `.`", callee.span)); + }; + self.enum_value(en, name, Some(args), span) + } + ExprKind::Field { expr, name } if self.user_alias(expr).is_some() => { + let module = self.user_alias(expr).unwrap(); + let key = format!("{module}.{name}"); + self.check_visible(&key, callee.span)?; + self.call_user_func(&key, name, callee.span, args, span) + } + ExprKind::Field { expr, name } if self.enum_named(expr).is_some() => { + let e = self.enum_named(expr).unwrap(); + self.check_visible(e, expr.span)?; + self.enum_value(e, name, Some(args), span) + } + ExprKind::Field { expr, name } => { + let recv = self.expr(expr, None)?; + self.method_call(recv, name, args, span, callee.span) + } + ExprKind::Ident(name) => self.call_named(name, callee.span, args, span), + _ => { + let h = self.expr(callee, None)?; + if matches!(h.ty, Ty::Func(_)) { + self.call_value(h, "this function", args, span) + } else { + Err(Diag::new(format!("can't call a `{}`", h.ty), callee.span)) + } + } + } + } + + fn call_named(&mut self, name: &str, name_span: Span, args: &[Expr], span: Span) -> PResult { + if let Some(l) = self.lookup(name) { + if let Ty::Func(_) = l.ty { + let callee = mk(HExprKind::Local(l.id, name.to_string()), l.ty); + return self.call_value(callee, name, args, span); + } + return Err(Diag::new(format!("`{name}` is a variable, not a function"), name_span)); + } + // inside a method, a bare name can be another method of the same struct + if let Some(owner) = self.cur_struct { + if self.methods_of(owner).map_or(false, |m| m.contains_key(name)) { + let recv = self.self_local(); + return self.method_call(recv, name, args, span, name_span); + } + if self.structs.get(owner).map_or(false, |i| i.fields.iter().any(|(n, _)| n == name)) { + if let Some(h) = self.implicit_field(name) { + if matches!(h.ty, Ty::Func(_)) { + return self.call_value(h, name, args, span); + } + } + return Err(Diag::new(format!("`{name}` is a field, not a method"), name_span)); + } + } + match name { + "print" => { + let mut hs = Vec::new(); + for a in args { + hs.push(self.expr(a, None)?); + } + Ok(mk(HExprKind::Print(hs), Ty::Unit)) + } + "error" => { + if args.len() != 1 { + return Err(Diag::new("`error` takes exactly 1 argument: the message", span)); + } + let msg = self.expr_as_owned(&args[0], Ty::Str)?; + Ok(mk(HExprKind::MakeError(Box::new(msg)), Ty::Error)) + } + "string" => { + if args.len() != 1 { + return Err(Diag::new("`string` takes exactly 1 argument", span)); + } + let a = self.expr(&args[0], None)?; + Ok(mk(HExprKind::Interp(vec![HInterp::Value(a)]), Ty::Str)) + } + n if conversion_target(n).is_some() => { + if args.len() != 1 { + return Err(Diag::new(format!("`{name}` takes exactly 1 argument"), span)); + } + let target = conversion_target(n).unwrap(); + let a = self.expr(&args[0], None)?; + match a.ty { + t if t == target => Ok(a), + // a character and its number: `int('a')` is 97, `char(97)` is 'a' + Ty::Char if target.is_int() => Ok(mk(HExprKind::Convert(Box::new(a)), target)), + t if t.is_int() && target == Ty::Char => Ok(mk(HExprKind::Convert(Box::new(a)), target)), + t if t.is_number() && target != Ty::Char => Ok(mk(HExprKind::Convert(Box::new(a)), target)), + t => Err(Diag::new(format!("can't convert a `{t}` to `{target}`"), args[0].span)), + } + } + _ => { + let key = self.q(name); + self.call_user_func(&key, name, name_span, args, span) + } + } + } + + fn expected_fn(expect: Option) -> (Option>, Option) { + match expect { + Some(Ty::Func(f)) => (Some(f.params.clone()), Some(f.ret)), + Some(Ty::Opt(Ty::Func(f))) => (Some(f.params.clone()), Some(f.ret)), + _ => (None, None), + } + } + + /// `func(x: int): int { ... }` and `x => x * 2`. Variables of the surrounding function that the body + /// uses are copied into the closure when it is made; from then on it has its own copies. + fn closure_expr( + &mut self, + params: &[(String, Option<&Type>)], + ret: Option<&Type>, + body: ClosureBody, + exp_params: Option>, + exp_ret: Option, + span: Span, + ) -> PResult { + // parameter types: written, or taken from where the closure is used + let mut ptys = Vec::new(); + for (i, (name, ty)) in params.iter().enumerate() { + let t = match ty { + Some(t) => self.resolve_type(t, span)?, + None => match &exp_params { + Some(ps) if ps.len() == params.len() => ps[i], + Some(ps) => { + return Err(Diag::new( + format!( + "this function has {} parameter{}, but {} {} expected here", + params.len(), + if params.len() == 1 { "" } else { "s" }, + ps.len(), + if ps.len() == 1 { "is" } else { "are" } + ), + span, + )) + } + None => { + return Err(Diag::new( + format!( + "can't tell what type `{name}` is here; write it out: `func({name}: int): int {{ ... }}`" + ), + span, + )) + } + }, + }; + ptys.push(t); + } + let mut ret_ty: Option = match ret { + Some(t) => Some(self.resolve_type(t, span)?), + None => match body { + ClosureBody::Block(_) => Some(Ty::Unit), + ClosureBody::Expr(_) => exp_ret, + }, + }; + if matches!(ret_ty, Some(Ty::Tuple(_))) { + return unsupported(span, "a function value with several results"); + } + + // what the body uses from outside + let mut used = HashSet::new(); + match &body { + ClosureBody::Block(b) => idents_block(b, &mut used), + ClosureBody::Expr(x) => idents_expr(x, &mut used), + } + let mut names: Vec = used + .iter() + .filter(|n| !params.iter().any(|(p, _)| p == *n) && self.lookup(n).is_some()) + .cloned() + .collect(); + let wants_self = self.cur_struct.is_some() + && !params.iter().any(|(p, _)| p == "self") + && self.lookup("self").is_some() + && used.iter().any(|n| { + self.lookup(n).is_none() + && (self.implicit_field(n).is_some() + || self.methods_of(self.cur_struct.unwrap()).map_or(false, |m| m.contains_key(n))) + }); + if wants_self && !names.iter().any(|n| n == "self") { + names.push("self".to_string()); + } + names.sort(); + let mut outer: Vec<(String, Local, HExpr)> = Vec::new(); + for n in names { + let l = self.lookup(&n).unwrap(); + let l = Local { id: l.id, ty: l.ty, is_const: l.is_const }; + let view = mk(HExprKind::Local(l.id, n.clone()), l.ty); + let owned = self.own(view); + outer.push((n, l, owned)); + } + + // check the body as a function of its own + let saved_scopes = std::mem::replace(&mut self.scopes, vec![HashMap::new()]); + let saved_ret = self.ret; + let saved_loops = std::mem::replace(&mut self.loops, 0); + let saved_narrowed = std::mem::take(&mut self.narrowed); + let saved_no_move = std::mem::take(&mut self.no_move); + let r = self.closure_inner(&outer, params, &ptys, &mut ret_ty, body, span); + self.scopes = saved_scopes; + self.ret = saved_ret; + self.loops = saved_loops; + self.narrowed = saved_narrowed; + self.no_move = saved_no_move; + let (inner_caps, plocals, stmts) = r?; + let ret = ret_ty.unwrap_or(Ty::Unit); + let captures: Vec<(HLocal, HExpr)> = + inner_caps.into_iter().zip(outer.into_iter().map(|(_, _, h)| h)).collect(); + Ok(mk( + HExprKind::Closure { params: plocals, ret, body: stmts, captures }, + Ty::func(ptys, ret), + )) + } + + fn closure_inner( + &mut self, + outer: &[(String, Local, HExpr)], + params: &[(String, Option<&Type>)], + ptys: &[Ty], + ret_ty: &mut Option, + body: ClosureBody, + span: Span, + ) -> PResult<(Vec, Vec, Vec)> { + let mut caps = Vec::new(); + for (n, l, _) in outer { + let inner = self.declare(n, l.ty, l.is_const, span)?; + self.no_move.insert(inner.id); + caps.push(inner); + } + let mut plocals = Vec::new(); + for ((name, _), ty) in params.iter().zip(ptys) { + let l = self.declare(name, *ty, false, span)?; + self.no_move.insert(l.id); + plocals.push(l); + } + self.ret = ret_ty.unwrap_or(Ty::Unit); + let stmts = match body { + ClosureBody::Block(b) => { + let stmts = self.stmts(&b.stmts)?; + let rt = ret_ty.unwrap_or(Ty::Unit); + if rt != Ty::Unit && !always_returns(&stmts) { + return Err(Diag::new( + format!("this function can reach its end without returning a `{rt}`"), + b.span, + )); + } + stmts + } + ClosureBody::Expr(x) => match *ret_ty { + Some(Ty::Unit) => vec![HStmt::Expr(self.expr(x, None)?)], + Some(rt) => { + let h = self.expr_as_owned(x, rt)?; + vec![HStmt::Return(Some(h))] + } + None => { + let h = self.expr(x, None)?; + if matches!(h.ty, Ty::Tuple(_)) { + return unsupported(x.span, "a function value with several results"); + } + *ret_ty = Some(h.ty); + if h.ty == Ty::Unit { + vec![HStmt::Expr(h)] + } else { + let h = self.own(h); + vec![HStmt::Return(Some(h))] + } + } + }, + }; + Ok((caps, plocals, stmts)) + } + + /// A call of a function value (a variable, a field, or any expression of a function type). + fn call_value(&mut self, callee: HExpr, name: &str, args: &[Expr], span: Span) -> PResult { + let Ty::Func(ft) = callee.ty else { unreachable!() }; + let hs = self.check_args(name, &ft.params, args, span)?; + Ok(mk(HExprKind::CallValue { callee: Box::new(callee), args: hs }, ft.ret)) + } + + /// The name of a top-level function used as a value: `apply(double, 3)`. + fn func_value(&mut self, key: &str, name: &str, span: Span) -> PResult { + let sig = self.funcs[key].clone(); + if matches!(sig.ret, Ty::Tuple(_)) { + return Err(Diag::new( + format!("`{name}` returns several values, so it can't be used as a value yet"), + span, + )); + } + let mut params = Vec::new(); + let mut args = Vec::new(); + for (i, t) in sig.params.iter().enumerate() { + let id = self.next_id; + self.next_id += 1; + let pname = format!("p{i}"); + args.push(self.own(mk(HExprKind::Local(id, pname.clone()), *t))); + params.push(HLocal { id, name: pname, ty: *t, is_const: false }); + } + let call = mk(HExprKind::Call { func: key.to_string(), args }, sig.ret); + let body = if sig.ret == Ty::Unit { vec![HStmt::Expr(call)] } else { vec![HStmt::Return(Some(call))] }; + Ok(mk( + HExprKind::Closure { params, ret: sig.ret, body, captures: Vec::new() }, + Ty::func(sig.params.clone(), sig.ret), + )) + } + + fn declare_generic_type(&mut self, name: &str, is_pub: bool, span: Span, tps: &[TypeParam]) -> PResult<()> { + self.check_type_name_free(name, span)?; + for (i, tp) in tps.iter().enumerate() { + if tps[..i].iter().any(|o| o.name == tp.name) { + return Err(Diag::new(format!("two type parameters are named `{}`", tp.name), span)); + } + } + let bounds = self.resolve_bounds(tps, span)?; + let key = self.q(name); + self.bounds_of.insert(key.clone(), bounds); + self.mark_private(&key, is_pub); + Ok(()) + } + + /// `Stack[int]`: the real struct or enum for these types, made the first time it is asked for. + fn instantiate_type(&mut self, key: &str, shown: &str, tys: Vec, span: Span) -> PResult { + let g = self.gtypes[key].clone(); + let (tps, module) = match &g { + GenericType::Struct(s, m) => (s.type_params.clone(), m.clone()), + GenericType::Enum(e, m) => (e.type_params.clone(), m.clone()), + }; + if tys.len() != tps.len() { + return Err(Diag::new( + format!( + "`{shown}` takes {} type{} (like `{shown}[{}]`), but {} {} given", + tps.len(), + if tps.len() == 1 { "" } else { "s" }, + tps.iter().map(|_| "int").collect::>().join(", "), + tys.len(), + if tys.len() == 1 { "was" } else { "were" } + ), + span, + )); + } + for (i, (tp, ty)) in tps.iter().zip(&tys).enumerate() { + if let Some(b) = &self.bounds_of[key][i] { + if let Some((what, why)) = self.bound_failure(b, *ty) { + return Err(Diag::new( + format!("`{shown}` needs `{}` to be {what}, but {why}", tp.name), + span, + )); + } + } + } + let inst_key = format!("{key}[{}]", tys.iter().map(|t| t.to_string()).collect::>().join(", ")); + if let Some(i) = self.structs.get(&inst_key) { + return Ok(Ty::Struct(i.name)); + } + if let Some(i) = self.enums.get(&inst_key) { + return Ok(Ty::Enum(i.name)); + } + let types: HashMap = tps.iter().map(|t| t.name.clone()).zip(tys).collect(); + // check the inside of the type as if we were in the module that wrote it + let saved_subst = std::mem::replace(&mut self.tsubst, types.clone()); + let saved_mod = std::mem::replace(&mut self.cur_mod, module.clone()); + let saved_imports = std::mem::replace(&mut self.imports, self.all_imports.get(&module).cloned().unwrap_or_default()); + let r = self.build_instance(&g, &inst_key, &module, types); + self.tsubst = saved_subst; + self.cur_mod = saved_mod; + self.imports = saved_imports; + r.map_err(|d| d.in_module(&module)) + } + + fn build_instance(&mut self, g: &GenericType, inst_key: &str, module: &str, types: HashMap) -> PResult { + let name: &'static str = Box::leak(inst_key.to_string().into_boxed_str()); + match g { + GenericType::Struct(s, _) => { + self.structs.insert( + inst_key.to_string(), + StructInfo { name, fields: Vec::new(), methods: HashMap::new(), span: s.span }, + ); + let mut fields: Vec<(String, Ty)> = Vec::new(); + for f in &s.fields { + if fields.iter().any(|(n, _)| *n == f.name) { + return Err(Diag::new(format!("struct `{}` has two fields named `{}`", s.name, f.name), s.span)); + } + let fty = self.resolve_type(&f.ty, s.span)?; + if matches!(fty, Ty::Opt(_)) { + return unsupported(s.span, "an optional (`T?`) as a struct field"); + } + self.mark_private(&format!("{inst_key}.{}", f.name), f.is_pub); + fields.push((f.name.clone(), fty)); + } + self.structs.get_mut(inst_key).unwrap().fields = fields; + self.declare_methods(inst_key, &s.methods)?; + for m in &s.methods { + self.inst_queue.push(Instance { + decl: m.clone(), + module: module.to_string(), + types: types.clone(), + owner: Some(inst_key.to_string()), + }); + } + Ok(Ty::Struct(name)) + } + GenericType::Enum(e, _) => { + self.enums.insert( + inst_key.to_string(), + EnumInfo { name, variants: Vec::new(), methods: HashMap::new(), span: e.span }, + ); + let mut variants: Vec = Vec::new(); + for v in &e.variants { + if variants.iter().any(|x| x.name == v.name) { + return Err(Diag::new(format!("enum `{}` has two cases named `{}`", e.name, v.name), e.span)); + } + let mut fields: Vec<(String, Ty)> = Vec::new(); + for f in &v.fields { + let fty = self.resolve_type(&f.ty, e.span)?; + if matches!(fty, Ty::Opt(_)) { + return unsupported(e.span, "an optional (`T?`) as the data of an enum case"); + } + fields.push((f.name.clone(), fty)); + } + variants.push(VariantInfo { name: v.name.clone(), fields }); + } + self.enums.get_mut(inst_key).unwrap().variants = variants; + self.declare_methods(inst_key, &e.methods)?; + for m in &e.methods { + self.inst_queue.push(Instance { + decl: m.clone(), + module: module.to_string(), + types: types.clone(), + owner: Some(inst_key.to_string()), + }); + } + Ok(Ty::Enum(name)) + } + } + } + + /// The type written as `base[args]` in an expression. + fn generic_expr_type(&mut self, base: &Expr, args: &[Type], span: Span) -> PResult { + let path = match &base.kind { + ExprKind::Ident(n) => vec![n.clone()], + ExprKind::Field { expr, name } => match &expr.kind { + ExprKind::Ident(m) => vec![m.clone(), name.clone()], + _ => return unsupported(span, "this kind of type"), + }, + _ => return unsupported(span, "this kind of type"), + }; + self.resolve_type(&Type::Named { path, args: args.to_vec() }, span) + } + + /// A call of a generic function: work out the types, make sure that copy exists, call it. + fn call_generic(&mut self, key: &str, name: &str, g: &GenericFn, args: &[Expr], span: Span) -> PResult { + let f = &g.decl; + if args.len() != f.params.len() { + return Err(Diag::new( + format!( + "`{name}` takes {} argument{}, but {} {} given", + f.params.len(), + if f.params.len() == 1 { "" } else { "s" }, + args.len(), + if args.len() == 1 { "was" } else { "were" }, + ), + span, + )); + } + // 1. what is each type parameter? read it off the arguments + let mut found: HashMap = HashMap::new(); + let mut weak: Vec<(String, bool)> = Vec::new(); // (param, is a float literal) for bare number literals + let saved = std::mem::take(&mut self.tsubst); + let mut deferred: Vec<(&Param, &Expr)> = Vec::new(); + for (p, a) in f.params.iter().zip(args) { + if !mentions_type_param(&p.ty, &f.type_params) { + continue; + } + if matches!(a.kind, ExprKind::Lambda { .. }) { + deferred.push((p, a)); + continue; + } + let h = match self.expr(a, None) { + Ok(h) => h, + Err(d) => { + self.tsubst = saved; + return Err(d); + } + }; + if let Type::Named { path, args: targs } = &p.ty { + if path.len() == 1 && targs.is_empty() && f.type_params.iter().any(|t| t.name == path[0]) { + if is_int_lit(&h) || is_float_lit(&h) { + weak.push((path[0].clone(), is_float_lit(&h))); + continue; + } + } + } + if let Err(msg) = unify(&p.ty, h.ty, &f.type_params, &mut found) { + self.tsubst = saved; + return Err(Diag::new(msg, a.span)); + } + } + for (t, is_float) in weak { + let e = found.entry(t).or_insert(if is_float { Ty::Float } else { Ty::Int }); + if is_float && *e == Ty::Int { + *e = Ty::Float; + } + } + // `x => x * 2`: its parameter types come from what is already known; its result may tell us more + for (p, a) in deferred { + let (Type::Func { params: pp, .. }, ExprKind::Lambda { params: names, body }) = (&p.ty, &a.kind) else { + self.tsubst = saved; + return Err(Diag::new("this short function (`x => ...`) needs a parameter of a function type", a.span)); + }; + self.tsubst = found.clone(); + let mut ptys = Vec::new(); + for t in pp { + match self.resolve_type(t, a.span) { + Ok(t) => ptys.push(t), + Err(_) => { + self.tsubst = saved; + return Err(Diag::new( + format!("can't tell the types of this function's parameters in this call of `{name}`; write them out: `func(x: int): int {{ ... }}`"), + a.span, + )); + } + } + } + self.tsubst = std::mem::take(&mut HashMap::new()); + let ps: Vec<(String, Option<&Type>)> = names.iter().map(|n| (n.clone(), None)).collect(); + let h = match self.closure_expr(&ps, None, ClosureBody::Expr(body), Some(ptys), None, a.span) { + Ok(h) => h, + Err(d) => { + self.tsubst = saved; + return Err(d); + } + }; + if let Err(msg) = unify(&p.ty, h.ty, &f.type_params, &mut found) { + self.tsubst = saved; + return Err(Diag::new(msg, a.span)); + } + } + self.tsubst = saved; + for (i, tp) in f.type_params.iter().enumerate() { + let Some(ty) = found.get(&tp.name) else { + return Err(Diag::new( + format!("can't tell what `{}` is in this call of `{name}`; give an argument that has that type", tp.name), + span, + )); + }; + if let Some(b) = &self.bounds_of[key][i] { + if let Some((what, why)) = self.bound_failure(b, *ty) { + return Err(Diag::new(format!("`{name}` needs `{}` to be {what}, but {why}", tp.name), span)); + } + } + } + // 2. the copy for these types + let tys: Vec = f.type_params.iter().map(|t| found[&t.name].to_string()).collect(); + let inst_name = format!("{}[{}]", f.name, tys.join(", ")); + let inst_key = format!("{key}[{}]", tys.join(", ")); + if !self.funcs.contains_key(&inst_key) { + let mut decl = f.clone(); + decl.name = inst_name; + decl.type_params = Vec::new(); + let saved = std::mem::replace(&mut self.tsubst, found.clone()); + let saved_mod = std::mem::replace(&mut self.cur_mod, g.module.clone()); + let sig = self.sig_of(&decl); + self.cur_mod = saved_mod; + self.tsubst = saved; + let sig = sig.map_err(|d| d.in_module(&g.module))?; + self.funcs.insert(inst_key.clone(), sig); + self.inst_queue.push(Instance { decl, module: g.module.clone(), types: found, owner: None }); + } + let sig = self.funcs[&inst_key].clone(); + let hs = self.check_args(name, &sig.params, args, span)?; + Ok(mk(HExprKind::Call { func: inst_key, args: hs }, sig.ret)) + } + + /// Call the function stored under `key`; `name` is how it was written. + fn call_user_func(&mut self, key: &str, name: &str, name_span: Span, args: &[Expr], span: Span) -> PResult { + if let Some(g) = self.generics.get(key).cloned() { + return self.call_generic(key, name, &g, args, span); + } + let Some(sig) = self.funcs.get(key) else { + return Err(Diag::new(format!("unknown function `{name}`"), name_span)); + }; + let (params, ret) = (sig.params.clone(), sig.ret); + let hs = self.check_args(name, ¶ms, args, span)?; + Ok(mk(HExprKind::Call { func: key.to_string(), args: hs }, ret)) + } +} + +// ---------- mutation inference ---------- + +/// A method "mutates" if its body assigns to something inside `self`, or calls +/// a mutating method on something inside `self`. Found by iterating to a fixpoint. +fn infer_mutation(funcs: &mut [HFunc]) { + let mut mutating: HashSet<(String, String)> = HashSet::new(); + loop { + let mut changed = false; + for f in funcs.iter_mut() { + let (Some(owner), Some(me)) = (&f.owner, &f.self_param) else { continue }; + if f.mutates { + continue; + } + let self_id = me.id; + let mut found = false; + walk_stmts( + &f.body, + &mut |s| { + if let HStmt::Assign { target, .. } = s { + if place_root(target).map(|(id, _)| id) == Some(self_id) { + found = true; + } + } + }, + &mut |_| {}, + ); + walk_stmts( + &f.body, + &mut |_| {}, + &mut |e| { + if expr_mutates_root(e, self_id, &|o, m| mutating.contains(&(o.to_string(), m.to_string()))) { + found = true; + } + }, + ); + if found { + f.mutates = true; + mutating.insert((owner.clone(), f.name.clone())); + changed = true; + } + } + if !changed { + break; + } + } +} + +/// Mutating methods need a variable to change: not a constant, not a temporary value. +fn check_mutating_calls(funcs: &[HFunc]) -> PResult<()> { + let mutating: HashSet<(&str, &str)> = funcs + .iter() + .filter(|f| f.mutates) + .map(|f| (f.owner.as_deref().unwrap(), f.name.as_str())) + .collect(); + for f in funcs { + let mut consts: HashSet = HashSet::new(); + walk_stmts( + &f.body, + &mut |s| { + if let HStmt::Let { local, .. } = s { + if local.is_const { + consts.insert(local.id); + } + } + }, + &mut |_| {}, + ); + let mut err: Option = None; + walk_stmts( + &f.body, + &mut |_| {}, + &mut |e| { + let HExprKind::MethodCall { owner, method, recv, span, .. } = &e.kind else { return }; + if err.is_some() || !mutating.contains(&(owner.as_str(), method.as_str())) { + return; + } + let msg = match place_root(recv) { + _ if through_shared(recv) => None, + None => Some(format!( + "`{method}` changes the {owner} it is called on, but this value is a temporary or a constant" + )), + Some((id, name)) if consts.contains(&id) => { + Some(format!("`{method}` changes `{name}`, but `{name}` is a constant")) + } + _ => None, + }; + if let Some(m) = msg { + err = Some(Diag::new(m, *span)); + } + }, + ); + if let Some(d) = err { + return Err(d); + } + } + Ok(()) +} diff --git a/src/codegen_c.rs b/src/codegen_c.rs new file mode 100644 index 0000000..9d9bf19 --- /dev/null +++ b/src/codegen_c.rs @@ -0,0 +1,2650 @@ +//! Emits C from the typed tree. +//! +//! Evaluation order: every call (and every operation that can panic) is stored +//! in a temporary in its own statement, so Dino's left-to-right order holds even +//! though C leaves argument order unspecified. An operand read before a later +//! operand that changes things (or may panic) is copied to a temporary first. +//! +//! Data: structs are plain C structs. Lists are `{ptr, len, cap}` structs, one +//! generated type per element type. Values with heap data are deep-copied where +//! the typed tree says so (`Copy`). Methods take `self` by pointer, and so do +//! parameters that are never changed by their function ("borrowed"). + +use std::collections::{HashMap, HashSet}; + +use crate::ast::{BinOp, UnOp}; +use crate::hir::*; +use crate::types::{Num, Ty}; + +const RUNTIME: &str = include_str!("runtime.c"); + +// ---------- program-wide facts ---------- + +struct Info { + /// Types of the data in every struct (its fields) and enum (the data of all its cases). + types: HashMap>, + /// Methods that change `self`: (struct, method). + mutating: HashSet<(String, String)>, + /// For every function (by C name): which parameters are borrowed by pointer. + borrowed: HashMap>, +} + +impl Info { + fn needs_copy(&self, ty: Ty) -> bool { + match ty { + Ty::List(_) | Ty::Map(..) | Ty::Str | Ty::Error | Ty::Shared(_) | Ty::Weak(_) | Ty::Func(_) | Ty::Iface(_) => true, + Ty::Opt(inner) => self.needs_copy(*inner), + Ty::Tuple(items) => items.iter().any(|t| self.needs_copy(*t)), + Ty::Struct(n) | Ty::Enum(n) => self.types[n].iter().any(|t| self.needs_copy(*t)), + _ => false, + } + } + + fn needs_copy_named(&self, name: &str) -> bool { + self.types[name].iter().any(|t| self.needs_copy(*t)) + } + + fn is_mut_method(&self, owner: &str, method: &str) -> bool { + self.mutating.contains(&(owner.to_string(), method.to_string())) + } + + fn has_mutation(&self, e: &HExpr) -> bool { + let mut found = false; + walk_expr(e, &mut |_| {}, &mut |x| match &x.kind { + HExprKind::ListPush { .. } | HExprKind::ListPop(_) | HExprKind::MapCall { op: MapOp::Remove, .. } => found = true, + HExprKind::MethodCall { owner, method, .. } if self.is_mut_method(owner, method) => found = true, + _ => {} + }); + found + } +} + +fn has_index(e: &HExpr) -> bool { + let mut found = false; + walk_expr(e, &mut |_| {}, &mut |x| { + if matches!(x.kind, HExprKind::Index { .. } | HExprKind::Slice { .. } | HExprKind::MapCall { .. }) { + found = true; + } + }); + found +} + +pub fn generate(p: &HProgram) -> String { + let mut info = Info { types: HashMap::new(), mutating: HashSet::new(), borrowed: HashMap::new() }; + let mut enums: HashMap = HashMap::new(); + for t in &p.types { + match t { + HTypeDef::Struct(s) => { + info.types.insert(s.name.clone(), s.fields.iter().map(|(_, t)| *t).collect()); + } + HTypeDef::Enum(e) => { + info.types.insert( + e.name.clone(), + e.variants.iter().flat_map(|v| v.fields.iter().map(|(_, t)| *t)).collect(), + ); + enums.insert(e.name.clone(), e.clone()); + } + } + } + for f in &p.funcs { + if let (Some(o), true) = (&f.owner, f.mutates) { + info.mutating.insert((o.clone(), f.name.clone())); + } + } + for f in &p.funcs { + let flags: Vec = f + .params + .iter() + .map(|prm| { + info.needs_copy(prm.ty) + && !mutates_root(&f.body, prm.id, &|o, m| info.mutating.contains(&(o.to_string(), m.to_string()))) + }) + .collect(); + info.borrowed.insert(func_cname(f.owner.as_deref(), &f.name), flags); + } + + let all_lists = collect_lists(p); + let lists: Vec = all_lists.iter().copied().filter(|t| matches!(t, Ty::List(_))).collect(); + let maps: Vec = all_lists.iter().copied().filter(|t| matches!(t, Ty::Map(..))).collect(); + let shareds: Vec = all_lists.iter().copied().filter(|t| matches!(t, Ty::Shared(_) | Ty::Weak(_))).collect(); + let iface_pairs = collect_iface_pairs(p); + let fn_types: Vec = all_lists.iter().copied().filter(|t| matches!(t, Ty::Func(_))).collect(); + let mut boxes: Vec = Vec::new(); + for t in &shareds { + let (Ty::Shared(i) | Ty::Weak(i)) = t else { unreachable!() }; + if !boxes.iter().any(|b| c_ty(*b) == c_ty(**i)) { + boxes.push(**i); + } + } + let wrappers = collect_wrappers(p); + let main_fails = p.funcs.iter().any(|f| f.owner.is_none() && f.name == "main" && f.ret == Ty::Error); + let mut g = Gen { out: String::new(), indent: 0, tmp: 0, ref_ids: HashSet::new(), info, enums, cur_ret: Ty::Unit, scopes: Vec::new(), pending: Vec::new(), loops: Vec::new(), closures: 0, closure_protos: String::new(), closure_defs: String::new() }; + g.out.push_str(RUNTIME); + g.out.push('\n'); + + // forward declarations, then bodies: lists first (they only hold pointers), then structs and enums + for t in &p.types { + g.out.push_str(&format!("typedef struct {0} {0};\n", c_struct(t.name()))); + } + for l in &lists { + g.out.push_str(&format!("typedef struct {0} {0};\n", c_ty(*l))); + } + for m in &maps { + g.out.push_str(&format!("typedef struct {0} {0};\n", c_ty(*m))); + } + for f in &fn_types { + g.out.push_str(&format!("typedef struct {0} {0};\n", c_ty(*f))); + } + for f in &fn_types { + let Ty::Func(ft) = f else { unreachable!() }; + let mut ps = vec!["void *".to_string()]; + ps.extend(ft.params.iter().map(|p| c_ty(*p))); + g.out.push_str(&format!( + "struct {} {{ {} (*fn)({}); void *env; void *(*copy)(void *); void (*drop)(void *); }};\n", + c_ty(*f), + c_ty(ft.ret), + ps.join(", ") + )); + } + for (name, methods) in &p.ifaces { + let c = c_ty(Ty::Iface(Box::leak(name.clone().into_boxed_str()))); + g.out.push_str(&format!("typedef struct {0} {0};\n", c)); + let mut members = String::new(); + for (m, params, ret) in methods { + let mut ps = vec!["void *".to_string()]; + ps.extend(params.iter().map(|t| c_ty(*t))); + members.push_str(&format!(" {} (*m_{})({});", c_ty(*ret), mangle(m), ps.join(", "))); + } + g.out.push_str(&format!( + "struct VT_{c} {{ void *(*copy)(void *); void (*drop)(void *); dstr (*str)(void *);{members} }};\n\ + struct {c} {{ void *data; const struct VT_{c} *vt; }};\n" + )); + } + for b in &boxes { + g.out.push_str(&format!("typedef struct {0} {0};\n", box_name(*b))); + } + for s in &shareds { + g.out.push_str(&format!("typedef struct {0} {0};\n", c_ty(*s))); + } + for s in &shareds { + let (Ty::Shared(i) | Ty::Weak(i)) = s else { unreachable!() }; + g.out.push_str(&format!("struct {} {{ {} *p; }};\n", c_ty(*s), box_name(**i))); + } + for l in &lists { + let Ty::List(elem) = *l else { unreachable!() }; + if *elem == Ty::Str { + continue; // `[]string` is defined by the runtime, which builds such lists itself + } + g.out.push_str(&format!( + "struct {} {{ {} *p; int64_t len; int64_t cap; }};\n", + c_ty(*l), + c_ty(*elem) + )); + } + for m in &maps { + let Ty::Map(k, v) = *m else { unreachable!() }; + g.out.push_str(&format!( + "struct {} {{ {} *k; {} *v; int64_t len; int64_t cap; int64_t *idx; int64_t icap; }};\n", + c_ty(*m), + c_ty(*k), + c_ty(*v) + )); + } + for t in &p.types { + match t { + HTypeDef::Struct(s) => g.struct_def(s), + HTypeDef::Enum(e) => g.enum_def(e), + } + } + + for b in &boxes { + g.out.push_str(&format!( + "struct {} {{ int64_t rc; int64_t wk; {} v; }};\n", + box_name(*b), + c_ty(*b) + )); + } + + // optionals and multiple results are plain structs over the types above + for w in &wrappers { + g.wrapper_def(*w); + } + + // prototypes + for l in &lists { + g.out.push_str(&g.list_protos(*l)); + } + for m in &maps { + g.out.push_str(&g.map_protos(*m)); + } + for w in &wrappers { + g.out.push_str(&g.wrapper_protos(*w)); + } + for s in &shareds { + g.out.push_str(&g.shared_protos(*s)); + } + for f in &fn_types { + let c = c_ty(*f); + g.out.push_str(&format!( + "static {c} {c}_copy({c} v);\nstatic void {c}_drop({c} *v);\nstatic dstr {c}_str({c} v);\n" + )); + } + for (name, _) in &p.ifaces { + let c = c_ty(Ty::Iface(Box::leak(name.clone().into_boxed_str()))); + g.out.push_str(&format!( + "static {c} {c}_copy({c} v);\nstatic void {c}_drop({c} *v);\nstatic dstr {c}_str({c} v);\n" + )); + } + for (i, t) in &iface_pairs { + g.out.push_str(&format!("static {} {}_from_{}({} v);\n", c_ty(*i), c_ty(*i), c_ty(*t), c_ty(*t))); + } + for t in &p.types { + let c = c_struct(t.name()); + g.out.push_str(&format!("static dstr {c}_str({c} v);\n")); + if g.info.needs_copy_named(t.name()) { + g.out.push_str(&format!("static {c} {c}_copy({c} v);\n")); + g.out.push_str(&format!("static void {c}_drop({c} *v);\n")); + } + } + for f in &p.funcs { + let sig = g.signature(f); + g.out.push_str(&format!("{sig};\n")); + } + g.out.push_str("/*CLOSURE_PROTOS*/\n"); + g.out.push('\n'); + + // helper bodies + for l in &lists { + let body = g.list_bodies(*l); + g.out.push_str(&body); + } + for m in &maps { + let body = g.map_bodies(*m); + g.out.push_str(&body); + } + for t in &p.types { + match t { + HTypeDef::Struct(s) => g.struct_helpers(s), + HTypeDef::Enum(e) => g.enum_helpers(e), + } + } + + for s in &shareds { + let body = g.shared_bodies(*s); + g.out.push_str(&body); + } + for (name, _) in &p.ifaces { + let c = c_ty(Ty::Iface(Box::leak(name.clone().into_boxed_str()))); + g.out.push_str(&format!( + "static {c} {c}_copy({c} v) {{\n if (v.data) v.data = v.vt->copy(v.data);\n return v;\n}}\n\n\ + static void {c}_drop({c} *v) {{\n if (v->data) v->vt->drop(v->data);\n v->data = NULL;\n}}\n\n\ + static dstr {c}_str({c} v) {{\n return v.data ? v.vt->str(v.data) : {};\n}}\n\n", + str_lit("nil") + )); + } + for (i, t) in &iface_pairs { + let body = g.vtable(*i, *t, &p.ifaces); + g.out.push_str(&body); + } + for f in &fn_types { + let c = c_ty(*f); + g.out.push_str(&format!( + "static {c} {c}_copy({c} v) {{\n if (v.env) v.env = v.copy(v.env);\n return v;\n}}\n\n\ + static void {c}_drop({c} *v) {{\n if (v->env) v->drop(v->env);\n v->env = NULL;\n}}\n\n\ + static dstr {c}_str({c} v) {{\n return {};\n}}\n\n", + str_lit("func") + )); + } + for w in &wrappers { + let body = g.wrapper_bodies(*w); + g.out.push_str(&body); + } + + for f in &p.funcs { + g.func(f); + } + let protos = std::mem::take(&mut g.closure_protos); + g.out = g.out.replacen("/*CLOSURE_PROTOS*/", &protos, 1); + let defs = std::mem::take(&mut g.closure_defs); + g.out.push_str(&defs); + if main_fails { + g.out.push_str( + "int main(int argc, char **argv) {\n dino_set_args(argc, argv);\n dino_err e = d_main();\n if (e.set) {\n fflush(stdout);\n fprintf(stderr, \"error: %.*s\\n\", (int)e.msg.n, e.msg.p);\n dino_err_release(e);\n return 1;\n }\n return 0;\n}\n", + ); + } else { + g.out.push_str("int main(int argc, char **argv) {\n dino_set_args(argc, argv);\n d_main();\n return 0;\n}\n"); + } + g.out +} + +/// Every list type used anywhere, inner element types first. +fn collect_lists(p: &HProgram) -> Vec { + fn add(t: Ty, out: &mut Vec, seen: &mut HashSet) { + match t { + Ty::List(e) => { + add(*e, out, seen); + if seen.insert(c_ty(t)) { + out.push(t); + } + } + Ty::Map(k, v) => { + add(*k, out, seen); + add(*v, out, seen); + if seen.insert(c_ty(t)) { + out.push(t); + } + } + Ty::Opt(inner) => add(*inner, out, seen), + Ty::Tuple(items) => items.iter().for_each(|i| add(*i, out, seen)), + Ty::Func(ft) => { + ft.params.iter().for_each(|p| add(*p, out, seen)); + add(ft.ret, out, seen); + if seen.insert(c_ty(t)) { + out.push(t); + } + } + Ty::Shared(_) | Ty::Weak(_) => { + if let Ty::Weak(i) = t { + add(Ty::shared(*i), out, seen); + } + if seen.insert(c_ty(t)) { + out.push(t); + } + } + _ => {} + } + } + let mut out = Vec::new(); + let mut seen = HashSet::new(); + for t in &p.types { + match t { + HTypeDef::Struct(s) => s.fields.iter().for_each(|(_, t)| add(*t, &mut out, &mut seen)), + HTypeDef::Enum(e) => e + .variants + .iter() + .for_each(|v| v.fields.iter().for_each(|(_, t)| add(*t, &mut out, &mut seen))), + } + } + for f in &p.funcs { + add(f.ret, &mut out, &mut seen); + for l in f.params.iter().chain(f.self_param.iter()) { + add(l.ty, &mut out, &mut seen); + } + let mut tys: Vec = Vec::new(); + walk_stmts( + &f.body, + &mut |s| match s { + HStmt::Let { local, .. } => tys.push(local.ty), + HStmt::LetMany { locals, .. } => tys.extend(locals.iter().map(|l| l.ty)), + HStmt::ForRange { var, .. } => tys.push(var.ty), + HStmt::ForEach { index, var, .. } => { + tys.push(var.ty); + if let Some(i) = index { + tys.push(i.ty); + } + } + _ => {} + }, + &mut |_| {}, + ); + walk_stmts(&f.body, &mut |_| {}, &mut |e| tys.push(e.ty)); + for t in tys { + add(t, &mut out, &mut seen); + } + } + out +} + +/// Every (interface, concrete type) pair where a value is turned into an interface value. +fn collect_iface_pairs(p: &HProgram) -> Vec<(Ty, Ty)> { + let mut out: Vec<(Ty, Ty)> = Vec::new(); + for f in &p.funcs { + walk_stmts(&f.body, &mut |_| {}, &mut |e| { + if let HExprKind::ToIface(inner) = &e.kind { + if !out.iter().any(|(i, t)| c_ty(*i) == c_ty(e.ty) && c_ty(*t) == c_ty(inner.ty)) { + out.push((e.ty, inner.ty)); + } + } + }); + } + out +} + +/// Every optional and multi-result type used anywhere, inner types first. +fn collect_wrappers(p: &HProgram) -> Vec { + fn add(t: Ty, out: &mut Vec, seen: &mut HashSet) { + match t { + Ty::Opt(inner) => add(*inner, out, seen), + Ty::Tuple(items) => items.iter().for_each(|i| add(*i, out, seen)), + _ => return, + } + if seen.insert(c_ty(t)) { + out.push(t); + } + } + let mut out = Vec::new(); + let mut seen = HashSet::new(); + for f in &p.funcs { + add(f.ret, &mut out, &mut seen); + for l in f.params.iter().chain(f.self_param.iter()) { + add(l.ty, &mut out, &mut seen); + } + let mut tys: Vec = Vec::new(); + walk_stmts( + &f.body, + &mut |s| match s { + HStmt::Let { local, .. } => tys.push(local.ty), + HStmt::LetMany { locals, .. } => tys.extend(locals.iter().map(|l| l.ty)), + _ => {} + }, + &mut |_| {}, + ); + walk_stmts(&f.body, &mut |_| {}, &mut |e| tys.push(e.ty)); + for t in tys { + add(t, &mut out, &mut seen); + } + } + out +} + +// ---------- naming ---------- + +/// The C struct that holds a shared value with its two counters. +fn box_name(inner: Ty) -> String { + format!("BX_{}", tag(inner)) +} + +fn c_struct(name: &str) -> String { + format!("S_{}", mangle(name)) +} + +/// Short name of a type, used inside names of generated list types. +fn tag(t: Ty) -> String { + match t { + Ty::Int => "i64".into(), + Ty::Float => "f64".into(), + Ty::Bool => "bool".into(), + Ty::Char => "char".into(), + Ty::Str => "str".into(), + Ty::Struct(name) | Ty::Enum(name) => c_struct(name), + Ty::List(e) => format!("L_{}", tag(*e)), + Ty::Map(k, v) => format!("M_{}_{}", tag(*k), tag(*v)), + Ty::Num(n) => n.name().into(), + Ty::Error => "err".into(), + Ty::Opt(inner) => format!("O_{}", tag(*inner)), + Ty::Shared(inner) => format!("SH_{}", tag(*inner)), + Ty::Weak(inner) => format!("WK_{}", tag(*inner)), + Ty::Iface(name) => format!("IF_{}", mangle(name)), + Ty::Func(ft) => format!( + "CL_{}_R_{}", + ft.params.iter().map(|p| tag(*p)).collect::>().join("_"), + tag(ft.ret) + ), + Ty::Tuple(items) => format!("T_{}", items.iter().map(|t| tag(*t)).collect::>().join("_")), + Ty::Unit => "void".into(), + } +} + +fn c_ty(t: Ty) -> String { + match t { + Ty::Int => "int64_t".into(), + Ty::Float => "double".into(), + Ty::Bool => "bool".into(), + Ty::Char => "uint32_t".into(), + Ty::Str => "dstr".into(), + Ty::Struct(name) | Ty::Enum(name) => c_struct(name), + Ty::List(_) | Ty::Map(..) | Ty::Opt(_) | Ty::Tuple(_) | Ty::Shared(_) | Ty::Weak(_) | Ty::Func(_) | Ty::Iface(_) => tag(t), + Ty::Num(n) => num_c(n).into(), + Ty::Error => "dino_err".into(), + Ty::Unit => "void".into(), + } +} + +fn num_c(n: Num) -> &'static str { + match n { + Num::I8 => "int8_t", + Num::I16 => "int16_t", + Num::I32 => "int32_t", + Num::U8 => "uint8_t", + Num::U16 => "uint16_t", + Num::U32 => "uint32_t", + Num::U64 => "uint64_t", + Num::F32 => "float", + } +} + +/// Make a Dino name safe to use as part of a C identifier. +fn mangle(name: &str) -> String { + name.chars() + .map(|c| if c.is_ascii_alphanumeric() || c == '_' { c.to_string() } else { format!("u{:x}_", c as u32) }) + .collect() +} + +fn local_name(id: u32, name: &str) -> String { + format!("{}_{}", mangle(name), id) +} + +/// The union member holding the data of an enum case. +fn variant_member(case: &str) -> String { + format!("v_{}", mangle(case)) +} + +/// Fields get a prefix so a field called `int` or `default` can't clash with C keywords. +fn field_name(name: &str) -> String { + format!("f_{}", mangle(name)) +} + +fn func_cname(owner: Option<&str>, name: &str) -> String { + match owner { + Some(o) => format!("d_{}__{}", mangle(o), mangle(name)), + None => format!("d_{}", mangle(name)), + } +} + +/// A C string literal for `dstr`, as `dino_lit("...", len)`. +fn str_lit(s: &str) -> String { + let mut out = String::from("dino_lit(\""); + for b in s.bytes() { + match b { + b'"' => out.push_str("\\\""), + b'\\' => out.push_str("\\\\"), + b'\n' => out.push_str("\\n"), + b'\t' => out.push_str("\\t"), + b'\r' => out.push_str("\\r"), + 0x20..=0x7e if b != b'?' => out.push(b as char), + _ => out.push_str(&format!("\\{:03o}", b)), // octal: never swallows following digits + } + } + out.push_str(&format!("\", {})", s.len())); + out +} + +fn sym(op: BinOp) -> &'static str { + match op { + BinOp::Or => "||", + BinOp::And => "&&", + BinOp::Eq => "==", + BinOp::Ne => "!=", + BinOp::Lt => "<", + BinOp::Gt => ">", + BinOp::Le => "<=", + BinOp::Ge => ">=", + BinOp::Add => "+", + BinOp::Sub => "-", + BinOp::Mul => "*", + BinOp::Div => "/", + BinOp::Rem => "%", + } +} + +fn is_literal(e: &HExpr) -> bool { + matches!(e.kind, HExprKind::Int(_) | HExprKind::Float(_) | HExprKind::Bool(_) | HExprKind::Str(_)) +} + +/// A variable or a chain of fields/indexes of one: something with an address. +fn is_place(e: &HExpr) -> bool { + place_root(e).is_some() +} + +/// A C expression turning a value of type `ty` (given as C code) into a `dstr`. +fn to_str(ty: Ty, c: &str) -> String { + match ty { + Ty::Int => format!("dino_i64_str({c})"), + Ty::Float => format!("dino_f64_str({c})"), + Ty::Bool => format!("dino_bool_str({c})"), + Ty::Char => format!("dino_char_str({c})"), + Ty::Str => format!("dino_str_retain({c})"), + Ty::Struct(_) | Ty::Enum(_) | Ty::List(_) | Ty::Map(..) | Ty::Opt(_) | Ty::Shared(_) | Ty::Weak(_) | Ty::Func(_) | Ty::Iface(_) => format!("{}_str({c})", c_ty(ty)), + Ty::Num(Num::U64) => format!("dino_u64_str({c})"), + Ty::Num(Num::F32) => format!("dino_f32_str({c})"), + Ty::Num(_) => format!("dino_i64_str((int64_t)({c}))"), + Ty::Error => format!("dino_err_str({c})"), + Ty::Tuple(_) => unreachable!("several values can't be turned into text"), + Ty::Unit => unreachable!("nothing can't be turned into text"), + } +} + +struct Gen { + out: String, + indent: usize, + tmp: u32, + /// Locals of the current function that are pointers in C (`self`, borrowed parameters). + ref_ids: HashSet, + info: Info, + enums: HashMap, + /// Return type of the function being generated (for `?`). + cur_ret: Ty, + /// Heap values owned by the variables of each open block, innermost last. + /// They are freed when the block ends (or early, on `return`/`break`/`continue`). + scopes: Vec>, + /// Fresh heap values (call results, copies, ...) not yet given to an owner. + /// Whoever takes one removes it; the rest are freed at the end of the statement. + pending: Vec, + /// Open loops: how many blocks and pending values existed outside the loop body. + loops: Vec, + /// Closures made so far (each becomes a C function `CF_n` and, if it captures, a struct `CE_n`). + closures: u32, + closure_protos: String, + closure_defs: String, +} + +#[derive(Clone)] +struct Owned { + name: String, + ty: Ty, +} + +struct LoopMark { + scopes: usize, + pending: usize, +} + +impl Gen { + fn line(&mut self, s: &str) { + for _ in 0..self.indent { + self.out.push_str(" "); + } + self.out.push_str(s); + self.out.push('\n'); + } + + fn fresh(&mut self) -> String { + self.tmp += 1; + format!("t{}", self.tmp) + } + + /// Run `f`, returning the statements it emitted separately from the output so far. + fn capture(&mut self, f: impl FnOnce(&mut Self) -> R) -> (String, R) { + let saved = std::mem::take(&mut self.out); + let r = f(self); + let emitted = std::mem::replace(&mut self.out, saved); + (emitted, r) + } + + // ---------- ownership ---------- + + fn heap(&self, ty: Ty) -> bool { + self.info.needs_copy(ty) + } + + /// A fresh heap value in the C variable `name`: free it at the end of the statement + /// unless someone takes it. + fn register_temp(&mut self, name: &str, ty: Ty) { + if self.heap(ty) { + self.pending.push(Owned { name: name.to_string(), ty }); + } + } + + fn is_pending(&self, name: &str) -> bool { + self.pending.iter().any(|o| o.name == name) + } + + /// A new owner takes the fresh value `name` (nothing to do for places and literals). + fn take(&mut self, name: &str) { + if let Some(i) = self.pending.iter().rposition(|o| o.name == name) { + self.pending.remove(i); + } + } + + /// Variables own the heap values they hold until the end of their block. + fn own_local(&mut self, name: String, ty: Ty) { + if self.heap(ty) { + self.scopes.last_mut().expect("inside a function").push(Owned { name, ty }); + } + } + + /// The C statement that frees the value in the lvalue `lv`. + fn drop_line(&self, ty: Ty, lv: &str) -> String { + match ty { + Ty::Str => format!("dino_str_release({lv});"), + Ty::Error => format!("dino_err_release({lv});"), + _ => format!("{}_drop(&({lv}));", c_ty(ty)), + } + } + + fn emit_drops(&mut self, items: Vec) { + for o in items { + let l = self.drop_line(o.ty, &o.name); + self.line(&l); + } + } + + /// Free the fresh values created since `mark` and forget them. + fn flush_pending(&mut self, mark: usize) { + let items: Vec = self.pending.drain(mark..).rev().collect(); + self.emit_drops(items); + } + + /// Free the variables of the innermost block and close it. + fn end_scope(&mut self) { + let sc = self.scopes.pop().expect("a scope is open"); + self.emit_drops(sc.into_iter().rev().collect()); + } + + /// Leaving the function early: free every fresh value and every variable + /// (except `skip`, which is being returned). Nothing is forgotten, the code + /// after still needs them. + fn drop_everything(&mut self, skip: &[String]) { + let mut items: Vec = self.pending.iter().rev().cloned().collect(); + for sc in self.scopes.iter().rev() { + items.extend(sc.iter().rev().filter(|o| !skip.contains(&o.name)).cloned()); + } + self.emit_drops(items); + } + + /// `break` / `continue`: free what was created inside the loop body. + fn drop_loop_exit(&mut self) { + let m = self.loops.last().expect("inside a loop"); + let (sc, pd) = (m.scopes, m.pending); + let mut items: Vec = self.pending[pd..].iter().rev().cloned().collect(); + for scope in self.scopes[sc..].iter().rev() { + items.extend(scope.iter().rev().cloned()); + } + self.emit_drops(items); + } + + /// C code that deep-copies the value `e` of type `ty` (or just reads it, if that is enough). + fn copy_of(&self, ty: Ty, e: &str) -> String { + if !self.info.needs_copy(ty) { + e.to_string() + } else if ty == Ty::Str { + format!("dino_str_retain({e})") + } else if ty == Ty::Error { + format!("dino_err_retain({e})") + } else { + format!("{}_copy({e})", c_ty(ty)) + } + } + + // ---------- types ---------- + + fn struct_def(&mut self, s: &HStruct) { + self.out.push_str(&format!("struct {} {{\n", c_struct(&s.name))); + if s.fields.is_empty() { + self.out.push_str(" char _empty;\n"); + } + for (name, ty) in &s.fields { + self.out.push_str(&format!(" {} {};\n", c_ty(*ty), field_name(name))); + } + self.out.push_str("};\n\n"); + } + + /// `S_Vec2_str` (how a struct looks when printed) and `S_Vec2_copy` (if it holds lists). + fn struct_helpers(&mut self, s: &HStruct) { + let c = c_struct(&s.name); + let mut parts: Vec = Vec::new(); + for (i, (name, ty)) in s.fields.iter().enumerate() { + let head = format!("{}{}: ", if i == 0 { format!("{}{{", s.name) } else { ", ".to_string() }, name); + parts.push(str_lit(&head)); + parts.push(to_str(*ty, &format!("v.{}", field_name(name)))); + } + parts.push(str_lit(&if s.fields.is_empty() { format!("{}{{}}", s.name) } else { "}".to_string() })); + self.out.push_str(&format!("static dstr {c}_str({c} v) {{\n")); + self.out.push_str(&format!(" dstr parts[] = {{{}}};\n", parts.join(", "))); + self.out.push_str(&format!( + " dstr r = dino_cat_n({0}, parts);\n for (int i = 0; i < {0}; i++) dino_str_release(parts[i]);\n return r;\n}}\n\n", + parts.len() + )); + + if self.info.needs_copy_named(&s.name) { + self.out.push_str(&format!("static void {c}_drop({c} *v) {{\n")); + for (name, fty) in &s.fields { + if self.info.needs_copy(*fty) { + let l = self.drop_line(*fty, &format!("v->{}", field_name(name))); + self.out.push_str(&format!(" {l}\n")); + } + } + self.out.push_str("}\n\n"); + } + + if self.info.needs_copy_named(&s.name) { + self.out.push_str(&format!("static {c} {c}_copy({c} v) {{\n {c} r = v;\n")); + for (name, fty) in &s.fields { + if self.info.needs_copy(*fty) { + let f = field_name(name); + let copy = self.copy_of(*fty, &format!("v.{f}")); + self.out.push_str(&format!(" r.{f} = {copy};\n")); + } + } + self.out.push_str(" return r;\n}\n\n"); + } + } + + /// A tag plus a union with the data of each case. + fn enum_def(&mut self, e: &HEnum) { + self.out.push_str(&format!("struct {} {{\n int32_t tag;\n", c_struct(&e.name))); + if e.variants.iter().any(|v| !v.fields.is_empty()) { + self.out.push_str(" union {\n"); + for v in e.variants.iter().filter(|v| !v.fields.is_empty()) { + let fields: Vec = + v.fields.iter().map(|(n, t)| format!("{} {};", c_ty(*t), field_name(n))).collect(); + self.out.push_str(&format!(" struct {{ {} }} {};\n", fields.join(" "), variant_member(&v.name))); + } + self.out.push_str(" } u;\n"); + } + self.out.push_str("};\n\n"); + } + + /// `S_Shape_str` (printing) and `S_Shape_copy` (if a case holds a list). + fn enum_helpers(&mut self, e: &HEnum) { + let c = c_struct(&e.name); + self.out.push_str(&format!("static dstr {c}_str({c} v) {{\n switch (v.tag) {{\n")); + for (i, v) in e.variants.iter().enumerate() { + if v.fields.is_empty() { + self.out.push_str(&format!(" case {i}: return {};\n", str_lit(&format!("{}.{}", e.name, v.name)))); + } else { + let mut parts = vec![str_lit(&format!("{}.{}(", e.name, v.name))]; + for (k, (n, t)) in v.fields.iter().enumerate() { + if k > 0 { + parts.push(str_lit(", ")); + } + parts.push(to_str(*t, &format!("v.u.{}.{}", variant_member(&v.name), field_name(n)))); + } + parts.push(str_lit(")")); + self.out.push_str(&format!( + " case {i}: {{ dstr parts[] = {{{}}}; dstr r = dino_cat_n({n}, parts); for (int k = 0; k < {n}; k++) dino_str_release(parts[k]); return r; }}\n", + parts.join(", "), + n = parts.len() + )); + } + } + self.out.push_str(" }\n return dino_lit(\"?\", 1);\n}\n\n"); + + if self.info.needs_copy_named(&e.name) { + self.out.push_str(&format!("static {c} {c}_copy({c} v) {{\n {c} r = v;\n switch (v.tag) {{\n")); + for (i, v) in e.variants.iter().enumerate() { + let heavy: Vec<&(String, Ty)> = v.fields.iter().filter(|(_, t)| self.info.needs_copy(*t)).collect(); + if heavy.is_empty() { + continue; + } + self.out.push_str(&format!(" case {i}:\n")); + for (n, t) in heavy { + let place = format!("{}.{}", variant_member(&v.name), field_name(n)); + let copy = self.copy_of(*t, &format!("v.u.{place}")); + self.out.push_str(&format!(" r.u.{place} = {copy};\n")); + } + self.out.push_str(" break;\n"); + } + self.out.push_str(" }\n return r;\n}\n\n"); + + self.out.push_str(&format!("static void {c}_drop({c} *v) {{\n switch (v->tag) {{\n")); + for (i, v) in e.variants.iter().enumerate() { + let heavy: Vec<&(String, Ty)> = v.fields.iter().filter(|(_, t)| self.info.needs_copy(*t)).collect(); + if heavy.is_empty() { + continue; + } + self.out.push_str(&format!(" case {i}:\n")); + for (n, t) in heavy { + let l = self.drop_line(*t, &format!("v->u.{}.{}", variant_member(&v.name), field_name(n))); + self.out.push_str(&format!(" {l}\n")); + } + self.out.push_str(" break;\n"); + } + self.out.push_str(" }\n}\n\n"); + } + } + + /// `O_int` = `{ has, v }`; a tuple = `{ f0, f1, ... }`. + fn wrapper_def(&mut self, w: Ty) { + let c = c_ty(w); + match w { + Ty::Opt(inner) => { + self.out.push_str(&format!("typedef struct {{ bool has; {} v; }} {c};\n", c_ty(*inner))); + } + Ty::Tuple(items) => { + let fields: Vec = + items.iter().enumerate().map(|(i, t)| format!("{} f{i};", c_ty(*t))).collect(); + self.out.push_str(&format!("typedef struct {{ {} }} {c};\n", fields.join(" "))); + } + _ => unreachable!(), + } + } + + fn wrapper_protos(&self, w: Ty) -> String { + let c = c_ty(w); + let mut out = String::new(); + if let Ty::Opt(_) = w { + out.push_str(&format!("static dstr {c}_str({c} v);\n")); + } + if self.info.needs_copy(w) { + if let Ty::Opt(_) = w { + out.push_str(&format!("static {c} {c}_copy({c} v);\n")); + } + out.push_str(&format!("static void {c}_drop({c} *v);\n")); + } + out + } + + fn wrapper_bodies(&self, w: Ty) -> String { + let c = c_ty(w); + let mut out = String::new(); + match w { + Ty::Opt(inner) => { + out.push_str(&format!( + "static dstr {c}_str({c} v) {{\n if (!v.has) return dino_lit(\"nil\", 3);\n return {};\n}}\n\n", + to_str(*inner, "v.v") + )); + if self.info.needs_copy(*inner) { + out.push_str(&format!( + "static {c} {c}_copy({c} v) {{\n {c} r = v;\n if (v.has) r.v = {};\n return r;\n}}\n\n", + self.copy_of(*inner, "v.v") + )); + out.push_str(&format!( + "static void {c}_drop({c} *v) {{\n if (v->has) {}\n}}\n\n", + self.drop_line(*inner, "v->v") + )); + } + } + Ty::Tuple(items) if self.info.needs_copy(w) => { + out.push_str(&format!("static void {c}_drop({c} *v) {{\n")); + for (i, t) in items.iter().enumerate() { + if self.info.needs_copy(*t) { + out.push_str(&format!(" {}\n", self.drop_line(*t, &format!("v->f{i}")))); + } + } + out.push_str("}\n\n"); + } + _ => {} + } + out + } + + /// The table of methods that lets a `t` be used as the interface `i`, and the function that makes the value. + fn vtable(&self, i: Ty, t: Ty, ifaces: &[(String, Vec<(String, Vec, Ty)>)]) -> String { + let (Ty::Iface(iname), Ty::Struct(owner) | Ty::Enum(owner)) = (i, t) else { unreachable!() }; + let (ic, tc) = (c_ty(i), c_ty(t)); + let base = format!("VTI_{}__{}", mangle(iname), mangle(owner)); + let methods = &ifaces.iter().find(|(n, _)| n == iname).unwrap().1; + let drop_t = if self.info.needs_copy(t) { format!(" {}\n", self.drop_line(t, &format!("*({tc} *)p"))) } else { String::new() }; + let mut out = format!( + "static void *{base}_copy(void *p) {{\n {tc} *d = ({tc} *)malloc(sizeof({tc}));\n if (!d) dino_panic(\"out of memory\");\n *d = {};\n return d;\n}}\n\ + static void {base}_drop(void *p) {{\n{drop_t} free(p);\n}}\n\ + static dstr {base}_str(void *p) {{\n return {tc}_str(*({tc} *)p);\n}}\n", + self.copy_of(t, &format!("*({tc} *)p")) + ); + for (m, params, ret) in methods { + let cname = func_cname(Some(owner), m); + let flags = self.info.borrowed.get(&cname).cloned().unwrap_or_default(); + let mut ps = vec!["void *p".to_string()]; + ps.extend(params.iter().enumerate().map(|(k, t)| format!("{} a{k}", c_ty(*t)))); + let args: Vec = std::iter::once(format!("({tc} *)p")) + .chain((0..params.len()).map(|k| if flags.get(k) == Some(&true) { format!("&a{k}") } else { format!("a{k}") })) + .collect(); + let drops: String = params + .iter() + .enumerate() + .filter(|(k, t)| flags.get(*k) == Some(&true) && self.info.needs_copy(**t)) + .map(|(k, t)| format!(" {}\n", self.drop_line(*t, &format!("a{k}")))) + .collect(); + let call = format!("{cname}({})", args.join(", ")); + if *ret == Ty::Unit { + out.push_str(&format!( + "static void {base}_m_{}({}) {{\n {call};\n{drops}}}\n", + mangle(m), + ps.join(", ") + )); + } else { + out.push_str(&format!( + "static {rc} {base}_m_{}({}) {{\n {rc} r = {call};\n{drops} return r;\n}}\n", + mangle(m), + ps.join(", "), + rc = c_ty(*ret) + )); + } + } + let entries: Vec = ["copy", "drop", "str"] + .iter() + .map(|n| format!("{base}_{n}")) + .chain(methods.iter().map(|(m, _, _)| format!("{base}_m_{}", mangle(m)))) + .collect(); + out.push_str(&format!( + "static const struct VT_{ic} {base}_vt = {{ {} }};\n\ + static {ic} {ic}_from_{tc}({tc} v) {{\n {tc} *b = ({tc} *)malloc(sizeof({tc}));\n if (!b) dino_panic(\"out of memory\");\n *b = v;\n return ({ic}){{ b, &{base}_vt }};\n}}\n\n", + entries.join(", ") + )); + out + } + + /// A function value: the body becomes a C function (written to `closure_defs`), and the + /// captured values go into a heap struct that the value points at. + fn closure(&mut self, ty: Ty, params: &[HLocal], ret: Ty, body: &[HStmt], captures: &[(HLocal, HExpr)]) -> String { + let n = self.closures; + self.closures += 1; + let (env, fname, clty) = (format!("CE_{n}"), format!("CF_{n}"), c_ty(ty)); + + // the captured values are fresh copies, evaluated now + let refs: Vec<&HExpr> = captures.iter().map(|(_, h)| h).collect(); + let vs = self.seq(&refs); + self.take_all(&refs, &vs); + + // declarations + let mut proto_params = vec!["void *".to_string()]; + proto_params.extend(params.iter().map(|p| c_ty(p.ty))); + self.closure_protos.push_str(&format!("static {} {fname}({});\n", c_ty(ret), proto_params.join(", "))); + if !captures.is_empty() { + let fields: Vec = + captures.iter().enumerate().map(|(i, (l, _))| format!("{} f{i};", c_ty(l.ty))).collect(); + let copies: Vec = captures + .iter() + .enumerate() + .map(|(i, (l, _))| format!(" d->f{i} = {};", self.copy_of(l.ty, &format!("s->f{i}")))) + .collect(); + let drops: Vec = captures + .iter() + .enumerate() + .filter(|(_, (l, _))| self.info.needs_copy(l.ty)) + .map(|(i, (l, _))| format!(" {}", self.drop_line(l.ty, &format!("s->f{i}")))) + .collect(); + self.closure_protos.push_str(&format!( + "typedef struct {env} {{ {} }} {env};\n\ + static void *{env}_copy(void *p) {{\n {env} *s = ({env} *)p;\n {env} *d = ({env} *)malloc(sizeof({env}));\n if (!d) dino_panic(\"out of memory\");\n{}\n return d;\n}}\n\ + static void {env}_drop(void *p) {{\n {env} *s = ({env} *)p;\n{}\n free(s);\n}}\n", + fields.join(" "), + copies.join("\n"), + drops.join("\n") + )); + } + + // the function itself, generated with a clean slate + let saved_out = std::mem::take(&mut self.out); + let saved_indent = self.indent; + let saved_scopes = std::mem::take(&mut self.scopes); + let saved_pending = std::mem::take(&mut self.pending); + let saved_loops = std::mem::take(&mut self.loops); + let saved_refs = std::mem::take(&mut self.ref_ids); + let saved_ret = self.cur_ret; + let mut ps = vec!["void *env_".to_string()]; + ps.extend(params.iter().map(|p| format!("{} {}", c_ty(p.ty), local_name(p.id, &p.name)))); + self.out.push_str(&format!("static {} {fname}({}) {{\n", c_ty(ret), ps.join(", "))); + self.indent = 1; + self.cur_ret = ret; + self.scopes = vec![Vec::new()]; + if !captures.is_empty() { + self.line(&format!("{env} *env = ({env} *)env_;")); + for (i, (l, _)) in captures.iter().enumerate() { + self.line(&format!("{} *{} = &env->f{i};", c_ty(l.ty), local_name(l.id, &l.name))); + self.ref_ids.insert(l.id); + } + } else { + self.line("(void)env_;"); + } + for p in params { + self.own_local(local_name(p.id, &p.name), p.ty); + } + self.stmts(body); + self.end_scope(); + if ret != Ty::Unit { + self.line("dino_panic(\"reached the end of a function without returning\");"); + } + self.out.push_str("}\n\n"); + let text = std::mem::replace(&mut self.out, saved_out); + self.closure_defs.push_str(&text); + self.indent = saved_indent; + self.scopes = saved_scopes; + self.pending = saved_pending; + self.loops = saved_loops; + self.ref_ids = saved_refs; + self.cur_ret = saved_ret; + + // the value + let (env_ptr, cp, dp) = if captures.is_empty() { + ("NULL".to_string(), "NULL".to_string(), "NULL".to_string()) + } else { + let t = self.fresh(); + self.line(&format!("{env} *{t} = ({env} *)malloc(sizeof({env}));")); + self.line(&format!("if (!{t}) dino_panic(\"out of memory\");")); + for (i, v) in vs.iter().enumerate() { + self.line(&format!("{t}->f{i} = {v};")); + } + (t, format!("{env}_copy"), format!("{env}_drop")) + }; + self.materialize(ty, format!("(({clty}){{ .fn = {fname}, .env = (void *){env_ptr}, .copy = {cp}, .drop = {dp} }})")) + } + + fn shared_protos(&self, s: Ty) -> String { + let c = c_ty(s); + match s { + Ty::Shared(i) => { + let t = c_ty(*i); + format!( + "static {c} {c}_new({t} v);\nstatic {c} {c}_copy({c} h);\nstatic void {c}_drop({c} *h);\nstatic {t} *{c}_ref({c} h);\nstatic dstr {c}_str({c} h);\n" + ) + } + Ty::Weak(i) => { + let (t, sh) = (c_ty(*i), c_ty(Ty::shared(*i))); + format!( + "static {sh} {c}_up({c} w);\nstatic {c} {c}_down({sh} h);\nstatic {c} {c}_copy({c} w);\nstatic void {c}_drop({c} *w);\nstatic bool {c}_alive({c} w);\nstatic {t} *{c}_ref({c} w);\nstatic dstr {c}_str({c} w);\n" + ) + } + _ => unreachable!(), + } + } + + fn shared_bodies(&self, s: Ty) -> String { + let c = c_ty(s); + match s { + Ty::Shared(i) => { + let (t, bx) = (c_ty(*i), box_name(*i)); + let drop_inner = if self.info.needs_copy(*i) { format!("{t}_drop(&b->v); ") } else { String::new() }; + let inner_str = to_str(*i, "h.p->v"); + format!( + r#"static {c} {c}_new({t} v) {{ + {bx} *b = ({bx} *)malloc(sizeof({bx})); + if (!b) dino_panic("out of memory"); + b->rc = 1; + b->wk = 0; + b->v = v; + return ({c}){{b}}; +}} + +static {c} {c}_copy({c} h) {{ + if (h.p) h.p->rc++; + return h; +}} + +static void {c}_drop({c} *h) {{ + {bx} *b = h->p; + if (!b) return; + h->p = NULL; + if (--b->rc == 0) {{ + b->wk++; /* weak pointers inside the value must not free the box under us */ + {drop_inner}b->wk--; + if (b->wk == 0) free(b); + }} +}} + +static {t} *{c}_ref({c} h) {{ + if (!h.p) dino_panic("this `shared` value is empty"); + return &h.p->v; +}} + +static dstr {c}_str({c} h) {{ + if (!h.p) return {nil}; + return {inner_str}; +}} + +"#, + nil = str_lit("nil") + ) + } + Ty::Weak(i) => { + let (t, bx, sh) = (c_ty(*i), box_name(*i), c_ty(Ty::shared(*i))); + format!( + r#"static {sh} {c}_up({c} w) {{ + if (w.p && w.p->rc > 0) {{ + w.p->rc++; + return ({sh}){{w.p}}; + }} + return ({sh}){{0}}; +}} + +static {c} {c}_down({sh} h) {{ + if (h.p) h.p->wk++; + return ({c}){{h.p}}; +}} + +static {c} {c}_copy({c} w) {{ + if (w.p) w.p->wk++; + return w; +}} + +static void {c}_drop({c} *w) {{ + {bx} *b = w->p; + if (!b) return; + w->p = NULL; + if (--b->wk == 0 && b->rc == 0) free(b); +}} + +static bool {c}_alive({c} w) {{ + return w.p && w.p->rc > 0; +}} + +static {t} *{c}_ref({c} w) {{ + if (!{c}_alive(w)) dino_panic("this `weak` value is gone (or was never set)"); + return &w.p->v; +}} + +static dstr {c}_str({c} w) {{ + return {c}_alive(w) ? {weak} : {nil}; +}} + +"#, + weak = str_lit("weak"), + nil = str_lit("nil") + ) + } + _ => unreachable!(), + } + } + + fn map_protos(&self, m: Ty) -> String { + let Ty::Map(k, v) = m else { unreachable!() }; + let (c, kt, vt) = (c_ty(m), c_ty(*k), c_ty(*v)); + format!( + "static int64_t {c}_find(const {c} *m, {kt} key);\n\ + static {vt} *{c}_ptr(const {c} *m, {kt} key);\n\ + static {vt} *{c}_at(const {c} *m, {kt} key);\n\ + static {vt} {c}_zero_get(const {c} *m, {kt} key);\n\ + static void {c}_set({c} *m, {kt} key, {vt} val);\n\ + static void {c}_remove({c} *m, {kt} key);\n\ + static {c} {c}_copy({c} m);\n\ + static void {c}_drop({c} *m);\n\ + static dstr {c}_str({c} m);\n" + ) + } + + fn map_bodies(&self, m: Ty) -> String { + let Ty::Map(k, v) = m else { unreachable!() }; + let (c, kt, vt) = (c_ty(m), c_ty(*k), c_ty(*v)); + let (hash, eq) = if *k == Ty::Str { + ("dino_hash_str(key)".to_string(), "dino_streq(a, b)".to_string()) + } else { + ("dino_hash_i64((int64_t)key)".to_string(), "a == b".to_string()) + }; + let copy_k = self.copy_of(*k, "m.k[i]"); + let copy_v = self.copy_of(*v, "m.v[i]"); + let drop_entry = |lv_k: &str, lv_v: &str| -> String { + let mut s = String::new(); + if self.info.needs_copy(*k) { + s.push_str(&format!("{} ", self.drop_line(*k, lv_k))); + } + if self.info.needs_copy(*v) { + s.push_str(&self.drop_line(*v, lv_v)); + } + s + }; + let drop_i = drop_entry("m->k[i]", "m->v[i]"); + let drop_j = drop_entry("m->k[j]", "m->v[j]"); + let key_str = to_str(*k, "m.k[i]"); + let val_str = to_str(*v, "m.v[i]"); + let copy_key_set = self.copy_of(*k, "key"); + format!( + r#"static inline uint64_t {c}_hash({kt} key) {{ return {hash}; }} + +static inline bool {c}_eq({kt} a, {kt} b) {{ return {eq}; }} + +static void {c}_reindex({c} *m) {{ + int64_t cap = 8; + while (cap < m->len * 2 + 2) cap *= 2; + free(m->idx); + m->idx = (int64_t *)calloc((size_t)cap, sizeof(int64_t)); + if (!m->idx) dino_panic("out of memory"); + m->icap = cap; + for (int64_t i = 0; i < m->len; i++) {{ + uint64_t h = {c}_hash(m->k[i]) & (uint64_t)(cap - 1); + while (m->idx[h]) h = (h + 1) & (uint64_t)(cap - 1); + m->idx[h] = i + 1; + }} +}} + +static int64_t {c}_find(const {c} *m, {kt} key) {{ + if (m->len == 0) return -1; + uint64_t mask = (uint64_t)(m->icap - 1), h = {c}_hash(key) & mask; + while (m->idx[h]) {{ + int64_t i = m->idx[h] - 1; + if ({c}_eq(m->k[i], key)) return i; + h = (h + 1) & mask; + }} + return -1; +}} + +static {vt} *{c}_ptr(const {c} *m, {kt} key) {{ + int64_t i = {c}_find(m, key); + return i < 0 ? NULL : &m->v[i]; +}} + +static {vt} *{c}_at(const {c} *m, {kt} key) {{ + {vt} *p = {c}_ptr(m, key); + if (!p) dino_panic("that key is not in the map (check with `has` or use `get`)"); + return p; +}} + +static {vt} {c}_zero_get(const {c} *m, {kt} key) {{ + {vt} *p = {c}_ptr(m, key); + if (p) return *p; + {vt} z = {{0}}; + return z; +}} + +static void {c}_set({c} *m, {kt} key, {vt} val) {{ + int64_t found = {c}_find(m, key); + if (found >= 0) {{ + int64_t i = found; + {drop_i_found} + m->v[i] = val; + return; + }} + if (m->len == m->cap) {{ + int64_t cap = m->cap ? m->cap * 2 : 4; + {kt} *nk = ({kt} *)realloc(m->k, (size_t)cap * sizeof({kt})); + if (!nk) dino_panic("out of memory"); + m->k = nk; + {vt} *nv = ({vt} *)realloc(m->v, (size_t)cap * sizeof({vt})); + if (!nv) dino_panic("out of memory"); + m->v = nv; + m->cap = cap; + }} + m->k[m->len] = {copy_key_set}; + m->v[m->len] = val; + m->len++; + {c}_reindex(m); +}} + +static void {c}_remove({c} *m, {kt} key) {{ + int64_t i = {c}_find(m, key); + if (i < 0) return; + {drop_i} + for (int64_t j = i; j + 1 < m->len; j++) {{ + m->k[j] = m->k[j + 1]; + m->v[j] = m->v[j + 1]; + }} + m->len--; + {c}_reindex(m); +}} + +static {c} {c}_copy({c} m) {{ + {c} r = {{0}}; + if (m.len > 0) {{ + r.k = ({kt} *)malloc((size_t)m.len * sizeof({kt})); + r.v = ({vt} *)malloc((size_t)m.len * sizeof({vt})); + if (!r.k || !r.v) dino_panic("out of memory"); + r.len = r.cap = m.len; + for (int64_t i = 0; i < m.len; i++) {{ + r.k[i] = {copy_k}; + r.v[i] = {copy_v}; + }} + {c}_reindex(&r); + }} + return r; +}} + +static void {c}_drop({c} *m) {{ + for (int64_t j = 0; j < m->len; j++) {{ {drop_j} }} + free(m->k); + free(m->v); + free(m->idx); + m->k = NULL; + m->v = NULL; + m->idx = NULL; + m->len = m->cap = m->icap = 0; +}} + +static dstr {c}_str({c} m) {{ + dstr *parts = (dstr *)malloc(sizeof(dstr) * (size_t)(m.len * 4 + 2)); + if (!parts) dino_panic("out of memory"); + int k = 0; + parts[k++] = dino_lit("{{", 1); + for (int64_t i = 0; i < m.len; i++) {{ + if (i > 0) parts[k++] = dino_lit(", ", 2); + parts[k++] = {key_str}; + parts[k++] = dino_lit(": ", 2); + parts[k++] = {val_str}; + }} + parts[k++] = dino_lit("}}", 1); + dstr r = dino_cat_n(k, parts); + for (int i = 0; i < k; i++) dino_str_release(parts[i]); + free(parts); + return r; +}} + +"#, + drop_i_found = drop_entry_v(self, *v, "m->v[i]"), + ) + } + + fn list_protos(&self, l: Ty) -> String { + let Ty::List(elem) = l else { unreachable!() }; + let (c, t) = (c_ty(l), c_ty(*elem)); + format!( + "static inline {t} *{c}_at(const {c} *l, int64_t i);\n\ + static void {c}_push({c} *l, {t} v);\n\ + static {t} {c}_pop({c} *l);\n\ + static {c} {c}_copy({c} l);\n\ + static void {c}_drop({c} *l);\n\ + static {c} {c}_slice({c} l, int64_t lo, int64_t hi);\n\ + static dstr {c}_str({c} l);\n" + ) + } + + fn list_bodies(&self, l: Ty) -> String { + let Ty::List(elem) = l else { unreachable!() }; + let (c, t) = (c_ty(l), c_ty(*elem)); + let copy_i = self.copy_of(*elem, "l.p[i]"); + let copy_lo = self.copy_of(*elem, "l.p[lo + i]"); + let elem_str = to_str(*elem, "l.p[i]"); + let drop_elem = if self.info.needs_copy(*elem) { + format!(" for (int64_t i = 0; i < l->len; i++) {}\n", self.drop_line(*elem, "l->p[i]")) + } else { + String::new() + }; + format!( + r#"static inline {t} *{c}_at(const {c} *l, int64_t i) {{ + if (i < 0 || i >= l->len) dino_index_panic(i, l->len); + return &l->p[i]; +}} + +static void {c}_push({c} *l, {t} v) {{ + if (l->len == l->cap) {{ + int64_t cap = l->cap ? l->cap * 2 : 4; + {t} *p = ({t} *)realloc(l->p, (size_t)cap * sizeof({t})); + if (!p) dino_panic("out of memory"); + l->p = p; + l->cap = cap; + }} + l->p[l->len++] = v; +}} + +static {t} {c}_pop({c} *l) {{ + if (l->len == 0) dino_panic("pop from an empty list"); + return l->p[--l->len]; +}} + +static {c} {c}_copy({c} l) {{ + {c} r = {{0}}; + if (l.len > 0) {{ + r.p = ({t} *)malloc((size_t)l.len * sizeof({t})); + if (!r.p) dino_panic("out of memory"); + r.len = r.cap = l.len; + for (int64_t i = 0; i < l.len; i++) r.p[i] = {copy_i}; + }} + return r; +}} + +static void {c}_drop({c} *l) {{ +{drop_elem} free(l->p); + l->p = NULL; + l->len = l->cap = 0; +}} + +static {c} {c}_slice({c} l, int64_t lo, int64_t hi) {{ + if (lo < 0 || hi > l.len || lo > hi) dino_slice_panic(lo, hi, l.len); + {c} r = {{0}}; + if (hi > lo) {{ + int64_t n = hi - lo; + r.p = ({t} *)malloc((size_t)n * sizeof({t})); + if (!r.p) dino_panic("out of memory"); + r.len = r.cap = n; + for (int64_t i = 0; i < n; i++) r.p[i] = {copy_lo}; + }} + return r; +}} + +static dstr {c}_str({c} l) {{ + dstr *parts = (dstr *)malloc(sizeof(dstr) * (size_t)(l.len * 2 + 2)); + if (!parts) dino_panic("out of memory"); + int k = 0; + parts[k++] = dino_lit("[", 1); + for (int64_t i = 0; i < l.len; i++) {{ + if (i > 0) parts[k++] = dino_lit(", ", 2); + parts[k++] = {elem_str}; + }} + parts[k++] = dino_lit("]", 1); + dstr r = dino_cat_n(k, parts); + for (int i = 0; i < k; i++) dino_str_release(parts[i]); + free(parts); + return r; +}} + +"# + ) + } + + // ---------- functions and statements ---------- + + fn signature(&self, f: &HFunc) -> String { + let cname = func_cname(f.owner.as_deref(), &f.name); + let flags = &self.info.borrowed[&cname]; + let mut params: Vec = Vec::new(); + if let (Some(owner), Some(me)) = (&f.owner, &f.self_param) { + let q = if f.mutates { "" } else { "const " }; + params.push(format!("{q}{} *{}", c_struct(owner), local_name(me.id, &me.name))); + } + for (p, borrowed) in f.params.iter().zip(flags) { + if *borrowed { + params.push(format!("const {} *{}", c_ty(p.ty), local_name(p.id, &p.name))); + } else { + params.push(format!("{} {}", c_ty(p.ty), local_name(p.id, &p.name))); + } + } + let params = if params.is_empty() { "void".to_string() } else { params.join(", ") }; + format!("static {} {}({})", c_ty(f.ret), cname, params) + } + + fn func(&mut self, f: &HFunc) { + let sig = self.signature(f); + self.out.push_str(&format!("{sig} {{\n")); + self.indent = 1; + self.ref_ids.clear(); + self.cur_ret = f.ret; + self.scopes = vec![Vec::new()]; + self.pending.clear(); + self.loops.clear(); + if let Some(me) = &f.self_param { + self.ref_ids.insert(me.id); + } + let flags = self.info.borrowed[&func_cname(f.owner.as_deref(), &f.name)].clone(); + for (p, borrowed) in f.params.iter().zip(flags) { + if borrowed { + self.ref_ids.insert(p.id); + } else { + // a parameter passed by value is owned by the function + self.own_local(local_name(p.id, &p.name), p.ty); + } + } + self.stmts(&f.body); + self.end_scope(); + if f.ret != Ty::Unit { + self.line("dino_panic(\"reached the end of a function without returning\");"); + } + self.indent = 0; + self.out.push_str("}\n\n"); + } + + fn stmts(&mut self, body: &[HStmt]) { + for s in body { + self.stmt(s); + } + } + + /// A block: its variables are freed when it ends. + fn nested(&mut self, body: &[HStmt]) { + self.indent += 1; + self.scopes.push(Vec::new()); + self.stmts(body); + self.end_scope(); + self.indent -= 1; + } + + fn mutates(&self, body: &[HStmt], id: u32) -> bool { + let info = &self.info; + mutates_root(body, id, &|o, m| info.is_mut_method(o, m)) + } + + /// Evaluate a condition. If that made values that need freeing, they are freed now + /// and the answer is kept in a variable. + fn cond_value(&mut self, cond: &HExpr) -> String { + let mark = self.pending.len(); + let c = self.expr(cond); + if self.pending.len() > mark { + let b = self.fresh(); + self.line(&format!("bool {b} = {c};")); + self.flush_pending(mark); + b + } else { + c + } + } + + fn stmt(&mut self, s: &HStmt) { + let mark = self.pending.len(); + let leaves = matches!(s, HStmt::Return(_) | HStmt::Break | HStmt::Continue); + self.stmt_inner(s); + if leaves { + // the jump already freed everything; the code after it never runs + self.pending.truncate(mark); + } else { + self.flush_pending(mark); + } + } + + fn stmt_inner(&mut self, s: &HStmt) { + match s { + HStmt::Let { local, init } => { + let v = self.expr(init); + self.take(&v); + let name = local_name(local.id, &local.name); + self.line(&format!("{} {} = {};", c_ty(local.ty), name, v)); + self.own_local(name, local.ty); + } + HStmt::LetMany { locals, init } => { + let v = self.expr(init); + self.take(&v); + let t = self.fresh(); + self.line(&format!("{} {t} = {v};", c_ty(init.ty))); + for (i, l) in locals.iter().enumerate() { + let name = local_name(l.id, &l.name); + self.line(&format!("{} {} = {t}.f{i};", c_ty(l.ty), name)); + self.own_local(name, l.ty); + } + } + HStmt::Assign { target, value } if matches!(&target.kind, HExprKind::Index { base, .. } if matches!(base.ty, Ty::Map(..))) => { + // `m[k] = v` adds the key, or replaces its value + let HExprKind::Index { base, index } = &target.kind else { unreachable!() }; + let v = self.expr(value); + let m = self.map_ref(base); + let k = self.expr(index); + if self.heap(value.ty) { + self.take(&v); + } + self.line(&format!("{}_set(&{m}, {k}, {v});", c_ty(base.ty))); + } + HStmt::Assign { target, value } => { + let v = self.expr(value); + let t = self.place_str(target); + if self.heap(target.ty) { + // the old value is freed once the new one is ready + self.take(&v); + let d = self.drop_line(target.ty, &t); + self.line(&d); + } + self.line(&format!("{t} = {v};")); + } + HStmt::Expr(e) => { + let v = self.expr(e); + if e.ty != Ty::Unit && !v.is_empty() { + self.line(&format!("(void){};", v)); + } + } + HStmt::If { cond, then, else_ } => { + let c = self.cond_value(cond); + self.line(&format!("if ({}) {{", c)); + self.nested(then); + if else_.is_empty() { + self.line("}"); + } else { + self.line("} else {"); + self.nested(else_); + self.line("}"); + } + } + HStmt::Loop(body) => { + self.line("for (;;) {"); + self.loops.push(LoopMark { scopes: self.scopes.len(), pending: self.pending.len() }); + self.nested(body); + self.loops.pop(); + self.line("}"); + } + HStmt::While { cond, body } => { + self.indent += 1; + let (pre, c) = self.capture(|g| g.cond_value(cond)); + self.indent -= 1; + if pre.is_empty() { + self.line(&format!("while ({}) {{", c)); + } else { + self.line("for (;;) {"); + self.out.push_str(&pre); + self.indent += 1; + self.line(&format!("if (!({})) break;", c)); + self.indent -= 1; + } + self.loops.push(LoopMark { scopes: self.scopes.len(), pending: self.pending.len() }); + self.nested(body); + self.loops.pop(); + self.line("}"); + } + HStmt::ForRange { var, start, end, step, body } => { + let mut operands = vec![start, end]; + if let Some(st) = step { + operands.push(st); + } + let vals = self.seq(&operands); + let (cs, ce, cc) = (self.fresh(), self.fresh(), self.fresh()); + self.line("{"); + self.indent += 1; + self.line(&format!("int64_t {} = {};", cs, vals[0])); + self.line(&format!("int64_t {} = {};", ce, vals[1])); + let step_v = vals.get(2).map(|st| { + let n = self.fresh(); + self.line(&format!("int64_t {} = {};", n, st)); + n + }); + match &step_v { + None => self.line(&format!("for (int64_t {cc} = {cs}; {cc} < {ce}; {cc}++) {{")), + Some(step_v) => { + self.line(&format!("if ({step_v} == 0) dino_panic(\"the step of a range can't be 0\");")); + self.line(&format!( + "for (int64_t {cc} = {cs}; {step_v} > 0 ? {cc} < {ce} : {cc} > {ce}; {cc} += {step_v}) {{" + )); + } + } + self.indent += 1; + self.line(&format!("int64_t {} = {};", local_name(var.id, &var.name), cc)); + self.indent -= 1; + self.loops.push(LoopMark { scopes: self.scopes.len(), pending: self.pending.len() }); + self.nested(body); + self.loops.pop(); + self.line("}"); + self.indent -= 1; + self.line("}"); + } + HStmt::ForEach { index, var, iter, body } => self.for_each(index.as_ref(), var, iter, body), + HStmt::Return(None) => { + self.drop_everything(&[]); + self.line("return;"); + } + HStmt::Return(Some(e)) => { + let v = self.expr(e); + // the value moves to the caller; variables that are returned as they are are not freed + let mut skip: Vec = Vec::new(); + if self.heap(e.ty) { + if self.is_pending(&v) { + self.take(&v); + } + self.moved_locals(e, &mut skip); + } + let r = self.fresh(); + self.line(&format!("{} {r} = {v};", c_ty(e.ty))); + self.drop_everything(&skip); + self.line(&format!("return {r};")); + } + HStmt::Break => { + self.drop_loop_exit(); + self.line("break;"); + } + HStmt::Continue => { + self.drop_loop_exit(); + self.line("continue;"); + } + HStmt::Block(b) => { + self.line("{"); + self.nested(b); + self.line("}"); + } + } + } + + /// Variables whose value is returned without a copy (the checker only does that + /// for a variable of the function that is not a view into someone else's data). + fn moved_locals(&self, e: &HExpr, out: &mut Vec) { + match &e.kind { + HExprKind::Local(id, name) if !self.ref_ids.contains(id) => out.push(local_name(*id, name)), + HExprKind::TupleLit(items) => items.iter().for_each(|i| self.moved_locals(i, out)), + _ => {} + } + } + + fn for_each(&mut self, index: Option<&HLocal>, var: &HLocal, iter: &HExpr, body: &[HStmt]) { + let (it, k) = (self.fresh(), self.fresh()); + let mark = self.pending.len(); + self.line("{"); + self.indent += 1; + self.scopes.push(Vec::new()); + // Walk over the list as it was when the loop started. A list that the body + // doesn't change can be read in place; otherwise the loop reads a copy. + let (src, owned) = if is_place(iter) { + let place = self.place_str(iter); + let root = place_root(iter).map(|(id, _)| id).unwrap(); + if self.mutates(body, root) { + (self.copy_of(iter.ty, &place), true) + } else { + (place, false) + } + } else { + let v = self.expr(iter); + self.take(&v); + (v, true) + }; + self.line(&format!("{} {it} = {src};", c_ty(iter.ty))); + if owned { + self.own_local(it.clone(), iter.ty); + } + self.flush_pending(mark); + let w = self.fresh(); + if iter.ty == Ty::Str { + // characters have different widths in bytes: each round steps over the one just read + self.line(&format!("for (int64_t {k} = 0, {w} = 0; {k} < {it}.n; {k} += {w}) {{")); + } else { + self.line(&format!("for (int64_t {k} = 0; {k} < {it}.len; {k}++) {{")); + } + self.indent += 1; + self.scopes.push(Vec::new()); + self.loops.push(LoopMark { scopes: self.scopes.len() - 1, pending: self.pending.len() }); + if let (Some(ix), false) = (index, matches!(iter.ty, Ty::Map(..))) { + self.line(&format!("int64_t {} = {k};", local_name(ix.id, &ix.name))); + } + if let Ty::Map(..) = iter.ty { + // `for k, v in m` walks the entries in the order they were added + let mut binds: Vec<(&HLocal, String)> = Vec::new(); + match index { + Some(key) => { + binds.push((key, format!("{it}.k[{k}]"))); + binds.push((var, format!("{it}.v[{k}]"))); + } + None => binds.push((var, format!("{it}.k[{k}]"))), + } + for (l, elem) in binds { + let copied = self.mutates(body, l.id); + let init = if copied { self.copy_of(l.ty, &elem) } else { elem }; + let name = local_name(l.id, &l.name); + self.line(&format!("{} {} = {};", c_ty(l.ty), name, init)); + if copied { + self.own_local(name, l.ty); + } + } + } else if iter.ty == Ty::Str { + let name = local_name(var.id, &var.name); + self.line(&format!("uint32_t {name} = 0;")); + self.line(&format!("{w} = dino_utf8_decode({it}.p + {k}, {it}.n - {k}, &{name});")); + } else { + let elem = format!("{it}.p[{k}]"); + // a list or struct element is copied only if the body changes the loop variable + let copied = self.mutates(body, var.id); + let init = if copied { self.copy_of(var.ty, &elem) } else { elem }; + let name = local_name(var.id, &var.name); + self.line(&format!("{} {} = {};", c_ty(var.ty), name, init)); + if copied { + self.own_local(name, var.ty); + } + } + self.stmts(body); + self.loops.pop(); + self.end_scope(); + self.indent -= 1; + self.line("}"); + self.end_scope(); + self.indent -= 1; + self.line("}"); + } + + // ---------- expressions ---------- + + /// Evaluate operands left to right. An operand that is read before a later operand + /// runs statements that could change it (or panic first) is copied to a temporary first. + fn seq(&mut self, es: &[&HExpr]) -> Vec { + let mut vals: Vec = Vec::new(); + for (j, e) in es.iter().enumerate() { + let (pre, v) = self.capture(|g| g.expr(e)); + if !pre.is_empty() { + let mutation = self.info.has_mutation(e); + let panics = has_index(e); + for (i, prev) in vals.iter_mut().enumerate() { + let earlier = es[i]; + // fresh temporaries can't be changed by anyone else, so they need no snapshot + if is_literal(earlier) || self.is_pending(prev) || !(mutation || has_index(earlier) || panics) { + continue; + } + let t = format!("t{}", { + self.tmp += 1; + self.tmp + }); + // a deep copy only when the later operand really changes data + let init = if mutation { self.copy_of(earlier.ty, prev) } else { prev.clone() }; + for _ in 0..self.indent { + self.out.push_str(" "); + } + self.out.push_str(&format!("{} {} = {};\n", c_ty(earlier.ty), t, init)); + if mutation { + // a real copy: it is ours to free + self.register_temp(&t, earlier.ty); + } + *prev = t; + } + let _ = j; + self.out.push_str(&pre); + } + vals.push(v); + } + vals + } + + /// The C lvalue for a variable, field, or list element. Variables and fields have no + /// side effects; an index runs its bounds check when the place is used. + fn place_str(&mut self, e: &HExpr) -> String { + match &e.kind { + HExprKind::Local(id, name) => { + if self.ref_ids.contains(id) { + format!("(*{})", local_name(*id, name)) + } else { + local_name(*id, name) + } + } + HExprKind::Field { base, name } => { + let b = if is_place(base) { self.place_str(base) } else { self.expr(base) }; + format!("{}.{}", b, field_name(name)) + } + HExprKind::Deref(inner) => { + let h = if is_place(inner) { self.place_str(inner) } else { self.expr(inner) }; + format!("(*{}_ref({h}))", c_ty(inner.ty)) + } + HExprKind::Index { base, index } => { + let b = if is_place(base) { + self.place_str(base) + } else { + let v = self.expr(base); + let t = self.fresh(); + self.line(&format!("{} {t} = {v};", c_ty(base.ty))); + t + }; + let idx = self.expr(index); + if base.ty == Ty::Str { + // a character of a string is a value, not a place + return format!("dino_str_char_at({b}, {idx})"); + } + format!("(*{}_at(&{b}, {idx}))", c_ty(base.ty)) + } + HExprKind::Unwrap(inner) => { + let b = if is_place(inner) { + self.place_str(inner) + } else { + let v = self.expr(inner); + let t = self.fresh(); + self.line(&format!("{} {t} = {v};", c_ty(inner.ty))); + t + }; + self.line(&format!("if (!{b}.has) dino_panic(\"used a nil value\");")); + format!("{b}.v") + } + _ => self.expr(e), + } + } + + /// Emit any statements the expression needs and return a C expression for its value. + /// For `nothing`-typed calls the result is empty. A new value that owns heap data + /// always ends up in a variable that is registered as pending. + fn expr(&mut self, e: &HExpr) -> String { + let v = self.expr_inner(e); + let builds_in_place = match &e.kind { + HExprKind::StructLit { .. } + | HExprKind::EnumLit { .. } + | HExprKind::Wrap(_) + | HExprKind::TupleLit(_) + | HExprKind::MakeError(_) => true, + HExprKind::ListLit { elems } => elems.is_empty(), + _ => false, + }; + if builds_in_place && self.heap(e.ty) { + let t = self.fresh(); + self.line(&format!("{} {t} = {v};", c_ty(e.ty))); + self.register_temp(&t, e.ty); + return t; + } + v + } + + /// Give every heap value in `es` (already computed as `vs`) to a new owner. + fn take_all(&mut self, es: &[&HExpr], vs: &[String]) { + for (e, v) in es.iter().zip(vs) { + if self.heap(e.ty) { + self.take(v); + } + } + } + + fn expr_inner(&mut self, e: &HExpr) -> String { + match &e.kind { + HExprKind::Int(n) => match e.ty { + Ty::Num(k) => format!("(({})INT64_C({n}))", num_c(k)), + _ => format!("INT64_C({n})"), + }, + HExprKind::Float(x) => match e.ty { + Ty::Num(_) => format!("((float){:?})", x), + _ => format!("{:?}", x), + }, + HExprKind::Bool(b) => b.to_string(), + HExprKind::Str(s) => str_lit(s), + HExprKind::Local(..) + | HExprKind::Field { .. } + | HExprKind::Index { .. } + | HExprKind::Unwrap(_) => self.place_str(e), + HExprKind::Nil => format!("(({}){{0}})", c_ty(e.ty)), + HExprKind::Wrap(inner) => { + let v = self.expr(inner); + if self.heap(inner.ty) { + self.take(&v); + } + format!("(({}){{ .has = true, .v = {v} }})", c_ty(e.ty)) + } + HExprKind::MakeError(msg) => { + let v = self.expr(msg); + self.take(&v); + format!("((dino_err){{ .set = true, .msg = {v} }})") + } + HExprKind::ToIface(inner) => { + let v = self.expr(inner); + self.take(&v); + self.materialize(e.ty, format!("{}_from_{}({v})", c_ty(e.ty), c_ty(inner.ty))) + } + HExprKind::IfaceCall { recv, method, args } => { + let held = if is_place(recv) { + None + } else { + let v = self.expr(recv); + let t = self.fresh(); + self.line(&format!("{} {t} = {v};", c_ty(recv.ty))); + Some(t) + }; + let refs: Vec<&HExpr> = args.iter().collect(); + let vs = self.seq(&refs); + self.take_all(&refs, &vs); + let cs = match held { + Some(t) => t, + None => self.place_str(recv), + }; + self.line(&format!("if (!{cs}.data) dino_panic(\"called a method on an empty interface value\");")); + let mut all = vec![format!("{cs}.data")]; + all.extend(vs); + self.materialize(e.ty, format!("{cs}.vt->m_{}({})", mangle(method), all.join(", "))) + } + HExprKind::CallValue { callee, args } => { + let held = if is_place(callee) { + None + } else { + let v = self.expr(callee); + let t = self.fresh(); + self.line(&format!("{} {t} = {v};", c_ty(callee.ty))); + Some(t) + }; + let refs: Vec<&HExpr> = args.iter().collect(); + let vs = self.seq(&refs); + self.take_all(&refs, &vs); + let cs = match held { + Some(t) => t, + None => self.place_str(callee), + }; + self.line(&format!("if (!{cs}.fn) dino_panic(\"called an empty function value\");")); + let mut all = vec![format!("{cs}.env")]; + all.extend(vs); + self.materialize(e.ty, format!("{cs}.fn({})", all.join(", "))) + } + HExprKind::Closure { params, ret, body, captures } => self.closure(e.ty, params, *ret, body, captures), + HExprKind::NewShared(inner) => { + let v = self.expr(inner); + self.take(&v); + self.materialize(e.ty, format!("{}_new({v})", c_ty(e.ty))) + } + HExprKind::Downgrade(inner) => { + let h = if is_place(inner) { self.place_str(inner) } else { self.expr(inner) }; + self.materialize(e.ty, format!("{}_down({h})", c_ty(e.ty))) + } + HExprKind::Deref(_) => self.place_str(e), + HExprKind::Upgrade(inner) => { + let w = if is_place(inner) { self.place_str(inner) } else { self.expr(inner) }; + let r = self.fresh(); + let wk = c_ty(inner.ty); + self.line(&format!("{} {r} = {{0}};", c_ty(e.ty))); + self.line(&format!("if ({wk}_alive({w})) {{ {r}.has = true; {r}.v = {wk}_up({w}); }}")); + self.register_temp(&r, e.ty); + r + } + HExprKind::IsSet(inner) => { + let v = self.expr(inner); + if matches!(inner.ty, Ty::Iface(_)) { + format!("(({v}).data != NULL)") + } else if matches!(inner.ty, Ty::Weak(_)) { + format!("{}_alive({v})", c_ty(inner.ty)) + } else if inner.ty == Ty::Error { + format!("(({v}).set)") + } else { + format!("(({v}).has)") + } + } + HExprKind::TupleLit(items) => { + let refs: Vec<&HExpr> = items.iter().collect(); + let vs = self.seq(&refs); + self.take_all(&refs, &vs); + let inits: Vec = vs.iter().enumerate().map(|(i, v)| format!(".f{i} = {v}")).collect(); + format!("(({}){{ {} }})", c_ty(e.ty), inits.join(", ")) + } + HExprKind::Try(inner) => { + // the call's result is a variable (an error is heap data); we handle it from here + let t = self.expr(inner); + self.take(&t); + let err = if inner.ty == Ty::Error { t.clone() } else { format!("{t}.f1") }; + self.line(&format!("if ({err}.set) {{")); + self.indent += 1; + // the error moves to our caller; the rest of the result is freed + if let Ty::Tuple(items) = inner.ty { + if self.heap(items[0]) { + let d = self.drop_line(items[0], &format!("{t}.f0")); + self.line(&d); + } + } + self.drop_everything(&[]); + let ret_val = match self.cur_ret { + Ty::Tuple(items) => { + let mut parts: Vec = + (0..items.len() - 1).map(|i| format!(".f{i} = ({}){{0}}", c_ty(items[i]))).collect(); + parts.push(format!(".f{} = {err}", items.len() - 1)); + format!("(({}){{ {} }})", c_ty(self.cur_ret), parts.join(", ")) + } + _ => err.clone(), + }; + self.line(&format!("return {ret_val};")); + self.indent -= 1; + self.line("}"); + match inner.ty { + Ty::Tuple(items) => { + let r = self.fresh(); + self.line(&format!("{} {r} = {t}.f0;", c_ty(items[0]))); + self.register_temp(&r, items[0]); + r + } + _ => String::new(), + } + } + HExprKind::Unary { op, expr } => { + let v = self.expr(expr); + match op { + UnOp::Neg if matches!(e.ty, Ty::Num(k) if !k.is_float()) => { + format!("(({})(-{v}))", c_ty(e.ty)) + } + UnOp::Neg => format!("(-{v})"), + UnOp::Not => format!("(!{v})"), + } + } + HExprKind::Binary { op, lhs, rhs } => self.binary(*op, lhs, rhs), + HExprKind::Builtin { c, shape, args } => { + // arguments are only borrowed; the result is a new value + let refs: Vec<&HExpr> = args.iter().collect(); + let vs = self.seq(&refs); + match shape { + BuiltinShape::Plain => self.materialize(e.ty, format!("{c}({})", vs.join(", "))), + BuiltinShape::ErrOut | BuiltinShape::OptOut => { + let t = self.fresh(); + let mut call_args = vs.clone(); + let (field, out_field) = if *shape == BuiltinShape::ErrOut { ("f1", "f0") } else { ("has", "v") }; + call_args.push(format!("&{t}.{out_field}")); + self.line(&format!("{} {t};", c_ty(e.ty))); + self.line(&format!("{t}.{field} = {c}({});", call_args.join(", "))); + self.register_temp(&t, e.ty); + t + } + BuiltinShape::ListOut => { + let t = self.fresh(); + self.line(&format!("{} {t} = {{0}};", c_ty(e.ty))); + self.line(&format!("{c}({}, (void **)&{t}.p, &{t}.len, &{t}.cap);", vs.join(", "))); + self.register_temp(&t, e.ty); + t + } + BuiltinShape::Min => format!("(({0}) < ({1}) ? ({0}) : ({1}))", vs[0], vs[1]), + BuiltinShape::Max => format!("(({0}) > ({1}) ? ({0}) : ({1}))", vs[0], vs[1]), + BuiltinShape::Abs => format!("(({ct})(({0}) < 0 ? -({0}) : ({0})))", vs[0], ct = c_ty(e.ty)), + } + } + HExprKind::Call { func, args } => { + let cname = func_cname(None, func); + let vs = self.arg_list(&cname, args); + self.materialize(e.ty, format!("{cname}({})", vs.join(", "))) + } + HExprKind::MethodCall { owner, method, recv, args, .. } => { + let cname = func_cname(Some(owner), method); + let recv_arg = if is_place(recv) { + None + } else { + let v = self.expr(recv); + let t = self.fresh(); + self.line(&format!("{} {t} = {v};", c_ty(recv.ty))); + Some(format!("&{t}")) + }; + let mut vs = self.arg_list(&cname, args); + // a variable receiver is passed by address, taken after the arguments ran + let recv_arg = match recv_arg { + Some(r) => r, + None => format!("&{}", self.place_str(recv)), + }; + vs.insert(0, recv_arg); + self.materialize(e.ty, format!("{cname}({})", vs.join(", "))) + } + HExprKind::Print(args) => { + let refs: Vec<&HExpr> = args.iter().collect(); + let vs = self.seq(&refs); + for (i, (a, v)) in args.iter().zip(&vs).enumerate() { + if i > 0 { + self.line("dino_print_sp();"); + } + let stmt = match a.ty { + Ty::Int => format!("dino_print_i64({v});"), + Ty::Float => format!("dino_print_f64({v});"), + Ty::Bool => format!("dino_print_bool({v});"), + Ty::Char => format!("dino_print_char({v});"), + Ty::Str => format!("dino_print_str({v});"), + Ty::Num(Num::U64) => format!("dino_print_u64({v});"), + Ty::Num(Num::F32) => format!("dino_print_f32({v});"), + Ty::Num(_) => format!("dino_print_i64((int64_t)({v}));"), + Ty::Struct(_) | Ty::Enum(_) | Ty::List(_) | Ty::Map(..) | Ty::Opt(_) | Ty::Error | Ty::Shared(_) | Ty::Weak(_) | Ty::Func(_) | Ty::Iface(_) => { + let t = self.fresh(); + self.line(&format!("dstr {t} = {};", to_str(a.ty, v))); + self.register_temp(&t, Ty::Str); + format!("dino_print_str({t});") + } + Ty::Tuple(_) | Ty::Unit => unreachable!(), + }; + self.line(&stmt); + } + self.line("dino_print_nl();"); + String::new() + } + HExprKind::Convert(inner) => { + let v = self.expr(inner); + let to = c_ty(e.ty); + match (inner.ty.is_float(), e.ty) { + // float to integer: out-of-range values stop at the nearest end, NaN is 0 + (true, Ty::Int) => format!("dino_f2i((double){v})"), + (true, Ty::Num(Num::U64)) => format!("dino_f2u64((double){v})"), + (true, Ty::Num(k)) if !k.is_float() => { + let (lo, hi) = k.range(); + format!("(({to})dino_f2i_clamp((double){v}, INT64_C({lo}), INT64_C({hi})))") + } + // everything else is a plain C conversion (integers wrap) + _ => format!("(({to}){v})"), + } + } + HExprKind::Interp(parts) => { + let values: Vec<&HExpr> = parts + .iter() + .filter_map(|p| match p { + HInterp::Value(v) => Some(v), + HInterp::Text(_) => None, + }) + .collect(); + let vs = self.seq(&values); + let mut vs = vs.into_iter(); + let mut cs = Vec::new(); + for p in parts { + cs.push(match p { + HInterp::Text(t) => str_lit(t), + HInterp::Value(v) => { + let val = vs.next().unwrap(); + if v.ty == Ty::Str { + val + } else { + // the text of a number, struct, ...: a new string, freed after the join + let p = self.fresh(); + self.line(&format!("dstr {p} = {};", to_str(v.ty, &val))); + self.register_temp(&p, Ty::Str); + p + } + } + }); + } + let t = self.fresh(); + self.line(&format!("dstr {t} = dino_cat_n({}, (dstr[]){{{}}});", cs.len(), cs.join(", "))); + self.register_temp(&t, Ty::Str); + t + } + HExprKind::StructLit { name, fields } => { + let c = c_struct(name); + if fields.is_empty() { + return format!("(({c}){{0}})"); + } + // values are evaluated in the order written; C places them by field name + let refs: Vec<&HExpr> = fields.iter().map(|(_, v)| v).collect(); + let vs = self.seq(&refs); + self.take_all(&refs, &vs); + let inits: Vec = + fields.iter().zip(&vs).map(|((n, _), v)| format!(".{} = {}", field_name(n), v)).collect(); + format!("(({c}){{{}}})", inits.join(", ")) + } + HExprKind::EnumLit { enum_name, variant, fields } => { + let c = c_struct(enum_name); + if fields.is_empty() { + return format!("(({c}){{ .tag = {variant} }})"); + } + let def = self.enums[enum_name].variants[*variant].clone(); + let refs: Vec<&HExpr> = fields.iter().collect(); + let vs = self.seq(&refs); + self.take_all(&refs, &vs); + let inits: Vec = + def.fields.iter().zip(&vs).map(|((n, _), v)| format!(".{} = {}", field_name(n), v)).collect(); + format!("(({c}){{ .tag = {variant}, .u.{} = {{ {} }} }})", variant_member(&def.name), inits.join(", ")) + } + HExprKind::Match { scrutinee, arms } => self.match_expr(e, scrutinee, arms), + HExprKind::ListLit { elems } => { + let c = c_ty(e.ty); + if elems.is_empty() { + return format!("(({c}){{0}})"); + } + let Ty::List(elem) = e.ty else { unreachable!() }; + let refs: Vec<&HExpr> = elems.iter().collect(); + let vs = self.seq(&refs); + self.take_all(&refs, &vs); + let (t, n, ct) = (self.fresh(), elems.len(), c_ty(*elem)); + self.line(&format!("{c} {t} = {{0}};")); + self.line(&format!("{t}.p = ({ct} *)malloc({n} * sizeof({ct}));")); + self.line(&format!("if (!{t}.p) dino_panic(\"out of memory\");")); + self.line(&format!("{t}.len = {t}.cap = {n};")); + for (i, v) in vs.iter().enumerate() { + self.line(&format!("{t}.p[{i}] = {v};")); + } + self.register_temp(&t, e.ty); + t + } + HExprKind::MapLit { entries } => { + let c = c_ty(e.ty); + if entries.is_empty() { + return format!("(({c}){{0}})"); + } + let mut refs: Vec<&HExpr> = Vec::new(); + for (k, v) in entries { + refs.push(k); + refs.push(v); + } + let vs = self.seq(&refs); + for (r, v) in refs.iter().skip(1).step_by(2).zip(vs.iter().skip(1).step_by(2)) { + if self.heap(r.ty) { + self.take(v); + } + } + let t = self.fresh(); + self.line(&format!("{c} {t} = {{0}};")); + for pair in vs.chunks(2) { + self.line(&format!("{c}_set(&{t}, {}, {});", pair[0], pair[1])); + } + self.register_temp(&t, e.ty); + t + } + HExprKind::MapCall { map, op, args } => { + let m = self.map_ref(map); + let Ty::Map(_, val) = map.ty else { unreachable!() }; + let c = c_ty(map.ty); + let refs: Vec<&HExpr> = args.iter().collect(); + let vs = self.seq(&refs); + match op { + MapOp::Has => format!("({c}_find(&{m}, {}) >= 0)", vs[0]), + MapOp::Zero => format!("{c}_zero_get(&{m}, {})", vs[0]), + MapOp::Remove => { + self.line(&format!("{c}_remove(&{m}, {});", vs[0])); + String::new() + } + MapOp::Get => { + let t = self.fresh(); + let copy = self.copy_of(*val, "(*p)"); + self.line(&format!("{} {t} = {{0}};", c_ty(e.ty))); + self.line(&format!("{{ {} *p = {c}_ptr(&{m}, {}); if (p) {{ {t}.has = true; {t}.v = {copy}; }} }}", c_ty(*val), vs[0])); + self.register_temp(&t, e.ty); + t + } + MapOp::GetOr => { + let t = self.fresh(); + let copy = self.copy_of(*val, "(*p)"); + let dflt = self.copy_of(*val, &vs[1]); + self.line(&format!("{} {t};", c_ty(*val))); + self.line(&format!("{{ {} *p = {c}_ptr(&{m}, {}); {t} = p ? {copy} : {dflt}; }}", c_ty(*val), vs[0])); + self.register_temp(&t, e.ty); + t + } + MapOp::Keys | MapOp::Values => { + let (src, et) = if *op == MapOp::Keys { + ("k", match map.ty { Ty::Map(k, _) => *k, _ => unreachable!() }) + } else { + ("v", *val) + }; + let (t, lc, ect) = (self.fresh(), c_ty(e.ty), c_ty(et)); + let copy = self.copy_of(et, &format!("{m}.{src}[i]")); + self.line(&format!("{lc} {t} = {{0}};")); + self.line(&format!( + "if ({m}.len > 0) {{ {t}.p = ({ect} *)malloc((size_t){m}.len * sizeof({ect})); if (!{t}.p) dino_panic(\"out of memory\"); {t}.len = {t}.cap = {m}.len; for (int64_t i = 0; i < {m}.len; i++) {t}.p[i] = {copy}; }}" + )); + self.register_temp(&t, e.ty); + t + } + } + } + HExprKind::Slice { base, lo, hi } => { + let mut operands: Vec<&HExpr> = Vec::new(); + if let Some(l) = lo { + operands.push(l); + } + if let Some(h) = hi { + operands.push(h); + } + let b = if is_place(base) { + self.place_str(base) + } else { + let v = self.expr(base); + let t = self.fresh(); + self.line(&format!("{} {t} = {v};", c_ty(base.ty))); + t + }; + let mut vs = self.seq(&operands).into_iter(); + let lo_s = if lo.is_some() { vs.next().unwrap() } else { "0".to_string() }; + let len_field = if base.ty == Ty::Str { "n" } else { "len" }; + let hi_s = if hi.is_some() { vs.next().unwrap() } else { format!("({b}).{len_field}") }; + self.materialize(e.ty, format!("{}_slice({b}, {lo_s}, {hi_s})", c_ty(base.ty))) + } + HExprKind::ListPush { list, value } => { + let v = self.expr(value); + if self.heap(value.ty) { + self.take(&v); + } + let l = self.place_str(list); + self.line(&format!("{}_push(&{l}, {v});", c_ty(list.ty))); + String::new() + } + HExprKind::ListPop(list) => { + let l = self.place_str(list); + self.materialize(e.ty, format!("{}_pop(&{l})", c_ty(list.ty))) + } + HExprKind::Len(inner) => { + let v = self.expr(inner); + if inner.ty == Ty::Str { + format!("({v}).n") + } else { + format!("({v}).len") + } + } + HExprKind::Copy(inner) => { + let p = self.place_str(inner); + let copy = self.copy_of(inner.ty, &p); + self.materialize(e.ty, copy) + } + HExprKind::Move(inner) => { + // hand the value over and leave the variable empty (its end-of-scope free is then a no-op) + let p = self.place_str(inner); + let t = self.materialize(e.ty, p.clone()); + self.line(&format!("{p} = ({}){{0}};", c_ty(e.ty))); + t + } + } + } + + /// Arguments for a call to `cname`. Borrowed parameters are passed by address + /// (no copy); the rest by value. + fn arg_list(&mut self, cname: &str, args: &[HExpr]) -> Vec { + let flags = self.info.borrowed.get(cname).cloned().unwrap_or_default(); + let ops: Vec<&HExpr> = args + .iter() + .enumerate() + .map(|(i, a)| match (&a.kind, flags.get(i)) { + (HExprKind::Copy(inner) | HExprKind::Move(inner), Some(true)) => &**inner, + _ => a, + }) + .collect(); + let vals = self.seq(&ops); + let mut out = Vec::new(); + for (i, v) in vals.into_iter().enumerate() { + if flags.get(i) == Some(&true) { + // borrowed: passed by address, so it needs to be a variable + let v = if is_place(ops[i]) || self.is_pending(&v) { + v + } else { + let t = self.fresh(); + self.line(&format!("{} {t} = {v};", c_ty(ops[i].ty))); + t + }; + out.push(format!("&{v}")); + } else { + // by value: a heap value moves into the callee + if self.heap(args[i].ty) { + self.take(&v); + } + out.push(v); + } + } + out + } + + fn arm_mutates(&self, body: &HArmBody, id: u32) -> bool { + match body { + HArmBody::Block(b) => self.mutates(b, id), + HArmBody::Expr(x) => self.mutates(&[HStmt::Expr(x.clone())], id), + } + } + + /// `match` becomes an `if`/`else if` chain on a snapshot of the matched value, + /// so `break`, `continue` and `return` inside an arm work as usual. + fn match_expr(&mut self, e: &HExpr, scrutinee: &HExpr, arms: &[HArm]) -> String { + let sty = scrutinee.ty; + let sv = self.expr(scrutinee); + let s = self.fresh(); + self.line(&format!("{} {s} = {sv};", c_ty(sty))); + let scrut_root = place_root(scrutinee).map(|(id, _)| id); + let result = if e.ty != Ty::Unit { + let r = self.fresh(); + self.line(&format!("{} {r};", c_ty(e.ty))); + Some(r) + } else { + None + }; + let n = arms.len(); + for (i, arm) in arms.iter().enumerate() { + let cond = match &arm.pat { + HPat::Variant { index, .. } => Some(format!("{s}.tag == {index}")), + HPat::Int(k) => Some(format!("{s} == INT64_C({k})")), + HPat::Bool(b) => Some(format!("{s} == {b}")), + HPat::Str(t) => Some(format!("dino_streq({s}, {})", str_lit(t))), + HPat::CatchAll(_) => None, + }; + // the last arm needs no test: the checker made sure the arms cover everything + let head = match (i, i + 1 == n, cond) { + (0, true, _) | (0, _, None) => "{".to_string(), + (0, false, Some(c)) => format!("if ({c}) {{"), + (_, true, _) | (_, _, None) => "} else {".to_string(), + (_, false, Some(c)) => format!("}} else if ({c}) {{"), + }; + self.line(&head); + self.indent += 1; + self.scopes.push(Vec::new()); + let arm_mark = self.pending.len(); + + let mut binds: Vec<(&HLocal, String)> = Vec::new(); + match &arm.pat { + HPat::Variant { index, binds: bs } => { + let Ty::Enum(en) = sty else { unreachable!() }; + let v = self.enums[en].variants[*index].clone(); + for (b, (fname, _)) in bs.iter().zip(&v.fields) { + if let Some(local) = b { + binds.push((local, format!("{s}.u.{}.{}", variant_member(&v.name), field_name(fname)))); + } + } + } + HPat::CatchAll(Some(local)) => binds.push((local, s.clone())), + _ => {} + } + for (local, src) in binds { + // data with a list inside is only copied if someone could change it meanwhile + let copy = self.info.needs_copy(local.ty) + && (self.arm_mutates(&arm.body, local.id) + || scrut_root.map_or(false, |r| self.arm_mutates(&arm.body, r))); + let init = if copy { self.copy_of(local.ty, &src) } else { src }; + let name = local_name(local.id, &local.name); + self.line(&format!("{} {} = {};", c_ty(local.ty), name, init)); + if copy { + self.own_local(name, local.ty); + } + } + + match &arm.body { + HArmBody::Expr(x) => { + let v = self.expr(x); + match &result { + Some(r) => { + if self.heap(x.ty) { + self.take(&v); + } + self.line(&format!("{r} = {v};")); + } + None if x.ty != Ty::Unit && !v.is_empty() => self.line(&format!("(void){v};")), + None => {} + } + self.flush_pending(arm_mark); + } + HArmBody::Block(b) => self.stmts(b), + } + self.end_scope(); + self.indent -= 1; + } + self.line("}"); + match result { + Some(r) => { + self.register_temp(&r, e.ty); + r + } + None => String::new(), + } + } + + /// Store a call's result in a temporary so it runs now, in order. + /// A C lvalue for a map, whose address can be taken. + fn map_ref(&mut self, map: &HExpr) -> String { + if is_place(map) { + self.place_str(map) + } else { + let v = self.expr(map); + let t = self.fresh(); + self.line(&format!("{} {t} = {v};", c_ty(map.ty))); + t + } + } + + fn materialize(&mut self, ty: Ty, call: String) -> String { + if ty == Ty::Unit { + self.line(&format!("{call};")); + String::new() + } else { + let t = self.fresh(); + self.line(&format!("{} {t} = {call};", c_ty(ty))); + self.register_temp(&t, ty); + t + } + } + + fn binary(&mut self, op: BinOp, lhs: &HExpr, rhs: &HExpr) -> String { + // `&&` and `||` only evaluate the right side when needed. + if matches!(op, BinOp::And | BinOp::Or) { + let l = self.expr(lhs); + self.indent += 1; + let (pre, r) = self.capture(|g| g.cond_value(rhs)); + self.indent -= 1; + let op_s = sym(op); + if pre.is_empty() { + return format!("({l} {op_s} {r})"); + } + let t = self.fresh(); + self.line(&format!("bool {t} = {l};")); + self.line(&if op == BinOp::And { format!("if ({t}) {{") } else { format!("if (!{t}) {{") }); + self.out.push_str(&pre); + self.indent += 1; + self.line(&format!("{t} = {r};")); + self.indent -= 1; + self.line("}"); + return t; + } + let vs = self.seq(&[lhs, rhs]); + let (l, r) = (&vs[0], &vs[1]); + match (op, lhs.ty) { + (BinOp::Div | BinOp::Rem, Ty::Num(k)) if !k.is_float() => { + let name = if op == BinOp::Div { "div" } else { "rem" }; + let call = if k == Num::U64 { + format!("dino_u{name}({l}, {r})") + } else { + format!("(({})dino_{name}((int64_t){l}, (int64_t){r}))", num_c(k)) + }; + self.materialize(lhs.ty, call) + } + // sized integers wrap around: C would compute in `int`, so cut the result back down + (BinOp::Add | BinOp::Sub | BinOp::Mul, Ty::Num(k)) if !k.is_float() => { + format!("(({})({l} {} {r}))", num_c(k), sym(op)) + } + (BinOp::Div, Ty::Int) => self.materialize(Ty::Int, format!("dino_div({l}, {r})")), + (BinOp::Rem, Ty::Int) => self.materialize(Ty::Int, format!("dino_rem({l}, {r})")), + (BinOp::Eq | BinOp::Ne, Ty::Enum(_)) => format!("({l}.tag {} {r}.tag)", sym(op)), + (BinOp::Add, Ty::Str) => self.materialize(Ty::Str, format!("dino_cat({l}, {r})")), + (BinOp::Eq, Ty::Str) => format!("dino_streq({l}, {r})"), + (BinOp::Ne, Ty::Str) => format!("(!dino_streq({l}, {r}))"), + (BinOp::Lt | BinOp::Gt | BinOp::Le | BinOp::Ge, Ty::Str) => { + format!("(dino_strcmp({l}, {r}) {} 0)", sym(op)) + } + _ => format!("({l} {} {r})", sym(op)), + } + } +} + +/// The statement that frees a value held in a map entry (nothing for plain numbers). +fn drop_entry_v(g: &Gen, ty: Ty, lv: &str) -> String { + if g.info.needs_copy(ty) { + g.drop_line(ty, lv) + } else { + String::new() + } +} diff --git a/src/driver.rs b/src/driver.rs new file mode 100644 index 0000000..ed97781 --- /dev/null +++ b/src/driver.rs @@ -0,0 +1,110 @@ +//! Ties the compiler stages together and invokes the C compiler. + +use std::collections::HashMap; +use std::path::{Path, PathBuf}; +use std::process::Command; +use std::time::{SystemTime, UNIX_EPOCH}; + +use crate::error::{Diag, PResult}; +use crate::{check, codegen_c, loader, parser}; + +/// Dino source -> C source, for a program in a single file. +pub fn compile_to_c(src: &str) -> PResult { + let ast = parser::parse(src)?; + let hir = check::check(&ast)?; + Ok(codegen_c::generate(&hir)) +} + +/// A whole program: the main file and the files it imports. `read(key)` finds a module +/// (see `loader`). Errors come back already rendered, naming the file they are in. +pub fn compile_project( + main_path: &str, + main_src: &str, + mut read: impl FnMut(&str) -> Option<(String, String)>, +) -> Result { + let mut seen: HashMap = HashMap::new(); + let loaded = loader::load(main_src, &mut |key| { + let found = read(key); + if let Some((path, src)) = &found { + seen.insert(key.to_string(), (path.clone(), src.clone())); + } + found + }); + let render = |d: Diag| -> String { + match d.file.as_deref().and_then(|k| seen.get(k)) { + Some((path, src)) => d.render(path, src), + None if d.file.is_some() => d.render(d.file.as_deref().unwrap(), ""), + None => d.render(main_path, main_src), + } + }; + let loaded = loaded.map_err(&render)?; + let modules: Vec<(String, crate::ast::Program)> = + loaded.modules.into_iter().map(|m| (m.key, m.program)).collect(); + let hir = check::check_project(&loaded.main, &modules).map_err(&render)?; + Ok(codegen_c::generate(&hir)) +} + +/// Compile the program whose main file is `path`; imports are found next to it. +pub fn compile_file(path: &str) -> Result { + let src = std::fs::read_to_string(path).map_err(|e| format!("error: cannot read {path}: {e}"))?; + let dir = Path::new(path).parent().map(Path::to_path_buf).unwrap_or_default(); + compile_project(path, &src, |key| { + let file = dir.join(format!("{}.dino", key.replace('.', "/"))); + std::fs::read_to_string(&file).ok().map(|s| (file.display().to_string(), s)) + }) +} + +fn unique_name(tag: &str, ext: &str) -> PathBuf { + // the counter makes names differ even when two threads ask in the same nanosecond + static COUNTER: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0); + let n = COUNTER.fetch_add(1, std::sync::atomic::Ordering::Relaxed); + let nanos = SystemTime::now().duration_since(UNIX_EPOCH).map(|d| d.as_nanos()).unwrap_or(0); + std::env::temp_dir().join(format!("dino-{}-{}-{}-{}{}", std::process::id(), nanos, n, tag, ext)) +} + +/// A path for a throw-away executable. +pub fn temp_exe() -> PathBuf { + unique_name("exe", if cfg!(windows) { ".exe" } else { "" }) +} + +/// Compile C source to an executable with the system C compiler (`$CC`, else `cc`). +pub fn build_exe(c_code: &str, out: &Path) -> Result<(), String> { + build_exe_with(c_code, out, &["-O2", "-std=c11", "-fwrapv", "-w"]) +} + +/// Like `build_exe`, with explicit compiler flags (used by tests to turn on strict warnings). +pub fn build_exe_with(c_code: &str, out: &Path, flags: &[&str]) -> Result<(), String> { + let c_file = unique_name("prog", ".c"); + std::fs::write(&c_file, c_code).map_err(|e| format!("cannot write {}: {e}", c_file.display()))?; + let cc = std::env::var("CC").unwrap_or_else(|_| "cc".to_string()); + let result = Command::new(&cc) + .args(flags) + .arg("-o") + .arg(out) + .arg(&c_file) + .arg("-lm") // the math functions + .output(); + let _ = std::fs::remove_file(&c_file); + match result { + Err(e) => Err(format!("cannot run the C compiler `{cc}`: {e}\nInstall one (gcc or clang) or set $CC.")), + Ok(o) if !o.status.success() => Err(format!( + "the C compiler rejected the generated code (this is a bug in Dino):\n{}", + String::from_utf8_lossy(&o.stderr) + )), + Ok(_) => Ok(()), + } +} + +/// Check a whole program without building it, for editors: `main_src` is the text of the main +/// file (it may differ from what is on disk) and `read(key)` finds the other files. +/// The error's `file` says which module it is in (`None`: the main file). +pub fn diagnose( + main_src: &str, + mut read: impl FnMut(&str) -> Option<(String, String)>, +) -> Result<(), Diag> { + let loaded = loader::load(main_src, &mut |key| read(key))?; + let modules: Vec<(String, crate::ast::Program)> = + loaded.modules.into_iter().map(|m| (m.key, m.program)).collect(); + check::check_project(&loaded.main, &modules)?; + Ok(()) +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..6647757 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,50 @@ +use crate::ast::Span; + +/// A compiler diagnostic pointing at a place in the source. +#[derive(Debug, Clone, PartialEq)] +pub struct Diag { + pub msg: String, + pub span: Span, + /// The module (file) the span belongs to; `None` for the main file. + pub file: Option, +} + +impl Diag { + pub fn new(msg: impl Into, span: Span) -> Diag { + Diag { msg: msg.into(), span, file: None } + } + + /// Say which module the error is in (the main file has the empty name). + pub fn in_module(mut self, module: &str) -> Diag { + if self.file.is_none() && !module.is_empty() { + self.file = Some(module.to_string()); + } + self + } + + /// Render like: + /// ```text + /// error: expected `}`, found end of file + /// --> hello.dino:3:10 + /// | + /// 3 | func main() { + /// | ^ + /// ``` + pub fn render(&self, file: &str, src: &str) -> String { + let line_no = self.span.line as usize; + let line = src.lines().nth(line_no.saturating_sub(1)).unwrap_or(""); + let gutter = line_no.to_string().len(); + let pad = " ".repeat(gutter); + let caret_pad: String = line + .chars() + .take((self.span.col as usize).saturating_sub(1)) + .map(|c| if c == '\t' { '\t' } else { ' ' }) + .collect(); + format!( + "error: {}\n{pad}--> {}:{}:{}\n{pad} |\n{} | {}\n{pad} | {}^", + self.msg, file, self.span.line, self.span.col, line_no, line, caret_pad + ) + } +} + +pub type PResult = Result; diff --git a/src/fmt.rs b/src/fmt.rs new file mode 100644 index 0000000..f7713e5 --- /dev/null +++ b/src/fmt.rs @@ -0,0 +1,457 @@ +//! The code formatter behind `dino fmt`. +//! +//! It works on the text, not on the syntax tree, so comments and blank lines survive. It +//! never moves a line break: it fixes the indentation, the spaces between tokens and runs of +//! blank lines. Before handing back the result it lexes both versions and checks that they +//! hold exactly the same tokens, so a formatter bug can never change a program. + +use crate::error::Diag; +use crate::lexer::{self, StrPart, Tok}; + +const INDENT: &str = " "; + +#[derive(Clone, Copy, PartialEq, Debug)] +enum Kind { + Word, + Num, + Str, + Char, + Punct, + LineComment, + BlockComment, +} + +#[derive(Clone, Debug)] +struct Piece { + kind: Kind, + text: String, + /// How many line breaks come before this piece (0: same line). + newlines: usize, +} + +const KEYWORDS: &[&str] = &[ + "import", "struct", "enum", "interface", "func", "var", "const", "if", "else", "for", "in", "match", "return", + "break", "continue", "pub", "shared", "weak", "step", +]; + +/// Words that make a following `{` open a block (rather than a struct or map literal). +const BLOCK_WORDS: &[&str] = &["func", "if", "for", "match", "struct", "enum", "interface", "else"]; + +const BINARY: &[&str] = &[ + "+", "-", "*", "/", "%", "==", "!=", "<", ">", "<=", ">=", "&&", "||", "=", "+=", "-=", "*=", "/=", "%=", "=>", +]; + +/// Format a whole file. Fails (with the problem) if the file doesn't parse. +pub fn format(src: &str) -> Result { + crate::parser::parse(src)?; + let pieces = scan(src); + let out = print(&pieces); + // the safety net: the same tokens in, the same tokens out + let before = fingerprint(src)?; + let after = fingerprint(&out).map_err(|d| { + Diag::new(format!("the formatter produced code that doesn't lex ({}); this is a bug in `dino fmt`", d.msg), d.span) + })?; + if before != after { + return Err(Diag::new( + "the formatter would change the meaning of this file; this is a bug in `dino fmt`, nothing was written", + crate::ast::Span::default(), + )); + } + Ok(out) +} + +/// The tokens of `src`, without positions and without repeated line ends. +fn fingerprint(src: &str) -> Result, Diag> { + let mut out: Vec = Vec::new(); + for t in lexer::lex(src)? { + let s = match &t.tok { + Tok::Str(parts) => { + let mut s = String::from("str:"); + for p in parts { + match p { + StrPart::Text(t) => s.push_str(&format!("T{t:?}")), + StrPart::Code { src, .. } => s.push_str(&format!("C{src:?}")), + } + } + s + } + Tok::Newline if out.last().map_or(true, |l| l == "Newline") => continue, + other => format!("{other:?}"), + }; + out.push(s); + } + Ok(out) +} + +// ---------- reading the text ---------- + +fn string_end(c: &[char], mut i: usize) -> usize { + i += 1; // opening quote + loop { + match c.get(i) { + None => return i, + Some('\\') => i += 2, + Some('"') => return i + 1, + Some('{') => { + let mut depth = 1; + i += 1; + while depth > 0 { + match c.get(i) { + None => return i, + Some('"') => i = string_end(c, i), + Some('{') => { + depth += 1; + i += 1; + } + Some('}') => { + depth -= 1; + i += 1; + } + _ => i += 1, + } + } + } + _ => i += 1, + } + } +} + +fn scan(src: &str) -> Vec { + let c: Vec = src.chars().collect(); + let mut out: Vec = Vec::new(); + let mut i = 0; + let mut newlines = 0; + let take = |from: usize, to: usize| -> String { c[from..to.min(c.len())].iter().collect() }; + while i < c.len() { + let ch = c[i]; + if ch == '\n' { + newlines += 1; + i += 1; + continue; + } + if ch.is_whitespace() { + i += 1; + continue; + } + let start = i; + let kind; + if ch == '/' && c.get(i + 1) == Some(&'/') { + while i < c.len() && c[i] != '\n' { + i += 1; + } + kind = Kind::LineComment; + } else if ch == '/' && c.get(i + 1) == Some(&'*') { + i += 2; + while i < c.len() && !(c[i] == '*' && c.get(i + 1) == Some(&'/')) { + i += 1; + } + i = (i + 2).min(c.len()); + kind = Kind::BlockComment; + } else if ch == '"' { + i = string_end(&c, i); + kind = Kind::Str; + } else if ch == '\'' { + let len = if c.get(i + 1) == Some(&'\\') { 4 } else { 3 }; + if c.get(i + len - 1) == Some(&'\'') { + i += len; + kind = Kind::Char; + } else { + i += 1; + kind = Kind::Punct; + } + } else if ch.is_ascii_digit() { + while i < c.len() && (c[i].is_ascii_digit() || c[i] == '_') { + i += 1; + } + if c.get(i) == Some(&'.') && c.get(i + 1).map_or(false, |d| d.is_ascii_digit()) { + i += 1; + while i < c.len() && (c[i].is_ascii_digit() || c[i] == '_') { + i += 1; + } + } + kind = Kind::Num; + } else if ch.is_alphabetic() || ch == '_' { + while i < c.len() && (c[i].is_alphanumeric() || c[i] == '_') { + i += 1; + } + kind = Kind::Word; + } else { + let two: String = c[i..(i + 2).min(c.len())].iter().collect(); + if ["==", "!=", "<=", ">=", "&&", "||", "+=", "-=", "*=", "/=", "%=", "=>", ".."].contains(&two.as_str()) { + i += 2; + } else { + i += 1; + } + kind = Kind::Punct; + } + let mut text = take(start, i); + if kind == Kind::LineComment { + text = text.trim_end().to_string(); + } + out.push(Piece { kind, text, newlines }); + newlines = 0; + } + out +} + +// ---------- printing ---------- + +fn is_keyword(p: &Piece) -> bool { + p.kind == Kind::Word && KEYWORDS.contains(&p.text.as_str()) +} + +fn is_closer(p: &Piece) -> bool { + p.kind == Kind::Punct && matches!(p.text.as_str(), "}" | ")" | "]") +} + +fn is_binary(p: &Piece) -> bool { + p.kind == Kind::Punct && BINARY.contains(&p.text.as_str()) +} + +/// Does a value end with this piece? (Then a following `-` is a subtraction.) +fn ends_value(p: &Piece) -> bool { + match p.kind { + Kind::Num | Kind::Str | Kind::Char => true, + Kind::Word => !is_keyword(p) || matches!(p.text.as_str(), "true" | "false" | "nil"), + Kind::Punct => matches!(p.text.as_str(), ")" | "]" | "}" | "?"), + _ => false, + } +} + +/// For each `{`: does it open a block (true) or a struct / map literal (false)? +fn classify_braces(p: &[Piece]) -> Vec { + let mut out = vec![true; p.len()]; + // the groups open at this point; `true` for a block `{` + let mut open: Vec = Vec::new(); + for i in 0..p.len() { + let is_punct = p[i].kind == Kind::Punct; + if is_punct && matches!(p[i].text.as_str(), ")" | "]" | "}") { + open.pop(); + } + if is_punct && matches!(p[i].text.as_str(), "(" | "[") { + open.push(false); + } + if !(is_punct && p[i].text == "{") { + continue; + } + // a comma ends an argument or an entry, unless it is in a block (`for k, v in m {`) + let comma_stops = open.last().map_or(false, |b| !*b); + out[i] = 'decide: { + if i == 0 || p[i].newlines > 0 || p[i - 1].text == "=>" { + break 'decide true; + } + // look back over this line, at the same nesting level, for a word that starts a block + let mut depth = 0i32; + let mut j = i; + while j > 0 { + j -= 1; + let t = &p[j]; + if t.kind == Kind::Punct { + match t.text.as_str() { + ")" | "]" | "}" => depth += 1, + "(" | "[" | "{" => { + depth -= 1; + if depth < 0 { + break; + } + } + "," if depth == 0 && comma_stops => break, + _ => {} + } + } else if depth == 0 && t.kind == Kind::Word && BLOCK_WORDS.contains(&t.text.as_str()) { + break 'decide true; + } + if t.newlines > 0 { + break; // start of the line + } + } + false + }; + open.push(out[i]); + } + out +} + +struct Open { + line: usize, + /// For `{`: true if it opens a block. + block: bool, +} + +fn print(p: &[Piece]) -> String { + let braces = classify_braces(p); + let mut lines: Vec = Vec::new(); + let mut cur = String::new(); + let mut stack: Vec = Vec::new(); + let mut line_no = 0usize; + let mut cont = false; // the previous line ended with an operator + let mut unary_before = false; // the last piece was a prefix `-` / `!` + // lines that end with a `//` comment, and the byte where the comment starts + let mut comment_lines: std::collections::HashMap = std::collections::HashMap::new(); + + let distinct = |s: &[Open]| -> usize { + let mut n = 0; + let mut last = usize::MAX; + for o in s { + if o.line != last { + n += 1; + last = o.line; + } + } + n + }; + + for (i, t) in p.iter().enumerate() { + let prev = if i > 0 { Some(&p[i - 1]) } else { None }; + let line_start = i == 0 || t.newlines > 0; + if line_start { + if i > 0 { + lines.push(std::mem::take(&mut cur)); + line_no += 1; + cont = prev.map_or(false, |l| is_binary(l) && l.kind == Kind::Punct) && !unary_before; + } + // blank lines: at most one, none after an opening bracket or before a closing one + let after_open = prev.map_or(true, |l| l.kind == Kind::Punct && matches!(l.text.as_str(), "{" | "(" | "[")); + if t.newlines > 1 && !after_open && !is_closer(t) && i > 0 { + lines.push(String::new()); + line_no += 1; + } + // indentation: leading closers don't count the groups they close + let mut k = 0; + while i + k < p.len() && is_closer(&p[i + k]) && (k == 0 || p[i + k].newlines == 0) { + k += 1; + } + let remaining = stack.len().saturating_sub(k); + let mut level = distinct(&stack[..remaining]); + if cont && !is_closer(t) { + level += 1; + } + for _ in 0..level { + cur.push_str(INDENT); + } + } else if let Some(pr) = prev { + let mut space = gap(pr, t, if i > 1 { Some(&p[i - 2]) } else { None }, unary_before, &stack, braces[i]); + if t.kind == Kind::Punct && t.text == "}" { + space = match stack.last() { + Some(o) if o.block => !(pr.kind == Kind::Punct && pr.text == "{"), + _ => false, + }; + } + if space { + cur.push(' '); + } + } + + // is this `-` / `!` a prefix operator? + let is_prefix = t.kind == Kind::Punct + && (t.text == "!" || (t.text == "-" && (line_start || prev.map_or(true, |l| !ends_value(l))))); + unary_before = is_prefix; + + if t.kind == Kind::LineComment && !line_start { + comment_lines.insert(lines.len(), cur.len()); + } + cur.push_str(&t.text); + + if t.kind == Kind::Punct { + match t.text.as_str() { + "(" | "[" => stack.push(Open { line: line_no, block: false }), + "{" => stack.push(Open { line: line_no, block: braces[i] }), + ")" | "]" | "}" => { + stack.pop(); + } + _ => {} + } + } + } + lines.push(cur); + while lines.last().map_or(false, |l| l.is_empty()) { + lines.pop(); + } + align_comments(&mut lines, &comment_lines); + let mut out = lines.join("\n"); + out.push('\n'); + out +} + +/// Lines in a row that end with a `//` comment get their comments in one column. +/// `comment_lines` holds, for each line that has one, where its code ends. +fn align_comments(lines: &mut [String], comment_lines: &std::collections::HashMap) { + let mut i = 0; + while i < lines.len() { + if !comment_lines.contains_key(&i) { + i += 1; + continue; + } + let mut j = i; + while j < lines.len() && comment_lines.contains_key(&j) { + j += 1; + } + let width = (i..j).map(|k| lines[k][..comment_lines[&k]].trim_end().chars().count()).max().unwrap_or(0); + for k in i..j { + let at = comment_lines[&k]; + let code = lines[k][..at].trim_end().to_string(); + let comment = lines[k][at..].trim_start().to_string(); + let pad = width - code.chars().count() + 1; + lines[k] = format!("{code}{}{comment}", " ".repeat(pad)); + } + i = j; + } +} + +/// Is there a space between `prev` and `cur` (which are on the same line)? +fn gap(prev: &Piece, cur: &Piece, before_prev: Option<&Piece>, prev_is_prefix: bool, stack: &[Open], cur_block: bool) -> bool { + let (pt, ct) = (prev.text.as_str(), cur.text.as_str()); + let punct = |p: &Piece, s: &str| p.kind == Kind::Punct && p.text == s; + + if cur.kind == Kind::LineComment || cur.kind == Kind::BlockComment || prev.kind == Kind::BlockComment { + return true; + } + if prev_is_prefix { + return false; + } + if prev.kind == Kind::Punct { + match pt { + "(" | "[" | "." | ".." => return false, + "{" => { + // inside a block: `{ x }`; inside a literal: `{a: 1}` + return stack.last().map_or(true, |o| o.block) && !punct(cur, "}"); + } + "," | ";" | ":" => return true, + _ => {} + } + } + if cur.kind == Kind::Punct { + match ct { + ")" | "]" | "," | ";" | "?" | ":" | "." | ".." => return false, + "{" => { + if cur_block { + return true; + } + // a literal: `Point{...}`, `map[string, int]{}`, but `= {...}` + let attached = (prev.kind == Kind::Word && prev.text.chars().next().map_or(false, |c| c.is_uppercase())) + || punct(prev, "]"); + return !attached; + } + "(" => { + return match prev.kind { + Kind::Word => is_keyword(prev) && prev.text != "func", + Kind::Punct => !matches!(pt, ")" | "]" | "}"), + _ => true, + }; + } + "[" => { + return match prev.kind { + Kind::Word => is_keyword(prev), + Kind::Punct => !matches!(pt, ")" | "]"), + _ => true, + }; + } + _ => {} + } + } + // `[]int`, `[][]int`: no space after an empty `[]` + if punct(prev, "]") && before_prev.map_or(false, |b| punct(b, "[")) && (cur.kind == Kind::Word || punct(cur, "[")) { + return false; + } + true +} diff --git a/src/hir.rs b/src/hir.rs new file mode 100644 index 0000000..e38a127 --- /dev/null +++ b/src/hir.rs @@ -0,0 +1,534 @@ +//! The typed tree produced by the checker. Every name is resolved and every +//! expression has a type. Code generation (and later, ownership analysis) +//! works on this instead of the raw syntax tree. + +use crate::ast::{BinOp, Span, UnOp}; +use crate::types::Ty; + +/// How a built-in function from the runtime is called from the generated C. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum BuiltinShape { + /// `f(args)` returns the value. + Plain, + /// Returns `(T, error)`: the C function returns the error and fills in `T` through a pointer. + ErrOut, + /// Returns `T?`: the C function returns whether there is a value and fills in `T` through a pointer. + OptOut, + /// `math.min`, `math.max`, `math.abs`: written out in C for the type of the arguments. + Min, + Max, + Abs, + /// `s.chars()`: the C function fills in the parts of a new list of characters. + ListOut, +} + +#[derive(Debug, Clone)] +pub struct HProgram { + /// Structs and enums in dependency order: a type comes after the types it contains by value. + pub types: Vec, + pub funcs: Vec, + /// Every interface: its table name and its methods (name, parameters, result). + pub ifaces: Vec<(String, Vec<(String, Vec, Ty)>)>, +} + +#[derive(Debug, Clone)] +pub enum HTypeDef { + Struct(HStruct), + Enum(HEnum), +} + +impl HTypeDef { + pub fn name(&self) -> &str { + match self { + HTypeDef::Struct(s) => &s.name, + HTypeDef::Enum(e) => &e.name, + } + } +} + +#[derive(Debug, Clone)] +pub struct HEnum { + pub name: String, + pub variants: Vec, +} + +#[derive(Debug, Clone)] +pub struct HVariant { + pub name: String, + pub fields: Vec<(String, Ty)>, +} + +#[derive(Debug, Clone)] +pub struct HArm { + pub pat: HPat, + pub body: HArmBody, +} + +#[derive(Debug, Clone)] +pub enum HPat { + /// A case of an enum; `binds[i]` names the i-th piece of data, if the arm uses it. + Variant { index: usize, binds: Vec> }, + Int(i64), + Bool(bool), + Str(String), + /// `_`, or a name that takes the whole value. + CatchAll(Option), +} + +#[derive(Debug, Clone)] +pub enum HArmBody { + Expr(HExpr), + Block(Vec), +} + +#[derive(Debug, Clone)] +pub struct HStruct { + pub name: String, + pub fields: Vec<(String, Ty)>, +} + +#[derive(Debug, Clone)] +pub struct HFunc { + pub name: String, + /// `Some(struct name)` for methods. + pub owner: Option, + /// The implicit `self` of a method. It is passed by reference. + pub self_param: Option, + /// True if the method changes `self` (inferred from its body). + pub mutates: bool, + pub params: Vec, + pub ret: Ty, + pub body: Vec, +} + +#[derive(Debug, Clone)] +pub struct HLocal { + /// Unique within a function. + pub id: u32, + pub name: String, + pub ty: Ty, + pub is_const: bool, +} + +#[derive(Debug, Clone)] +pub enum HStmt { + Let { local: HLocal, init: HExpr }, + /// `var a, b = f()`: `init` has a tuple type with one part per local. + LetMany { locals: Vec, init: HExpr }, + /// `target` is a place: a local, or a chain of fields starting at a local. + Assign { target: HExpr, value: HExpr }, + Expr(HExpr), + If { cond: HExpr, then: Vec, else_: Vec }, + /// `for { }` + Loop(Vec), + /// `for cond { }` + While { cond: HExpr, body: Vec }, + /// `for i in start..end step s { }` + ForRange { var: HLocal, start: HExpr, end: HExpr, step: Option, body: Vec }, + /// `for x in list { }` / `for i, x in list { }` + ForEach { index: Option, var: HLocal, iter: HExpr, body: Vec }, + Return(Option), + Break, + Continue, + Block(Vec), +} + +/// What a method of a map does. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum MapOp { + /// `m.has(k)`: bool + Has, + /// `m.get(k)`: the value, or nil + Get, + /// `m.get_or(k, d)`: the value, or `d` + GetOr, + /// `m.remove(k)` + Remove, + /// `m.keys()` + Keys, + /// `m.values()` + Values, + /// Reading `m[k]` for `m[k] += 1`: a missing key counts as the zero value + Zero, +} + +#[derive(Debug, Clone)] +pub struct HExpr { + pub kind: HExprKind, + pub ty: Ty, +} + +#[derive(Debug, Clone)] +pub enum HInterp { + Text(String), + Value(HExpr), +} + +#[derive(Debug, Clone)] +pub enum HExprKind { + Int(i64), + Float(f64), + Bool(bool), + Str(String), + /// Reference to a local variable by id. + Local(u32, String), + Unary { op: UnOp, expr: Box }, + Binary { op: BinOp, lhs: Box, rhs: Box }, + /// Call of a user function. + Call { func: String, args: Vec }, + /// Call of a method. + MethodCall { owner: String, method: String, recv: Box, args: Vec, span: Span }, + /// The built-in `print`. + Print(Vec), + /// `int(x)` / `float(x)` between different types; the target is the expression's `ty`. + Convert(Box), + /// A string with `{...}` parts. + Interp(Vec), + /// `base.name` + Field { base: Box, name: String }, + /// `Shape.Circle(2.0)`: the enum, the index of the case, and its data. + EnumLit { enum_name: String, variant: usize, fields: Vec }, + /// `match`: exhaustive; arms are tried in order. + Match { scrutinee: Box, arms: Vec }, + /// `[a, b, c]` + ListLit { elems: Vec }, + /// `list[i]`, a place when `list` is one + Index { base: Box, index: Box }, + /// `list[lo..hi]`: a new list holding a copy of that part + Slice { base: Box, lo: Option>, hi: Option> }, + /// `list.push(v)`; `list` is a place + ListPush { list: Box, value: Box }, + /// `list.pop()`; `list` is a place + ListPop(Box), + /// `list.len()`, `string.len()` or `map.len()` + Len(Box), + /// `{"a": 1}`: every key and value, in the order written + MapLit { entries: Vec<(HExpr, HExpr)> }, + /// A method of a map (`m.has(k)`, `m.get(k)`, ...); `map` is a place for the ones that change it + MapCall { map: Box, op: MapOp, args: Vec }, + /// A deep copy of a place that is being given to a new owner (`var b = a`). + Copy(Box), + /// Like `Copy`, but the variable is never used again, so its value is handed over + /// and the variable is left empty (no deep copy). Added by `moves`. + Move(Box), + /// `shared v`: move `v` into a new shared box. + NewShared(Box), + /// `weak x`: a weak pointer to the shared value `x` (which is only read). + Downgrade(Box), + /// `w.upgrade()`: the shared value a weak pointer points at, or nil if it is gone. + Upgrade(Box), + /// The value inside a `shared T` / `weak T` (a place; panics if empty or gone). + Deref(Box), + /// A function value. The captured variables are copied into it when it is made + /// (`captures`: the closure's own variable, and the value copied from outside). + Closure { params: Vec, ret: Ty, body: Vec, captures: Vec<(HLocal, HExpr)> }, + /// A struct or enum value turned into an interface value (it is moved into a box). + ToIface(Box), + /// `s.area()` where `s` has an interface type. + IfaceCall { recv: Box, method: String, args: Vec }, + /// Calling a function value. + CallValue { callee: Box, args: Vec }, + /// A call of a built-in (`os.read_file(p)`, `s.trim()`, `math.sqrt(x)`). The arguments are only + /// borrowed; the result is a new value. + Builtin { c: &'static str, shape: BuiltinShape, args: Vec }, + /// `nil`; the type is an optional or `error`. + Nil, + /// A `T` used where a `T?` is expected. + Wrap(Box), + /// `error("message")` + MakeError(Box), + /// `x != nil` for an optional or an error. + IsSet(Box), + /// The value inside a `T?` (panics if it is nil). The checker adds it where it knows `x != nil`. + Unwrap(Box), + /// `return a, b` + TupleLit(Vec), + /// `call()?`: the call returns `(T, error)` or `error`; on an error the function returns it. + Try(Box), + /// A struct value. Every field appears exactly once; initializers are listed in the + /// order they were written (so they run in that order), omitted fields last. + StructLit { name: String, fields: Vec<(String, HExpr)> }, +} + +/// The local variable at the start of a place like `a.b.c`, if it is one. +pub fn place_root(e: &HExpr) -> Option<(u32, &str)> { + match &e.kind { + HExprKind::Local(id, name) => Some((*id, name)), + HExprKind::Field { base, .. } + | HExprKind::Index { base, .. } + | HExprKind::Unwrap(base) + | HExprKind::Deref(base) => place_root(base), + _ => None, + } +} + +/// Visit `e` and every expression inside it, outer first. Statements inside +/// expressions (the blocks of `match` arms) are visited too. +pub fn walk_expr(e: &HExpr, on_stmt: &mut dyn FnMut(&HStmt), on_expr: &mut dyn FnMut(&HExpr)) { + on_expr(e); + match &e.kind { + HExprKind::Int(_) + | HExprKind::Float(_) + | HExprKind::Bool(_) + | HExprKind::Str(_) + | HExprKind::Local(..) + | HExprKind::Nil => {} + HExprKind::Wrap(x) + | HExprKind::MakeError(x) + | HExprKind::IsSet(x) + | HExprKind::NewShared(x) + | HExprKind::ToIface(x) + | HExprKind::Downgrade(x) + | HExprKind::Upgrade(x) + | HExprKind::Deref(x) + | HExprKind::Unwrap(x) + | HExprKind::Try(x) => walk_expr(x, on_stmt, on_expr), + HExprKind::TupleLit(items) => items.iter().for_each(|a| walk_expr(a, on_stmt, on_expr)), + HExprKind::Unary { expr, .. } | HExprKind::Convert(expr) => walk_expr(expr, on_stmt, on_expr), + HExprKind::Binary { lhs, rhs, .. } => { + walk_expr(lhs, on_stmt, on_expr); + walk_expr(rhs, on_stmt, on_expr); + } + HExprKind::Call { args, .. } | HExprKind::Print(args) | HExprKind::Builtin { args, .. } => { + args.iter().for_each(|a| walk_expr(a, on_stmt, on_expr)) + } + HExprKind::CallValue { callee, args } => { + walk_expr(callee, on_stmt, on_expr); + args.iter().for_each(|a| walk_expr(a, on_stmt, on_expr)); + } + HExprKind::IfaceCall { recv, args, .. } => { + walk_expr(recv, on_stmt, on_expr); + args.iter().for_each(|a| walk_expr(a, on_stmt, on_expr)); + } + HExprKind::Closure { body, captures, .. } => { + captures.iter().for_each(|(_, a)| walk_expr(a, on_stmt, on_expr)); + walk_stmts(body, on_stmt, on_expr); + } + HExprKind::MethodCall { recv, args, .. } => { + walk_expr(recv, on_stmt, on_expr); + args.iter().for_each(|a| walk_expr(a, on_stmt, on_expr)); + } + HExprKind::Interp(parts) => { + for p in parts { + if let HInterp::Value(v) = p { + walk_expr(v, on_stmt, on_expr); + } + } + } + HExprKind::Field { base, .. } => walk_expr(base, on_stmt, on_expr), + HExprKind::StructLit { fields, .. } => fields.iter().for_each(|(_, a)| walk_expr(a, on_stmt, on_expr)), + HExprKind::EnumLit { fields, .. } => fields.iter().for_each(|a| walk_expr(a, on_stmt, on_expr)), + HExprKind::Match { scrutinee, arms } => { + walk_expr(scrutinee, on_stmt, on_expr); + for arm in arms { + match &arm.body { + HArmBody::Expr(x) => walk_expr(x, on_stmt, on_expr), + HArmBody::Block(b) => walk_stmts(b, on_stmt, on_expr), + } + } + } + HExprKind::ListLit { elems } => elems.iter().for_each(|a| walk_expr(a, on_stmt, on_expr)), + HExprKind::MapLit { entries } => entries.iter().for_each(|(k, v)| { + walk_expr(k, on_stmt, on_expr); + walk_expr(v, on_stmt, on_expr); + }), + HExprKind::MapCall { map, args, .. } => { + walk_expr(map, on_stmt, on_expr); + args.iter().for_each(|a| walk_expr(a, on_stmt, on_expr)); + } + HExprKind::Index { base, index } => { + walk_expr(base, on_stmt, on_expr); + walk_expr(index, on_stmt, on_expr); + } + HExprKind::Slice { base, lo, hi } => { + walk_expr(base, on_stmt, on_expr); + if let Some(l) = lo { + walk_expr(l, on_stmt, on_expr); + } + if let Some(h) = hi { + walk_expr(h, on_stmt, on_expr); + } + } + HExprKind::ListPush { list, value } => { + walk_expr(list, on_stmt, on_expr); + walk_expr(value, on_stmt, on_expr); + } + HExprKind::ListPop(x) | HExprKind::Len(x) | HExprKind::Copy(x) | HExprKind::Move(x) => walk_expr(x, on_stmt, on_expr), + } +} + +/// Visit every statement (including nested ones) and every expression in them. +pub fn walk_stmts(stmts: &[HStmt], on_stmt: &mut dyn FnMut(&HStmt), on_expr: &mut dyn FnMut(&HExpr)) { + for s in stmts { + on_stmt(s); + match s { + HStmt::Let { init, .. } | HStmt::LetMany { init, .. } => walk_expr(init, on_stmt, on_expr), + HStmt::Assign { target, value } => { + walk_expr(target, on_stmt, on_expr); + walk_expr(value, on_stmt, on_expr); + } + HStmt::Expr(e) => walk_expr(e, on_stmt, on_expr), + HStmt::If { cond, then, else_ } => { + walk_expr(cond, on_stmt, on_expr); + walk_stmts(then, on_stmt, on_expr); + walk_stmts(else_, on_stmt, on_expr); + } + HStmt::Loop(b) | HStmt::Block(b) => walk_stmts(b, on_stmt, on_expr), + HStmt::While { cond, body } => { + walk_expr(cond, on_stmt, on_expr); + walk_stmts(body, on_stmt, on_expr); + } + HStmt::ForRange { start, end, step, body, .. } => { + walk_expr(start, on_stmt, on_expr); + walk_expr(end, on_stmt, on_expr); + if let Some(st) = step { + walk_expr(st, on_stmt, on_expr); + } + walk_stmts(body, on_stmt, on_expr); + } + HStmt::ForEach { iter, body, .. } => { + walk_expr(iter, on_stmt, on_expr); + walk_stmts(body, on_stmt, on_expr); + } + HStmt::Return(Some(e)) => walk_expr(e, on_stmt, on_expr), + HStmt::Return(None) | HStmt::Break | HStmt::Continue => {} + } + } +} + +/// Does anything in `stmts` change the variable `id` (or a part of it)? +/// `is_mut_method(owner, method)` says whether a method changes its receiver. +pub fn mutates_root(stmts: &[HStmt], id: u32, is_mut_method: &dyn Fn(&str, &str) -> bool) -> bool { + let mut found = false; + walk_stmts( + stmts, + &mut |s| { + if let HStmt::Assign { target, .. } = s { + if place_root(target).map(|(i, _)| i) == Some(id) { + found = true; + } + } + }, + &mut |_| {}, + ); + walk_stmts( + stmts, + &mut |_| {}, + &mut |e| { + if expr_mutates_root(e, id, is_mut_method) { + found = true; + } + }, + ); + found +} + +/// Is `e` itself (not its children) an operation that changes the variable `id`? +pub fn expr_mutates_root(e: &HExpr, id: u32, is_mut_method: &dyn Fn(&str, &str) -> bool) -> bool { + match &e.kind { + HExprKind::ListPush { list, .. } | HExprKind::ListPop(list) => { + place_root(list).map(|(i, _)| i) == Some(id) + } + HExprKind::MapCall { map, op: MapOp::Remove, .. } => place_root(map).map(|(i, _)| i) == Some(id), + // calling a function value may change what it captured + HExprKind::CallValue { callee, .. } => place_root(callee).map(|(i, _)| i) == Some(id), + // a method called through an interface may change the value inside + HExprKind::IfaceCall { recv, .. } => place_root(recv).map(|(i, _)| i) == Some(id), + HExprKind::MethodCall { owner, method, recv, .. } => { + is_mut_method(owner, method) && place_root(recv).map(|(i, _)| i) == Some(id) + } + _ => false, + } +} + +/// Visit the direct children of `e` mutably (not the statements of `match` arms). +pub fn children_mut(e: &mut HExpr, f: &mut dyn FnMut(&mut HExpr)) { + match &mut e.kind { + HExprKind::Int(_) + | HExprKind::Float(_) + | HExprKind::Bool(_) + | HExprKind::Str(_) + | HExprKind::Local(..) + | HExprKind::Nil => {} + HExprKind::Wrap(x) + | HExprKind::MakeError(x) + | HExprKind::IsSet(x) + | HExprKind::NewShared(x) + | HExprKind::ToIface(x) + | HExprKind::Downgrade(x) + | HExprKind::Upgrade(x) + | HExprKind::Deref(x) + | HExprKind::Unwrap(x) + | HExprKind::Try(x) + | HExprKind::ListPop(x) + | HExprKind::Len(x) + | HExprKind::Copy(x) + | HExprKind::Move(x) => f(x), + HExprKind::Unary { expr, .. } | HExprKind::Convert(expr) => f(expr), + HExprKind::TupleLit(items) => items.iter_mut().for_each(|a| f(a)), + HExprKind::Binary { lhs, rhs, .. } => { + f(lhs); + f(rhs); + } + HExprKind::Call { args, .. } | HExprKind::Print(args) | HExprKind::Builtin { args, .. } => { + args.iter_mut().for_each(|a| f(a)) + } + HExprKind::CallValue { callee, args } => { + f(callee); + args.iter_mut().for_each(|a| f(a)); + } + HExprKind::IfaceCall { recv, args, .. } => { + f(recv); + args.iter_mut().for_each(|a| f(a)); + } + HExprKind::Closure { captures, .. } => captures.iter_mut().for_each(|(_, a)| f(a)), + HExprKind::MethodCall { recv, args, .. } => { + f(recv); + args.iter_mut().for_each(|a| f(a)); + } + HExprKind::Interp(parts) => { + for p in parts { + if let HInterp::Value(v) = p { + f(v); + } + } + } + HExprKind::Field { base, .. } => f(base), + HExprKind::StructLit { fields, .. } => fields.iter_mut().for_each(|(_, a)| f(a)), + HExprKind::EnumLit { fields, .. } => fields.iter_mut().for_each(|a| f(a)), + HExprKind::Match { scrutinee, arms } => { + f(scrutinee); + for arm in arms { + if let HArmBody::Expr(x) = &mut arm.body { + f(x); + } + } + } + HExprKind::ListLit { elems } => elems.iter_mut().for_each(|a| f(a)), + HExprKind::MapLit { entries } => entries.iter_mut().for_each(|(k, v)| { + f(k); + f(v); + }), + HExprKind::MapCall { map, args, .. } => { + f(map); + args.iter_mut().for_each(|a| f(a)); + } + HExprKind::Index { base, index } => { + f(base); + f(index); + } + HExprKind::Slice { base, lo, hi } => { + f(base); + if let Some(l) = lo { + f(l); + } + if let Some(h) = hi { + f(h); + } + } + HExprKind::ListPush { list, value } => { + f(list); + f(value); + } + } +} diff --git a/src/index.rs b/src/index.rs new file mode 100644 index 0000000..f5735ce --- /dev/null +++ b/src/index.rs @@ -0,0 +1,398 @@ +//! A quick map of a Dino file for editors: what it declares, what it imports, and which names +//! are visible at a point. It reads the token stream only (not the syntax tree), so it keeps +//! working while the code is half typed and has errors in it. + +use crate::lexer::{self, Tok, Token}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Kind { + Func, + Method, + Struct, + Enum, + Interface, + Const, + Field, + Variant, + /// A local variable, parameter or loop variable. + Local, +} + +#[derive(Debug, Clone)] +pub struct Sym { + pub name: String, + pub kind: Kind, + /// 0-based line and column (in characters) of the name. + pub line: usize, + pub col: usize, + /// The last line of the whole declaration (its closing `}` for things with a body). + pub end_line: usize, + /// The type a method, field or variant belongs to. + pub container: Option, + pub is_pub: bool, +} + +#[derive(Debug, Clone)] +pub struct Import { + /// The name the module is used by (`utils`, or `fs` for `tools.fs`). + pub name: String, + /// The path as written (`utils`, `tools.fs`); empty for `import c "x.h"`. + pub path: String, +} + +#[derive(Debug, Clone, Default)] +pub struct Index { + pub syms: Vec, + pub imports: Vec, + toks: Vec, +} + +fn pos(t: &Token) -> (usize, usize) { + (t.span.line as usize - 1, t.span.col as usize - 1) +} + +/// Index of the `}` that closes the `{` at `open`. +fn matching(toks: &[Token], open: usize) -> Option { + let mut depth = 0; + for (i, t) in toks.iter().enumerate().skip(open) { + match t.tok { + Tok::LBrace => depth += 1, + Tok::RBrace => { + depth -= 1; + if depth == 0 { + return Some(i); + } + } + _ => {} + } + } + None +} + +impl Index { + pub fn new(src: &str) -> Option { + let toks = lexer::lex(src).ok()?; + let mut ix = Index { syms: Vec::new(), imports: Vec::new(), toks }; + ix.scan_top(); + Some(ix) + } + + fn ident(&self, i: usize) -> Option<&str> { + match &self.toks.get(i)?.tok { + Tok::Ident(s) => Some(s), + _ => None, + } + } + + fn scan_top(&mut self) { + let toks = self.toks.clone(); + let mut i = 0; + while i < toks.len() { + let mut j = i; + let mut is_pub = false; + if toks[j].tok == Tok::Pub { + is_pub = true; + j += 1; + } + let Some(head) = toks.get(j) else { break }; + match head.tok { + Tok::Import => { + // import a.b.c | import c "header.h" + let mut k = j + 1; + let mut parts: Vec = Vec::new(); + while let Some(Tok::Ident(s)) = toks.get(k).map(|t| &t.tok) { + parts.push(s.clone()); + k += 1; + if toks.get(k).map(|t| &t.tok) == Some(&Tok::Dot) { + k += 1; + } else { + break; + } + } + if let Some(last) = parts.last() { + let aliased = matches!(toks.get(k).map(|t| &t.tok), Some(Tok::Str(_))); + self.imports.push(Import { name: last.clone(), path: if aliased { String::new() } else { parts.join(".") } }); + } + i = k.max(i + 1); + } + Tok::Func => { + if let Some(name) = self.ident(j + 1).map(str::to_string) { + let (line, col) = pos(&toks[j + 1]); + let end = self.decl_end(j + 1); + self.syms.push(Sym { name, kind: Kind::Func, line, col, end_line: end, container: None, is_pub }); + } + i = self.skip_decl(j + 1); + } + Tok::Const => { + if let Some(name) = self.ident(j + 1).map(str::to_string) { + let (line, col) = pos(&toks[j + 1]); + self.syms.push(Sym { name, kind: Kind::Const, line, col, end_line: line, container: None, is_pub }); + } + i = j + 2; + } + Tok::Struct | Tok::Enum | Tok::Interface => { + let kind = match head.tok { + Tok::Struct => Kind::Struct, + Tok::Enum => Kind::Enum, + _ => Kind::Interface, + }; + let Some(name) = self.ident(j + 1).map(str::to_string) else { + i = j + 1; + continue; + }; + let (line, col) = pos(&toks[j + 1]); + // the body + let open = (j + 1..toks.len()).find(|&k| toks[k].tok == Tok::LBrace); + let close = open.and_then(|o| matching(&toks, o)); + let end_line = close.map_or(line, |c| pos(&toks[c]).0); + self.syms.push(Sym { name: name.clone(), kind, line, col, end_line, container: None, is_pub }); + if let (Some(o), Some(c)) = (open, close) { + self.scan_body(&name, kind, o + 1, c); + i = c + 1; + } else { + i = j + 2; + } + } + _ => i = j + 1, + } + } + } + + /// The members of a type body (tokens `from..to`, at its top level). + fn scan_body(&mut self, owner: &str, kind: Kind, from: usize, to: usize) { + let toks = self.toks.clone(); + let mut k = from; + let mut line_start = true; + let mut depth = 0i32; + while k < to { + let t = &toks[k]; + match t.tok { + Tok::LBrace | Tok::LParen | Tok::LBracket => depth += 1, + Tok::RBrace | Tok::RParen | Tok::RBracket => depth -= 1, + _ => {} + } + if depth != 0 && !matches!(t.tok, Tok::LBrace | Tok::LParen | Tok::LBracket) { + line_start = false; + k += 1; + continue; + } + if depth > 0 { + k += 1; + line_start = false; + continue; + } + if t.tok == Tok::Newline { + line_start = true; + k += 1; + continue; + } + let mut m = k; + let mut is_pub = false; + if toks[m].tok == Tok::Pub { + is_pub = true; + m += 1; + } + if toks[m.min(to - 1)].tok == Tok::Func && m + 1 < to { + if let Some(name) = self.ident(m + 1).map(str::to_string) { + let (line, col) = pos(&toks[m + 1]); + let end = self.decl_end(m + 1); + self.syms.push(Sym { name, kind: Kind::Method, line, col, end_line: end, container: Some(owner.to_string()), is_pub }); + } + // skip to the end of the method (its body) + let after = self.skip_decl(m + 1); + k = after.max(k + 1); + line_start = true; + depth = 0; + continue; + } + if line_start { + // `a, b: float` (fields), `Circle(r: float)` (a variant), `area(): float` (an interface method) + let mut names: Vec<(String, (usize, usize))> = Vec::new(); + let mut n = m; + while let Some(name) = self.ident(n) { + names.push((name.to_string(), pos(&toks[n]))); + if toks.get(n + 1).map(|t| &t.tok) == Some(&Tok::Comma) { + n += 2; + } else { + n += 1; + break; + } + } + let next = toks.get(n).map(|t| &t.tok); + let member_kind = match (kind, next) { + (Kind::Struct, Some(Tok::Colon)) => Some(Kind::Field), + (Kind::Enum, _) if !names.is_empty() => Some(Kind::Variant), + (Kind::Interface, Some(Tok::LParen)) => Some(Kind::Method), + _ => None, + }; + if let Some(mk) = member_kind { + for (name, (line, col)) in names { + self.syms.push(Sym { name, kind: mk, line, col, end_line: line, container: Some(owner.to_string()), is_pub }); + } + } + } + line_start = false; + k += 1; + } + } + + /// The last line of the declaration whose name is token `name_at` (through its body, if any). + fn decl_end(&self, name_at: usize) -> usize { + let end = self.skip_decl(name_at); + pos(&self.toks[end.saturating_sub(1).max(name_at)]).0 + } + + /// The token after the declaration whose name is token `name_at`: past its `{ ... }`. + fn skip_decl(&self, name_at: usize) -> usize { + let mut paren = 0i32; + for k in name_at..self.toks.len() { + match self.toks[k].tok { + Tok::LParen | Tok::LBracket => paren += 1, + Tok::RParen | Tok::RBracket => paren -= 1, + Tok::LBrace if paren == 0 => return matching(&self.toks, k).map_or(self.toks.len(), |c| c + 1), + Tok::Newline if paren == 0 => return k, // a signature without a body (interfaces) + _ => {} + } + } + self.toks.len() + } + + /// Names declared inside the function around `line`, before `(line, col)`: variables, parameters, + /// loop variables, `x =>` parameters and the names a `match` case binds. + pub fn locals_at(&self, line: usize, col: usize) -> Vec { + let Some(f) = self.enclosing_fn(line) else { return Vec::new() }; + let toks = &self.toks; + let mut out: Vec = Vec::new(); + let mut add = |t: &Token, name: &str, f: &Sym| { + let (l, c) = pos(t); + if name != "_" { + out.push(Sym { name: name.to_string(), kind: Kind::Local, line: l, col: c, end_line: l, container: f.container.clone(), is_pub: false }); + } + }; + let start = toks.iter().position(|t| pos(t) >= (f.line, f.col)).unwrap_or(0); + let mut k = start; + while k < toks.len() && pos(&toks[k]) < (line, col) { + match &toks[k].tok { + Tok::Var | Tok::Const => { + let mut n = k + 1; + while let Some(name) = self.ident(n) { + add(&toks[n], name, &f); + if toks.get(n + 1).map(|t| &t.tok) == Some(&Tok::Comma) { + n += 2; + } else { + break; + } + } + } + Tok::For => { + let mut n = k + 1; + while let Some(name) = self.ident(n) { + add(&toks[n], name, &f); + if toks.get(n + 1).map(|t| &t.tok) == Some(&Tok::Comma) { + n += 2; + } else { + break; + } + } + } + Tok::LParen => { + // `(a, b: int)` after `func name` or `func`: parameters. `(a, b) =>`: lambda parameters. + // `Circle(r) =>`: names bound by a match case. + let close = (k + 1..toks.len()).find(|&n| { + let mut d = 0; + for m in k..=n { + match toks[m].tok { + Tok::LParen => d += 1, + Tok::RParen => d -= 1, + _ => {} + } + } + d == 0 + }); + if let Some(c) = close { + let after_func = k >= 1 && (toks[k - 1].tok == Tok::Func || (k >= 2 && toks[k - 2].tok == Tok::Func)); + let arrow = toks.get(c + 1).map(|t| &t.tok) == Some(&Tok::FatArrow); + if after_func { + // `a, b: int, c: string`: a name belongs to the next `:` after it + let mut pending: Vec = Vec::new(); + let mut depth = 0; + let mut seg_first: Option = None; + for n in k..=c { + match &toks[n].tok { + Tok::LParen | Tok::LBracket => depth += 1, + Tok::RParen | Tok::RBracket => depth -= 1, + _ => {} + } + if depth == 1 && matches!(toks[n].tok, Tok::LParen | Tok::Comma) { + seg_first = Some(n + 1); + } else if depth == 1 && Some(n) == seg_first { + if self.ident(n).is_some() { + if toks.get(n + 1).map(|t| &t.tok) == Some(&Tok::Colon) { + for &p in pending.iter().chain(std::iter::once(&n)) { + if let Some(name) = self.ident(p) { + add(&toks[p], name, &f); + } + } + pending.clear(); + } else { + pending.push(n); + } + } + } + } + } else if arrow { + // `(a, b) =>` and `Circle(r) =>` + let mut depth = 0; + for n in k..c { + match &toks[n].tok { + Tok::LParen | Tok::LBracket => depth += 1, + Tok::RParen | Tok::RBracket => depth -= 1, + Tok::Ident(name) if depth == 1 && matches!(toks[n - 1].tok, Tok::LParen | Tok::Comma) => { + add(&toks[n], name, &f) + } + _ => {} + } + } + } + } + } + Tok::Ident(name) if toks.get(k + 1).map(|t| &t.tok) == Some(&Tok::FatArrow) => add(&toks[k], name, &f), + _ => {} + } + k += 1; + } + out + } + + /// The function or method whose lines hold `line` (the innermost). + pub fn enclosing_fn(&self, line: usize) -> Option { + self.syms + .iter() + .filter(|s| matches!(s.kind, Kind::Func | Kind::Method) && s.line <= line && line <= s.end_line) + .last() + .cloned() + } + + /// The type whose body holds `line`. + pub fn enclosing_type(&self, line: usize) -> Option { + self.syms + .iter() + .filter(|s| matches!(s.kind, Kind::Struct | Kind::Enum | Kind::Interface) && s.line <= line && line <= s.end_line) + .last() + .cloned() + } + + pub fn module(&self, name: &str) -> Option<&Import> { + self.imports.iter().find(|i| i.name == name) + } + + /// The top-level function, type or constant called `name`. + pub fn top(&self, name: &str) -> Option<&Sym> { + self.syms.iter().find(|s| s.name == name && s.container.is_none()) + } + + /// Members (fields, methods, variants) called `name`, of any type. + pub fn members(&self, name: &str) -> Vec<&Sym> { + self.syms.iter().filter(|s| s.name == name && s.container.is_some()).collect() + } +} diff --git a/src/json.rs b/src/json.rs new file mode 100644 index 0000000..b017d73 --- /dev/null +++ b/src/json.rs @@ -0,0 +1,264 @@ +//! A tiny JSON reader and writer, just enough for the language server. + +use std::collections::BTreeMap; + +#[derive(Debug, Clone, PartialEq)] +pub enum Json { + Null, + Bool(bool), + Num(f64), + Str(String), + Arr(Vec), + Obj(BTreeMap), +} + +impl Json { + pub fn obj(fields: Vec<(&str, Json)>) -> Json { + Json::Obj(fields.into_iter().map(|(k, v)| (k.to_string(), v)).collect()) + } + + pub fn str(s: impl Into) -> Json { + Json::Str(s.into()) + } + + pub fn get(&self, key: &str) -> Option<&Json> { + match self { + Json::Obj(m) => m.get(key), + _ => None, + } + } + + pub fn as_str(&self) -> Option<&str> { + match self { + Json::Str(s) => Some(s), + _ => None, + } + } + + pub fn as_i64(&self) -> Option { + match self { + Json::Num(n) => Some(*n as i64), + _ => None, + } + } + + /// `a.b.c` style lookup through objects. + pub fn path(&self, path: &[&str]) -> Option<&Json> { + let mut cur = self; + for p in path { + cur = cur.get(p)?; + } + Some(cur) + } + + pub fn to_string(&self) -> String { + let mut out = String::new(); + self.write(&mut out); + out + } + + fn write(&self, out: &mut String) { + match self { + Json::Null => out.push_str("null"), + Json::Bool(b) => out.push_str(if *b { "true" } else { "false" }), + Json::Num(n) => { + if n.fract() == 0.0 && n.abs() < 1e15 { + out.push_str(&format!("{}", *n as i64)); + } else { + out.push_str(&n.to_string()); + } + } + Json::Str(s) => write_str(s, out), + Json::Arr(items) => { + out.push('['); + for (i, x) in items.iter().enumerate() { + if i > 0 { + out.push(','); + } + x.write(out); + } + out.push(']'); + } + Json::Obj(m) => { + out.push('{'); + for (i, (k, v)) in m.iter().enumerate() { + if i > 0 { + out.push(','); + } + write_str(k, out); + out.push(':'); + v.write(out); + } + out.push('}'); + } + } + } +} + +fn write_str(s: &str, out: &mut String) { + out.push('"'); + for c in s.chars() { + match c { + '"' => out.push_str("\\\""), + '\\' => out.push_str("\\\\"), + '\n' => out.push_str("\\n"), + '\r' => out.push_str("\\r"), + '\t' => out.push_str("\\t"), + c if (c as u32) < 0x20 => out.push_str(&format!("\\u{:04x}", c as u32)), + c => out.push(c), + } + } + out.push('"'); +} + +pub fn parse(src: &str) -> Result { + let c: Vec = src.chars().collect(); + let mut p = Parser { c: &c, i: 0 }; + let v = p.value()?; + p.skip_ws(); + if p.i != c.len() { + return Err("extra text after the JSON value".into()); + } + Ok(v) +} + +struct Parser<'a> { + c: &'a [char], + i: usize, +} + +impl Parser<'_> { + fn skip_ws(&mut self) { + while self.i < self.c.len() && self.c[self.i].is_whitespace() { + self.i += 1; + } + } + + fn expect(&mut self, ch: char) -> Result<(), String> { + self.skip_ws(); + if self.c.get(self.i) == Some(&ch) { + self.i += 1; + Ok(()) + } else { + Err(format!("expected `{ch}` at {}", self.i)) + } + } + + fn word(&mut self, w: &str, v: Json) -> Result { + let n = w.chars().count(); + if self.c[self.i..].iter().take(n).collect::() == w { + self.i += n; + Ok(v) + } else { + Err(format!("bad JSON at {}", self.i)) + } + } + + fn value(&mut self) -> Result { + self.skip_ws(); + match self.c.get(self.i) { + None => Err("unexpected end of JSON".into()), + Some('n') => self.word("null", Json::Null), + Some('t') => self.word("true", Json::Bool(true)), + Some('f') => self.word("false", Json::Bool(false)), + Some('"') => Ok(Json::Str(self.string()?)), + Some('[') => { + self.i += 1; + let mut items = Vec::new(); + self.skip_ws(); + if self.c.get(self.i) == Some(&']') { + self.i += 1; + return Ok(Json::Arr(items)); + } + loop { + items.push(self.value()?); + self.skip_ws(); + match self.c.get(self.i) { + Some(',') => self.i += 1, + Some(']') => { + self.i += 1; + return Ok(Json::Arr(items)); + } + _ => return Err(format!("expected `,` or `]` at {}", self.i)), + } + } + } + Some('{') => { + self.i += 1; + let mut m = BTreeMap::new(); + self.skip_ws(); + if self.c.get(self.i) == Some(&'}') { + self.i += 1; + return Ok(Json::Obj(m)); + } + loop { + self.skip_ws(); + let k = self.string()?; + self.expect(':')?; + m.insert(k, self.value()?); + self.skip_ws(); + match self.c.get(self.i) { + Some(',') => self.i += 1, + Some('}') => { + self.i += 1; + return Ok(Json::Obj(m)); + } + _ => return Err(format!("expected `,` or `}}` at {}", self.i)), + } + } + } + Some(_) => { + let start = self.i; + while self.i < self.c.len() && matches!(self.c[self.i], '0'..='9' | '-' | '+' | '.' | 'e' | 'E') { + self.i += 1; + } + let text: String = self.c[start..self.i].iter().collect(); + text.parse::().map(Json::Num).map_err(|_| format!("bad JSON at {start}")) + } + } + } + + fn string(&mut self) -> Result { + if self.c.get(self.i) != Some(&'"') { + return Err(format!("expected a string at {}", self.i)); + } + self.i += 1; + let mut out = String::new(); + loop { + let ch = *self.c.get(self.i).ok_or("unterminated string")?; + self.i += 1; + match ch { + '"' => return Ok(out), + '\\' => { + let e = *self.c.get(self.i).ok_or("unterminated string")?; + self.i += 1; + match e { + 'n' => out.push('\n'), + 't' => out.push('\t'), + 'r' => out.push('\r'), + 'b' => out.push('\u{8}'), + 'f' => out.push('\u{c}'), + 'u' => { + let mut code = self.hex4()?; + // a pair of UTF-16 halves makes one character + if (0xD800..0xDC00).contains(&code) && self.c.get(self.i) == Some(&'\\') && self.c.get(self.i + 1) == Some(&'u') { + self.i += 2; + let low = self.hex4()?; + code = 0x10000 + ((code - 0xD800) << 10) + (low.wrapping_sub(0xDC00)); + } + out.push(char::from_u32(code).unwrap_or('\u{fffd}')); + } + other => out.push(other), + } + } + c => out.push(c), + } + } + } + + fn hex4(&mut self) -> Result { + let s: String = self.c.get(self.i..self.i + 4).ok_or("bad \\u escape")?.iter().collect(); + self.i += 4; + u32::from_str_radix(&s, 16).map_err(|_| "bad \\u escape".to_string()) + } +} diff --git a/src/lexer.rs b/src/lexer.rs new file mode 100644 index 0000000..0e4ead3 --- /dev/null +++ b/src/lexer.rs @@ -0,0 +1,591 @@ +use crate::ast::Span; +use crate::error::Diag; +use std::fmt; + +/// A piece of a string literal. `"hi {name}"` is `Text("hi ")` + `Code("name")`. +#[derive(Debug, Clone, PartialEq)] +pub enum StrPart { + Text(String), + Code { src: String, span: Span }, +} + +#[derive(Debug, Clone, PartialEq)] +pub enum Tok { + Ident(String), + Int(i64), + Float(f64), + /// `'a'` + Char(char), + Str(Vec), + + // keywords + Import, + Struct, + Enum, + Interface, + Func, + Var, + Const, + If, + Else, + For, + In, + Match, + Return, + Break, + Continue, + Pub, + Shared, + Weak, + Nil, + True, + False, + + // punctuation + LParen, + RParen, + LBrace, + RBrace, + LBracket, + RBracket, + Comma, + Dot, + DotDot, + Colon, + Question, + FatArrow, + Assign, + EqEq, + NotEq, + Lt, + Gt, + Le, + Ge, + Plus, + Minus, + Star, + Slash, + Percent, + Bang, + AndAnd, + OrOr, + PlusEq, + MinusEq, + StarEq, + SlashEq, + PercentEq, + + /// End of a statement (a real newline or `;`). Inserted like Go does. + Newline, + Eof, +} + +impl Tok { + fn text(&self) -> &'static str { + use Tok::*; + match self { + Import => "import", + Struct => "struct", + Enum => "enum", + Interface => "interface", + Func => "func", + Var => "var", + Const => "const", + If => "if", + Else => "else", + For => "for", + In => "in", + Match => "match", + Return => "return", + Break => "break", + Continue => "continue", + Pub => "pub", + Shared => "shared", + Weak => "weak", + Nil => "nil", + True => "true", + False => "false", + LParen => "(", + RParen => ")", + LBrace => "{", + RBrace => "}", + LBracket => "[", + RBracket => "]", + Comma => ",", + Dot => ".", + DotDot => "..", + Colon => ":", + Question => "?", + FatArrow => "=>", + Assign => "=", + EqEq => "==", + NotEq => "!=", + Lt => "<", + Gt => ">", + Le => "<=", + Ge => ">=", + Plus => "+", + Minus => "-", + Star => "*", + Slash => "/", + Percent => "%", + Bang => "!", + AndAnd => "&&", + OrOr => "||", + PlusEq => "+=", + MinusEq => "-=", + StarEq => "*=", + SlashEq => "/=", + PercentEq => "%=", + _ => "", + } + } +} + +impl fmt::Display for Tok { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Tok::Ident(s) => write!(f, "name `{s}`"), + Tok::Int(n) => write!(f, "number `{n}`"), + Tok::Float(x) => write!(f, "number `{x}`"), + Tok::Str(_) => write!(f, "a string"), + Tok::Char(_) => write!(f, "a character"), + Tok::Newline => write!(f, "end of line"), + Tok::Eof => write!(f, "end of file"), + other => write!(f, "`{}`", other.text()), + } + } +} + +#[derive(Debug, Clone)] +pub struct Token { + pub tok: Tok, + pub span: Span, +} + +pub fn lex(src: &str) -> Result, Diag> { + lex_at(src, 1, 1) +} + +/// Lex `src`, pretending it starts at `line:col` (used for `{code}` inside strings). +pub fn lex_at(src: &str, line: u32, col: u32) -> Result, Diag> { + let mut lx = Lexer { chars: src.chars().collect(), pos: 0, line, col, toks: Vec::new() }; + lx.run()?; + Ok(lx.toks) +} + +struct Lexer { + chars: Vec, + pos: usize, + line: u32, + col: u32, + toks: Vec, +} + +fn keyword(s: &str) -> Option { + Some(match s { + "import" => Tok::Import, + "struct" => Tok::Struct, + "enum" => Tok::Enum, + "interface" => Tok::Interface, + "func" => Tok::Func, + "var" => Tok::Var, + "const" => Tok::Const, + "if" => Tok::If, + "else" => Tok::Else, + "for" => Tok::For, + "in" => Tok::In, + "match" => Tok::Match, + "return" => Tok::Return, + "break" => Tok::Break, + "continue" => Tok::Continue, + "pub" => Tok::Pub, + "shared" => Tok::Shared, + "weak" => Tok::Weak, + "nil" => Tok::Nil, + "true" => Tok::True, + "false" => Tok::False, + _ => return None, + }) +} + +/// Tokens after which a line break ends the statement. +fn ends_statement(t: &Tok) -> bool { + matches!( + t, + Tok::Ident(_) + | Tok::Int(_) + | Tok::Float(_) + | Tok::Str(_) + | Tok::Char(_) + | Tok::RParen + | Tok::RBracket + | Tok::RBrace + | Tok::Question + | Tok::Return + | Tok::Break + | Tok::Continue + | Tok::Nil + | Tok::True + | Tok::False + ) +} + +impl Lexer { + fn span(&self) -> Span { + Span { line: self.line, col: self.col } + } + + fn peek(&self) -> Option { + self.chars.get(self.pos).copied() + } + + fn peek2(&self) -> Option { + self.chars.get(self.pos + 1).copied() + } + + fn bump(&mut self) -> Option { + let c = self.chars.get(self.pos).copied()?; + self.pos += 1; + if c == '\n' { + self.line += 1; + self.col = 1; + } else { + self.col += 1; + } + Some(c) + } + + fn eat(&mut self, c: char) -> bool { + if self.peek() == Some(c) { + self.bump(); + true + } else { + false + } + } + + fn push(&mut self, tok: Tok, span: Span) { + self.toks.push(Token { tok, span }); + } + + fn newline(&mut self, span: Span) { + if self.toks.last().map_or(false, |t| ends_statement(&t.tok)) { + self.push(Tok::Newline, span); + } + } + + fn run(&mut self) -> Result<(), Diag> { + while let Some(c) = self.peek() { + let span = self.span(); + match c { + '\n' => { + self.bump(); + self.newline(span); + } + ' ' | '\t' | '\r' => { + self.bump(); + } + ';' => { + self.bump(); + if self.toks.last().map_or(false, |t| t.tok != Tok::Newline) { + self.push(Tok::Newline, span); + } + } + '/' if self.peek2() == Some('/') => { + while let Some(c) = self.peek() { + if c == '\n' { + break; + } + self.bump(); + } + } + '/' if self.peek2() == Some('*') => { + self.bump(); + self.bump(); + loop { + match self.bump() { + None => return Err(Diag::new("unterminated comment", span)), + Some('*') if self.peek() == Some('/') => { + self.bump(); + break; + } + _ => {} + } + } + } + '"' => self.string(span)?, + '\'' => self.char_lit(span)?, + c if c.is_ascii_digit() => self.number(span)?, + c if c.is_alphabetic() || c == '_' => { + let mut s = String::new(); + while let Some(c) = self.peek() { + if c.is_alphanumeric() || c == '_' { + s.push(c); + self.bump(); + } else { + break; + } + } + let tok = keyword(&s).unwrap_or(Tok::Ident(s)); + self.push(tok, span); + } + _ => self.punct(c, span)?, + } + } + let end = self.span(); + self.newline(end); + self.push(Tok::Eof, end); + Ok(()) + } + + fn number(&mut self, span: Span) -> Result<(), Diag> { + let mut s = String::new(); + let digits = |lx: &mut Lexer, s: &mut String| { + while let Some(c) = lx.peek() { + if c.is_ascii_digit() { + s.push(c); + lx.bump(); + } else if c == '_' { + lx.bump(); + } else { + break; + } + } + }; + digits(self, &mut s); + // `1.5` is a float, but `1..5` is a range. + if self.peek() == Some('.') && self.peek2().map_or(false, |d| d.is_ascii_digit()) { + s.push('.'); + self.bump(); + digits(self, &mut s); + let x: f64 = s.parse().map_err(|_| Diag::new("invalid number", span))?; + self.push(Tok::Float(x), span); + } else { + let n: i64 = s + .parse() + .map_err(|_| Diag::new("number is too large", span))?; + self.push(Tok::Int(n), span); + } + Ok(()) + } + + /// `'a'`, `'\n'`, `'é'`: one character between single quotes. + fn char_lit(&mut self, span: Span) -> Result<(), Diag> { + self.bump(); // opening quote + let here = self.span(); + let c = match self.bump() { + None | Some('\n') => return Err(Diag::new("this character is never closed", span)), + Some('\'') => return Err(Diag::new("an empty character `''` (put one character between the quotes)", span)), + Some('\\') => match self.bump() { + Some('n') => '\n', + Some('t') => '\t', + Some('r') => '\r', + Some('0') => '\0', + Some('\\') => '\\', + Some('\'') => '\'', + Some('"') => '"', + Some(other) => return Err(Diag::new(format!("unknown escape `\\{other}`"), here)), + None => return Err(Diag::new("this character is never closed", span)), + }, + Some(c) => c, + }; + if self.bump() != Some('\'') { + return Err(Diag::new("a character holds exactly one character; for text use double quotes \"...\"", span)); + } + self.push(Tok::Char(c), span); + Ok(()) + } + + fn string(&mut self, span: Span) -> Result<(), Diag> { + self.bump(); // opening quote + let mut parts = Vec::new(); + let mut cur = String::new(); + loop { + let here = self.span(); + match self.bump() { + None | Some('\n') => return Err(Diag::new("this string is never closed", span)), + Some('"') => break, + Some('\\') => { + let esc = self.bump().ok_or_else(|| Diag::new("this string is never closed", span))?; + cur.push(match esc { + 'n' => '\n', + 't' => '\t', + 'r' => '\r', + '0' => '\0', + '\\' => '\\', + '"' => '"', + '{' => '{', + '}' => '}', + other => { + return Err(Diag::new(format!("unknown escape `\\{other}`"), here)); + } + }); + } + Some('{') => { + if !cur.is_empty() { + parts.push(StrPart::Text(std::mem::take(&mut cur))); + } + let code_span = self.span(); + let mut depth = 1; + let mut code = String::new(); + loop { + match self.bump() { + None | Some('\n') => { + return Err(Diag::new("this `{` in a string is never closed", here)) + } + Some('{') => { + depth += 1; + code.push('{'); + } + Some('}') => { + depth -= 1; + if depth == 0 { + break; + } + code.push('}'); + } + Some('"') => { + // a string inside the code part: copy it through + code.push('"'); + loop { + match self.bump() { + None | Some('\n') => { + return Err(Diag::new("this string is never closed", here)) + } + Some('\\') => { + code.push('\\'); + if let Some(c) = self.bump() { + code.push(c); + } + } + Some('"') => { + code.push('"'); + break; + } + Some(c) => code.push(c), + } + } + } + Some(c) => code.push(c), + } + } + if code.trim().is_empty() { + return Err(Diag::new("empty `{}` in a string (use `\\{` for a literal brace)", here)); + } + parts.push(StrPart::Code { src: code, span: code_span }); + } + Some(c) => cur.push(c), + } + } + if !cur.is_empty() { + parts.push(StrPart::Text(cur)); + } + self.push(Tok::Str(parts), span); + Ok(()) + } + + fn punct(&mut self, c: char, span: Span) -> Result<(), Diag> { + self.bump(); + let tok = match c { + '(' => Tok::LParen, + ')' => Tok::RParen, + '{' => Tok::LBrace, + '}' => Tok::RBrace, + '[' => Tok::LBracket, + ']' => Tok::RBracket, + ',' => Tok::Comma, + ':' => Tok::Colon, + '?' => Tok::Question, + '.' => { + if self.eat('.') { + Tok::DotDot + } else { + Tok::Dot + } + } + '=' => { + if self.eat('=') { + Tok::EqEq + } else if self.eat('>') { + Tok::FatArrow + } else { + Tok::Assign + } + } + '!' => { + if self.eat('=') { + Tok::NotEq + } else { + Tok::Bang + } + } + '<' => { + if self.eat('=') { + Tok::Le + } else { + Tok::Lt + } + } + '>' => { + if self.eat('=') { + Tok::Ge + } else { + Tok::Gt + } + } + '+' => { + if self.eat('=') { + Tok::PlusEq + } else { + Tok::Plus + } + } + '-' => { + if self.eat('=') { + Tok::MinusEq + } else { + Tok::Minus + } + } + '*' => { + if self.eat('=') { + Tok::StarEq + } else { + Tok::Star + } + } + '/' => { + if self.eat('=') { + Tok::SlashEq + } else { + Tok::Slash + } + } + '%' => { + if self.eat('=') { + Tok::PercentEq + } else { + Tok::Percent + } + } + '&' => { + if self.eat('&') { + Tok::AndAnd + } else { + return Err(Diag::new("a single `&` is not part of Dino; use `&&` for \"and\"", span)); + } + } + '|' => { + if self.eat('|') { + Tok::OrOr + } else { + return Err(Diag::new("a single `|` is not part of Dino; use `||` for \"or\"", span)); + } + } + other => return Err(Diag::new(format!("unexpected character `{other}`"), span)), + }; + self.push(tok, span); + Ok(()) + } +} diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..661dc08 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,18 @@ +//! The Dino compiler. + +pub mod ast; +pub mod check; +pub mod codegen_c; +pub mod driver; +pub mod error; +pub mod fmt; +pub mod hir; +pub mod index; +pub mod lexer; +pub mod json; +pub mod loader; +pub mod lsp; +pub mod moves; +pub mod parser; +pub mod stdlib; +pub mod types; diff --git a/src/loader.rs b/src/loader.rs new file mode 100644 index 0000000..5f27e5d --- /dev/null +++ b/src/loader.rs @@ -0,0 +1,68 @@ +//! Finds the files a program imports. `import utils` loads `utils.dino` from the +//! folder of the main file; `import a.b` loads `a/b.dino`. + +use crate::ast::{Item, Program}; +use crate::error::{Diag, PResult}; +use crate::{parser, stdlib}; + +pub struct Module { + /// The import path, like `utils` or `a.b`. + pub key: String, + pub program: Program, +} + +pub struct Loaded { + pub main: Program, + /// Every imported file, each one after the files it imports. + pub modules: Vec, +} + +/// `read(key)` returns the display path and the source of a module, if it exists. +pub fn load(main_src: &str, read: &mut dyn FnMut(&str) -> Option<(String, String)>) -> PResult { + let main = parser::parse(main_src)?; + let mut modules = Vec::new(); + let mut stack = Vec::new(); + load_imports(&main, "", read, &mut modules, &mut stack)?; + Ok(Loaded { main, modules }) +} + +fn load_imports( + p: &Program, + from: &str, + read: &mut dyn FnMut(&str) -> Option<(String, String)>, + modules: &mut Vec, + stack: &mut Vec, +) -> PResult<()> { + for item in &p.items { + let Item::Import(i) = item else { continue }; + if i.alias.is_some() || stdlib::MODULES.contains(&i.path.as_str()) { + continue; + } + let key = i.path.clone(); + if modules.iter().any(|m| m.key == key) { + continue; + } + if stack.contains(&key) { + let mut cycle = stack[stack.iter().position(|k| *k == key).unwrap()..].to_vec(); + cycle.push(key.clone()); + return Err(Diag::new(format!("circular import: {}", cycle.join(" -> ")), i.span).in_module(from)); + } + let Some((_, src)) = read(&key) else { + let file = format!("{}.dino", key.replace('.', "/")); + return Err(Diag::new( + format!( + "cannot find module `{key}` (looked for {file} next to your program; built-in modules: {})", + stdlib::MODULES.join(", ") + ), + i.span, + ) + .in_module(from)); + }; + let program = parser::parse(&src).map_err(|d| d.in_module(&key))?; + stack.push(key.clone()); + load_imports(&program, &key, read, modules, stack)?; + stack.pop(); + modules.push(Module { key, program }); + } + Ok(()) +} diff --git a/src/lsp.rs b/src/lsp.rs new file mode 100644 index 0000000..198c09e --- /dev/null +++ b/src/lsp.rs @@ -0,0 +1,755 @@ +//! The language server (`dino lsp`): talks to an editor over stdin/stdout and shows the +//! compiler's errors while you type, and formats the file on request. + +use std::collections::HashMap; +use std::io::{BufRead, Write}; +use std::path::PathBuf; + +use crate::error::Diag; +use crate::json::{self, Json}; +use crate::index::{Index, Kind, Sym}; +use crate::{driver, fmt, stdlib}; + +/// Run the server until the editor says `exit`. Returns the process exit code. +pub fn run(mut input: impl BufRead, mut output: impl Write) -> i32 { + let mut server = Server { docs: HashMap::new(), indexes: HashMap::new(), shutdown: false }; + loop { + let Some(msg) = read_message(&mut input) else { return 1 }; + let Ok(msg) = json::parse(&msg) else { continue }; + let replies = server.handle(&msg); + for r in replies { + let body = r.to_string(); + let _ = write!(output, "Content-Length: {}\r\n\r\n{}", body.len(), body); + let _ = output.flush(); + } + if msg.get("method").and_then(Json::as_str) == Some("exit") { + return if server.shutdown { 0 } else { 1 }; + } + } +} + +fn read_message(input: &mut impl BufRead) -> Option { + let mut len: Option = None; + loop { + let mut line = String::new(); + if input.read_line(&mut line).ok()? == 0 { + return None; + } + let line = line.trim(); + if line.is_empty() { + break; + } + if let Some(v) = line.to_ascii_lowercase().strip_prefix("content-length:") { + len = v.trim().parse().ok(); + } + } + let mut buf = vec![0u8; len?]; + input.read_exact(&mut buf).ok()?; + String::from_utf8(buf).ok() +} + +struct Server { + /// Open files: uri -> text. + docs: HashMap, + /// What each open file declares (the last version that could be read). + indexes: HashMap, + shutdown: bool, +} + +impl Server { + fn handle(&mut self, msg: &Json) -> Vec { + let method = msg.get("method").and_then(Json::as_str).unwrap_or(""); + let id = msg.get("id").cloned(); + let params = msg.get("params").cloned().unwrap_or(Json::Null); + let reply = |result: Json| -> Vec { + match &id { + Some(id) => vec![Json::obj(vec![("jsonrpc", Json::str("2.0")), ("id", id.clone()), ("result", result)])], + None => vec![], + } + }; + match method { + "initialize" => reply(Json::obj(vec![ + ( + "capabilities", + Json::obj(vec![ + ("textDocumentSync", Json::Num(1.0)), + ("documentFormattingProvider", Json::Bool(true)), + ("documentSymbolProvider", Json::Bool(true)), + ("definitionProvider", Json::Bool(true)), + ("hoverProvider", Json::Bool(true)), + ("completionProvider", Json::obj(vec![("triggerCharacters", Json::Arr(vec![Json::str(".")]))])), + ("signatureHelpProvider", Json::obj(vec![("triggerCharacters", Json::Arr(vec![Json::str("("), Json::str(",")]))])), + ]), + ), + ("serverInfo", Json::obj(vec![("name", Json::str("dino"))])), + ])), + "shutdown" => { + self.shutdown = true; + reply(Json::Null) + } + "textDocument/didOpen" => { + let (Some(uri), Some(text)) = ( + params.path(&["textDocument", "uri"]).and_then(Json::as_str), + params.path(&["textDocument", "text"]).and_then(Json::as_str), + ) else { + return vec![]; + }; + self.set_text(uri, text); + vec![self.diagnostics(uri)] + } + "textDocument/didChange" => { + let uri = params.path(&["textDocument", "uri"]).and_then(Json::as_str); + let text = match params.get("contentChanges") { + Some(Json::Arr(changes)) => changes.last().and_then(|c| c.get("text")).and_then(Json::as_str), + _ => None, + }; + let (Some(uri), Some(text)) = (uri, text) else { return vec![] }; + self.set_text(uri, text); + vec![self.diagnostics(uri)] + } + "textDocument/didSave" => match params.path(&["textDocument", "uri"]).and_then(Json::as_str) { + Some(uri) if self.docs.contains_key(uri) => vec![self.diagnostics(uri)], + _ => vec![], + }, + "textDocument/didClose" => match params.path(&["textDocument", "uri"]).and_then(Json::as_str) { + Some(uri) => { + self.docs.remove(uri); + self.indexes.remove(uri); + vec![publish(uri, vec![])] + } + None => vec![], + }, + "textDocument/documentSymbol" => { + let uri = params.path(&["textDocument", "uri"]).and_then(Json::as_str).unwrap_or(""); + reply(self.symbols(uri)) + } + "textDocument/definition" | "textDocument/hover" | "textDocument/completion" | "textDocument/signatureHelp" => { + let uri = params.path(&["textDocument", "uri"]).and_then(Json::as_str).unwrap_or(""); + let line = params.path(&["position", "line"]).and_then(Json::as_i64).unwrap_or(0) as usize; + let ch = params.path(&["position", "character"]).and_then(Json::as_i64).unwrap_or(0) as usize; + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| match method { + "textDocument/definition" => self.definition(uri, line, ch), + "textDocument/hover" => self.hover(uri, line, ch), + "textDocument/completion" => self.completion(uri, line, ch), + _ => self.signature(uri, line, ch), + })); + reply(result.unwrap_or(Json::Null)) + } + "textDocument/formatting" => { + let uri = params.path(&["textDocument", "uri"]).and_then(Json::as_str).unwrap_or(""); + let Some(text) = self.docs.get(uri) else { return reply(Json::Null) }; + match fmt::format(text) { + // a file that doesn't parse can't be formatted; the editor already shows why + Err(_) => reply(Json::Null), + Ok(new) if &new == text => reply(Json::Arr(vec![])), + Ok(new) => { + let lines = text.split('\n').count(); + reply(Json::Arr(vec![Json::obj(vec![ + ("range", range((0, 0), (lines as i64, 0))), + ("newText", Json::str(new)), + ])])) + } + } + } + _ => match &id { + // a request we don't know + Some(id) if !method.is_empty() => vec![Json::obj(vec![ + ("jsonrpc", Json::str("2.0")), + ("id", id.clone()), + ("error", Json::obj(vec![("code", Json::Num(-32601.0)), ("message", Json::str(format!("not supported: {method}")))])), + ])], + _ => vec![], + }, + } + } + + /// Check the file and build the `publishDiagnostics` message. + fn diagnostics(&self, uri: &str) -> Json { + let text = &self.docs[uri]; + let dir: Option = uri_to_path(uri).and_then(|p| p.parent().map(PathBuf::from)); + let docs = &self.docs; + let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| { + driver::diagnose(text, |key| { + let file = dir.as_ref()?.join(format!("{}.dino", key.replace('.', "/"))); + // an import that is open in the editor counts with its unsaved changes + for (u, t) in docs { + if uri_to_path(u).as_deref() == Some(file.as_path()) { + return Some((file.display().to_string(), t.clone())); + } + } + std::fs::read_to_string(&file).ok().map(|s| (file.display().to_string(), s)) + }) + })); + let diags = match result { + Ok(Ok(())) | Err(_) => vec![], + Ok(Err(d)) => vec![diagnostic(&d, text)], + }; + publish(uri, diags) + } +} + +fn publish(uri: &str, diagnostics: Vec) -> Json { + Json::obj(vec![ + ("jsonrpc", Json::str("2.0")), + ("method", Json::str("textDocument/publishDiagnostics")), + ("params", Json::obj(vec![("uri", Json::str(uri)), ("diagnostics", Json::Arr(diagnostics))])), + ]) +} + +fn range(start: (i64, i64), end: (i64, i64)) -> Json { + let pos = |(l, c): (i64, i64)| Json::obj(vec![("line", Json::Num(l as f64)), ("character", Json::Num(c as f64))]); + Json::obj(vec![("start", pos(start)), ("end", pos(end))]) +} + +/// A compiler error as an editor diagnostic. Errors in another file are shown at the top of this one. +fn diagnostic(d: &Diag, text: &str) -> Json { + let (message, start, end) = match &d.file { + Some(module) => (format!("in module `{module}`: {}", d.msg), (0, 0), (0, 1)), + None => { + let line_idx = (d.span.line as usize).saturating_sub(1); + let line = text.split('\n').nth(line_idx).unwrap_or("").trim_end_matches('\r'); + let chars: Vec = line.chars().collect(); + let col = (d.span.col as usize).saturating_sub(1).min(chars.len()); + // underline the word at the error (or one character) + let mut stop = col; + while stop < chars.len() && (chars[stop].is_alphanumeric() || chars[stop] == '_') { + stop += 1; + } + if stop == col { + stop = (col + 1).min(chars.len()); + } + let utf16 = |upto: usize| chars[..upto].iter().map(|c| c.len_utf16()).sum::() as i64; + (d.msg.clone(), (line_idx as i64, utf16(col)), (line_idx as i64, utf16(stop))) + } + }; + Json::obj(vec![ + ("range", range(start, end)), + ("severity", Json::Num(1.0)), + ("source", Json::str("dino")), + ("message", Json::str(message)), + ]) +} + +/// `file:///home/me/a%20b.dino` -> `/home/me/a b.dino` +fn uri_to_path(uri: &str) -> Option { + let rest = uri.strip_prefix("file://")?; + let bytes = rest.as_bytes(); + let mut out: Vec = Vec::new(); + let mut i = 0; + while i < bytes.len() { + if bytes[i] == b'%' && i + 2 < bytes.len() { + if let Ok(b) = u8::from_str_radix(&rest[i + 1..i + 3], 16) { + out.push(b); + i += 3; + continue; + } + } + out.push(bytes[i]); + i += 1; + } + String::from_utf8(out).ok().map(PathBuf::from) +} + +// ---------- code intelligence ---------- + +/// Something found for a name: where it is declared. +struct Found { + uri: String, + sym: Sym, + /// The text of the file it is in. + text: String, +} + +fn utf16_len(s: &str) -> usize { + s.chars().map(|c| c.len_utf16()).sum() +} + +/// The character index in `line` that is `units` UTF-16 units from its start. +fn char_of_units(line: &str, units: usize) -> usize { + let mut used = 0; + for (i, c) in line.chars().enumerate() { + if used >= units { + return i; + } + used += c.len_utf16(); + } + line.chars().count() +} + +fn is_ident(c: char) -> bool { + c.is_alphanumeric() || c == '_' +} + +/// The identifier around character `col`: its text, and where it starts and ends (in characters). +fn word_at(line: &str, col: usize) -> Option<(String, usize, usize)> { + let c: Vec = line.chars().collect(); + let col = col.min(c.len()); + let mut a = col; + while a > 0 && is_ident(c[a - 1]) { + a -= 1; + } + let mut b = col; + while b < c.len() && is_ident(c[b]) { + b += 1; + } + if a == b { + return None; + } + Some((c[a..b].iter().collect(), a, b)) +} + +/// The identifier that ends right before character `end`, skipping nothing. +fn word_before(line: &str, end: usize) -> Option { + let c: Vec = line.chars().collect(); + let mut a = end.min(c.len()); + let b = a; + while a > 0 && is_ident(c[a - 1]) { + a -= 1; + } + if a == b { + None + } else { + Some(c[a..b].iter().collect()) + } +} + +fn path_to_uri(p: &std::path::Path) -> String { + let mut out = String::from("file://"); + for b in p.display().to_string().bytes() { + if b.is_ascii_alphanumeric() || b"/-_.~".contains(&b) { + out.push(b as char); + } else { + out.push_str(&format!("%{b:02X}")); + } + } + out +} + +fn kind_number(k: Kind) -> i64 { + match k { + Kind::Func => 12, + Kind::Method => 6, + Kind::Struct => 23, + Kind::Enum => 10, + Kind::Interface => 11, + Kind::Const => 14, + Kind::Field => 8, + Kind::Variant => 22, + Kind::Local => 13, + } +} + +/// The completion kind numbers of the protocol. +fn completion_kind(k: Kind) -> i64 { + match k { + Kind::Func => 3, + Kind::Method => 2, + Kind::Struct => 22, + Kind::Enum => 13, + Kind::Interface => 8, + Kind::Const => 21, + Kind::Field => 5, + Kind::Variant => 20, + Kind::Local => 6, + } +} + +impl Server { + fn set_text(&mut self, uri: &str, text: &str) { + self.docs.insert(uri.to_string(), text.to_string()); + if let Some(ix) = Index::new(text) { + self.indexes.insert(uri.to_string(), ix); + } + } + + /// The source of the module `key` as seen from the file `uri`: open in the editor, or on disk. + fn module_source(&self, uri: &str, key: &str) -> Option<(String, String)> { + let dir = uri_to_path(uri)?.parent()?.to_path_buf(); + let file = dir.join(format!("{}.dino", key.replace('.', "/"))); + let target = path_to_uri(&file); + for (u, t) in &self.docs { + if uri_to_path(u).as_deref() == Some(file.as_path()) { + return Some((u.clone(), t.clone())); + } + } + std::fs::read_to_string(&file).ok().map(|t| (target, t)) + } + + fn line_of<'a>(&'a self, uri: &str, line: usize) -> Option<&'a str> { + self.docs.get(uri)?.split('\n').nth(line).map(|l| l.trim_end_matches('\r')) + } + + /// Declared in this file, or in the module `module` that it imports. + fn find_in_module(&self, uri: &str, module: &str, name: &str) -> Option { + let ix = self.indexes.get(uri)?; + let imp = ix.module(module)?; + if imp.path.is_empty() { + return None; + } + let (muri, text) = self.module_source(uri, &imp.path)?; + let mix = Index::new(&text)?; + let sym = mix.top(name)?.clone(); + Some(Found { uri: muri, sym, text }) + } + + /// What the name at a position refers to. + fn resolve(&self, uri: &str, line: usize, ch: usize) -> Option { + let text = self.docs.get(uri)?; + let ix = self.indexes.get(uri)?; + let line_text = self.line_of(uri, line)?; + let col = char_of_units(line_text, ch); + let (word, a, _) = word_at(line_text, col)?; + let here = |sym: Sym| Found { uri: uri.to_string(), sym, text: text.clone() }; + // `module.name` or `value.member` + let chars: Vec = line_text.chars().collect(); + if a > 0 && chars[a - 1] == '.' { + let left = word_before(line_text, a - 1); + if let Some(q) = &left { + if ix.module(q).is_some() { + return self.find_in_module(uri, q, &word); + } + } + if let Some(m) = ix.members(&word).first() { + return Some(here((*m).clone())); + } + // a member of a type from an imported file + for imp in &ix.imports { + if imp.path.is_empty() { + continue; + } + if let Some((muri, mtext)) = self.module_source(uri, &imp.path) { + if let Some(mix) = Index::new(&mtext) { + if let Some(m) = mix.members(&word).first() { + return Some(Found { uri: muri, sym: (*m).clone(), text: mtext }); + } + } + } + } + return None; + } + if let Some(l) = ix.locals_at(line, a).into_iter().rev().find(|s| s.name == word) { + return Some(here(l)); + } + if let Some(t) = ix.enclosing_type(line) { + if let Some(m) = ix.syms.iter().find(|s| s.container.as_deref() == Some(&t.name) && s.name == word) { + return Some(here(m.clone())); + } + } + if let Some(t) = ix.top(&word) { + return Some(here(t.clone())); + } + // a variant written bare in a `match` (`Circle(r) =>`) + if let Some(m) = ix.syms.iter().find(|s| s.kind == Kind::Variant && s.name == word) { + return Some(here(m.clone())); + } + // the name of an imported file + if ix.module(&word).is_some() { + let imp = ix.module(&word)?; + if let Some((muri, mtext)) = self.module_source(uri, &imp.path) { + let sym = Sym { name: word.clone(), kind: Kind::Const, line: 0, col: 0, end_line: 0, container: None, is_pub: true }; + return Some(Found { uri: muri, sym, text: mtext }); + } + } + None + } + + fn symbols(&self, uri: &str) -> Json { + let Some(ix) = self.indexes.get(uri) else { return Json::Arr(vec![]) }; + let text = self.docs.get(uri).map(String::as_str).unwrap_or(""); + let lines: Vec<&str> = text.split('\n').collect(); + let width = |l: usize| utf16_len(lines.get(l).copied().unwrap_or("").trim_end_matches('\r')) as i64; + let make = |s: &Sym, children: Vec| -> Json { + let name_end = s.col as i64 + utf16_len(&s.name) as i64; + let col = utf16_len(&lines.get(s.line).map_or(String::new(), |l| l.chars().take(s.col).collect())) as i64; + Json::obj(vec![ + ("name", Json::str(s.name.clone())), + ("kind", Json::Num(kind_number(s.kind) as f64)), + ("range", range((s.line as i64, 0), (s.end_line as i64, width(s.end_line)))), + ("selectionRange", range((s.line as i64, col), (s.line as i64, col + (name_end - s.col as i64)))), + ("children", Json::Arr(children)), + ]) + }; + let mut out = Vec::new(); + for s in ix.syms.iter().filter(|s| s.container.is_none()) { + let kids: Vec = + ix.syms.iter().filter(|m| m.container.as_deref() == Some(&s.name)).map(|m| make(m, vec![])).collect(); + out.push(make(s, kids)); + } + Json::Arr(out) + } + + fn definition(&self, uri: &str, line: usize, ch: usize) -> Json { + let Some(f) = self.resolve(uri, line, ch) else { return Json::Null }; + let lines: Vec<&str> = f.text.split('\n').collect(); + let before: String = lines.get(f.sym.line).map_or(String::new(), |l| l.chars().take(f.sym.col).collect()); + let col = utf16_len(&before) as i64; + let len = utf16_len(&f.sym.name) as i64; + Json::obj(vec![("uri", Json::str(f.uri)), ("range", range((f.sym.line as i64, col), (f.sym.line as i64, col + len)))]) + } + + /// The declaration as written: from its line up to the `{` that opens its body. + fn declaration_text(f: &Found) -> String { + let lines: Vec<&str> = f.text.split('\n').collect(); + let mut out: Vec = Vec::new(); + for l in lines.iter().skip(f.sym.line).take(6) { + let l = l.trim(); + if let Some(i) = l.find('{') { + // keep a field-less struct literal out of it; the body starts at the first brace + let head = l[..i].trim(); + if !head.is_empty() { + out.push(head.to_string()); + } + break; + } + out.push(l.to_string()); + if !matches!(f.sym.kind, Kind::Func | Kind::Method) || l.ends_with(')') && !l.contains(':') { + // one line is enough for everything but a signature that continues + } + let open = out.join(" ").matches('(').count(); + let close = out.join(" ").matches(')').count(); + if open == close { + break; + } + } + let mut text = out.join(" "); + // a field or a local: the name and what it is, without a trailing comment + if let Some(i) = text.find("//") { + text.truncate(i); + } + text.trim().to_string() + } + + /// The `//` comment lines right above a declaration. + fn doc_comment(f: &Found) -> String { + let lines: Vec<&str> = f.text.split('\n').collect(); + let mut docs: Vec = Vec::new(); + let mut l = f.sym.line; + // a `pub` or `func` keyword sits on the name's line + while l > 0 { + let above = lines[l - 1].trim(); + match above.strip_prefix("//") { + Some(c) => docs.push(c.trim().to_string()), + None => break, + } + l -= 1; + } + docs.reverse(); + docs.join("\n") + } + + fn hover(&self, uri: &str, line: usize, ch: usize) -> Json { + let line_text = self.line_of(uri, line).unwrap_or(""); + let col = char_of_units(line_text, ch); + // a name from a built-in module: `math.sqrt` + if let Some((word, a, _)) = word_at(line_text, col) { + let chars: Vec = line_text.chars().collect(); + if a > 0 && chars[a - 1] == '.' { + if let Some(q) = word_before(line_text, a - 1) { + if let Some(sig) = stdlib::module_fn(&q, &word) { + let params: Vec = sig.params.iter().map(|t| t.to_string()).collect(); + let ret = if sig.ret == crate::types::Ty::Unit { String::new() } else { format!(": {}", sig.ret) }; + let text = format!("{q}.{word}({}){ret}", params.join(", ")); + return hover_text(text); + } + } + } + } + let Some(f) = self.resolve(uri, line, ch) else { return Json::Null }; + let mut text = Self::declaration_text(&f); + if text.is_empty() { + return Json::Null; + } + let doc = Self::doc_comment(&f); + if !doc.is_empty() { + text = format!("{text}\n\n{doc}"); + } + hover_text(text) + } + + fn completion(&self, uri: &str, line: usize, ch: usize) -> Json { + let Some(ix) = self.indexes.get(uri) else { return Json::Arr(vec![]) }; + let line_text = self.line_of(uri, line).unwrap_or(""); + let col = char_of_units(line_text, ch); + let chars: Vec = line_text.chars().collect(); + // the word being typed, and what comes before it + let mut a = col.min(chars.len()); + while a > 0 && is_ident(chars[a - 1]) { + a -= 1; + } + let item = |label: &str, kind: i64, detail: &str| -> Json { + Json::obj(vec![("label", Json::str(label)), ("kind", Json::Num(kind as f64)), ("detail", Json::str(detail))]) + }; + let mut items: Vec = Vec::new(); + let mut seen: std::collections::HashSet = std::collections::HashSet::new(); + let mut push = |items: &mut Vec, label: &str, kind: i64, detail: &str| { + if seen.insert(label.to_string()) { + items.push(item(label, kind, detail)); + } + }; + if a > 0 && chars[a - 1] == '.' { + let left = word_before(line_text, a - 1); + if let Some(q) = left.as_deref() { + // a built-in module + if stdlib::MODULES.contains(&q) && ix.module(q).is_some() { + for n in stdlib::module_names(q) { + let detail = stdlib::module_fn(q, n) + .map(|s| format!("{}({})", n, s.params.iter().map(|t| t.to_string()).collect::>().join(", "))) + .unwrap_or_default(); + let kind = if n.chars().all(|c| c.is_uppercase()) { 21 } else { 3 }; + push(&mut items, n, kind, &detail); + } + return Json::Arr(items); + } + // one of your own files + if let Some(imp) = ix.module(q) { + if !imp.path.is_empty() { + if let Some((_, text)) = self.module_source(uri, &imp.path) { + if let Some(mix) = Index::new(&text) { + for s in mix.syms.iter().filter(|s| s.container.is_none() && s.is_pub) { + push(&mut items, &s.name, completion_kind(s.kind), ""); + } + } + } + } + return Json::Arr(items); + } + } + // a value: its fields and methods are not known without types, so offer every member + // of the types in this file, then what strings, lists, maps and characters have + for s in ix.syms.iter().filter(|s| s.container.is_some()) { + push(&mut items, &s.name, completion_kind(s.kind), s.container.as_deref().unwrap_or("")); + } + for imp in &ix.imports { + if imp.path.is_empty() { + continue; + } + if let Some((_, text)) = self.module_source(uri, &imp.path) { + if let Some(mix) = Index::new(&text) { + for s in mix.syms.iter().filter(|s| s.container.is_some() && s.is_pub) { + push(&mut items, &s.name, completion_kind(s.kind), s.container.as_deref().unwrap_or("")); + } + } + } + } + for n in ["len", "push", "pop", "has", "get", "get_or", "remove", "keys", "values", "upgrade"] { + push(&mut items, n, 2, "built in"); + } + for n in stdlib::STR_METHODS.split(", ").chain(stdlib::CHAR_METHODS.split(", ")).chain(["join"]) { + push(&mut items, n, 2, "built in"); + } + return Json::Arr(items); + } + for l in ix.locals_at(line, a).into_iter().rev() { + push(&mut items, &l.name, 6, "variable"); + } + if let Some(t) = ix.enclosing_type(line) { + for s in ix.syms.iter().filter(|s| s.container.as_deref() == Some(&t.name)) { + push(&mut items, &s.name, completion_kind(s.kind), &t.name); + } + } + for s in ix.syms.iter().filter(|s| s.container.is_none()) { + push(&mut items, &s.name, completion_kind(s.kind), ""); + } + for imp in &ix.imports { + push(&mut items, &imp.name, 9, "module"); + } + for k in [ + "func", "var", "const", "import", "struct", "enum", "interface", "pub", "if", "else", "for", "in", "step", "match", + "return", "break", "continue", "shared", "weak", "nil", "true", "false", + ] { + push(&mut items, k, 14, "keyword"); + } + for t in ["int", "float", "bool", "char", "string", "error", "map", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64"] { + push(&mut items, t, 7, "type"); + } + for f in ["print", "error"] { + push(&mut items, f, 3, "built in"); + } + for m in ["os", "math", "time"] { + if !ix.imports.iter().any(|i| i.name == m) { + push(&mut items, m, 9, "built-in module (needs `import`)"); + } + } + Json::Arr(items) + } + + /// The signature of the function being called at the cursor, with the active parameter. + fn signature(&self, uri: &str, line: usize, ch: usize) -> Json { + let Some(text) = self.docs.get(uri) else { return Json::Null }; + let lines: Vec<&str> = text.split('\n').collect(); + // walk back from the cursor to the `(` that is still open + let mut depth = 0i32; + let mut commas = 0usize; + let mut open: Option<(usize, usize)> = None; + 'outer: for l in (line.saturating_sub(20)..=line).rev() { + let t: Vec = lines.get(l).copied().unwrap_or("").trim_end_matches('\r').chars().collect(); + let end = if l == line { char_of_units(lines[l], ch).min(t.len()) } else { t.len() }; + for c in (0..end).rev() { + match t[c] { + ')' | ']' | '}' => depth += 1, + '(' | '[' | '{' => { + if depth == 0 { + if t[c] == '(' { + open = Some((l, c)); + } + break 'outer; + } + depth -= 1; + } + ',' if depth == 0 => commas += 1, + _ => {} + } + } + } + let Some((ol, oc)) = open else { return Json::Null }; + let open_line = lines[ol]; + let Some(name) = word_before(open_line, oc) else { return Json::Null }; + let name_start = oc - name.chars().count(); + let units_before_name: usize = utf16_len(&open_line.chars().take(name_start).collect::()); + let Some(f) = self.resolve(uri, ol, units_before_name) else { return Json::Null }; + if !matches!(f.sym.kind, Kind::Func | Kind::Method) { + return Json::Null; + } + let decl = Self::declaration_text(&f); + // `pub func add(a: int, b: int): int` -> `add(a: int, b: int): int` + let label = decl.find("func ").map_or(decl.clone(), |i| decl[i + 5..].trim().to_string()); + let (Some(p0), Some(p1)) = (label.find('('), label.rfind(')')) else { return Json::Null }; + // the parameters: split the text between the parentheses at top-level commas + let mut params: Vec = Vec::new(); + let (mut d, mut start) = (0i32, p0 + 1); + let bytes: Vec<(usize, char)> = label.char_indices().collect(); + let unit = |byte: usize| utf16_len(&label[..byte]) as f64; + for &(i, c) in &bytes { + if i <= p0 || i > p1 { + continue; + } + match c { + '(' | '[' | '{' => d += 1, + ')' | ']' | '}' if i < p1 || d > 0 => d -= 1, + ',' if d == 0 => { + params.push(Json::obj(vec![("label", Json::Arr(vec![Json::Num(unit(start)), Json::Num(unit(i))]))])); + start = i + 1; + while label[start..].starts_with(' ') { + start += 1; + } + } + _ => {} + } + } + if label[p0 + 1..p1].trim().len() > 0 { + params.push(Json::obj(vec![("label", Json::Arr(vec![Json::Num(unit(start)), Json::Num(unit(p1))]))])); + } + Json::obj(vec![ + ( + "signatures", + Json::Arr(vec![Json::obj(vec![("label", Json::str(label)), ("parameters", Json::Arr(params))])]), + ), + ("activeSignature", Json::Num(0.0)), + ("activeParameter", Json::Num(commas as f64)), + ]) + } +} + +fn hover_text(text: String) -> Json { + Json::obj(vec![("contents", Json::obj(vec![("kind", Json::str("plaintext")), ("value", Json::str(text))]))]) +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..f172bfe --- /dev/null +++ b/src/main.rs @@ -0,0 +1,174 @@ +use std::path::PathBuf; +use std::{env, fs, process}; + +use dino::{driver, fmt, lexer, lsp, parser}; + +fn usage() -> ! { + eprintln!("Dino compiler (work in progress)\n"); + eprintln!("usage:"); + eprintln!(" dino run [args...] compile and run (args go to the program)"); + eprintln!(" dino build [-o out] compile to an executable"); + eprintln!(" dino emit-c print the generated C code"); + eprintln!(" dino check check for errors without building"); + eprintln!(" dino fmt [--check] .. format code in place (--check only reports; `-` = stdin to stdout)"); + eprintln!(" dino lsp run the language server (for editors)"); + eprintln!(" dino parse print the syntax tree"); + eprintln!(" dino tokens print the tokens"); + process::exit(2); +} + +fn die(msg: &str) -> ! { + eprintln!("{msg}"); + process::exit(1); +} + +/// All `.dino` files at `path` (a file, or a folder searched recursively). +fn dino_files(path: &std::path::Path, out: &mut Vec) { + if path.is_dir() { + let mut entries: Vec = fs::read_dir(path).map(|r| r.filter_map(|e| e.ok().map(|e| e.path())).collect()).unwrap_or_default(); + entries.sort(); + for e in entries { + dino_files(&e, out); + } + } else if path.extension().map_or(false, |e| e == "dino") || path.is_file() && out.is_empty() && path.extension().is_none() { + out.push(path.to_path_buf()); + } +} + +/// `dino fmt [--check] files-or-folders...` +fn run_fmt(args: &[String]) -> ! { + // `dino fmt -`: text in on stdin, tidy text out on stdout (for editors) + if args.len() == 1 && args[0] == "-" { + let mut src = String::new(); + if std::io::Read::read_to_string(&mut std::io::stdin(), &mut src).is_err() { + die("error: cannot read the input"); + } + match fmt::format(&src) { + Ok(out) => { + print!("{out}"); + process::exit(0); + } + Err(d) => die(&d.render("", &src)), + } + } + let check = args.iter().any(|a| a == "--check"); + let paths: Vec<&String> = args.iter().filter(|a| *a != "--check").collect(); + if paths.is_empty() { + usage(); + } + let mut files = Vec::new(); + for p in paths { + let before = files.len(); + let path = PathBuf::from(p); + if path.is_file() { + files.push(path); + } else { + dino_files(&path, &mut files); + if files.len() == before { + die(&format!("error: no .dino files found at {p}")); + } + } + } + let mut problems = 0; + let mut changed = 0; + for file in &files { + let shown = file.display().to_string(); + let src = fs::read_to_string(file).unwrap_or_else(|e| die(&format!("error: cannot read {shown}: {e}"))); + match fmt::format(&src) { + Err(d) => { + eprintln!("{}", d.render(&shown, &src)); + problems += 1; + } + Ok(out) if out == src => {} + Ok(out) => { + changed += 1; + if check { + println!("{shown} needs formatting"); + } else if let Err(e) = fs::write(file, out) { + die(&format!("error: cannot write {shown}: {e}")); + } else { + println!("formatted {shown}"); + } + } + } + } + if problems > 0 || (check && changed > 0) { + process::exit(1); + } + process::exit(0); +} + +fn main() { + let args: Vec = env::args().skip(1).collect(); + if args.first().map(String::as_str) == Some("lsp") { + let stdin = std::io::stdin(); + process::exit(lsp::run(stdin.lock(), std::io::stdout())); + } + if args.first().map(String::as_str) == Some("fmt") { + run_fmt(&args[1..]); + } + if args.len() < 2 { + usage(); + } + let cmd = args[0].as_str(); + let path = &args[1]; + let mut out: Option = None; + // `dino run file.dino a b c` hands `a b c` to the program; `build` takes `-o` + let mut program_args: Vec = Vec::new(); + let mut rest = args[2..].iter(); + while let Some(a) = rest.next() { + match a.as_str() { + "-o" if cmd == "build" => out = Some(PathBuf::from(rest.next().unwrap_or_else(|| usage()))), + _ if cmd == "run" => program_args.push(a.clone()), + _ => usage(), + } + } + + let src = fs::read_to_string(path).unwrap_or_else(|e| die(&format!("error: cannot read {path}: {e}"))); + let fail = |d: dino::error::Diag| -> ! { die(&d.render(path, &src)) }; + + match cmd { + "tokens" => match lexer::lex(&src) { + Ok(toks) => { + for t in toks { + println!("{:>4}:{:<3} {:?}", t.span.line, t.span.col, t.tok); + } + } + Err(d) => fail(d), + }, + "parse" => match parser::parse(&src) { + Ok(p) => println!("{:#?}", p), + Err(d) => fail(d), + }, + "check" => match driver::compile_file(path) { + Ok(_) => println!("{path}: ok"), + Err(e) => die(&e), + }, + "emit-c" => match driver::compile_file(path) { + Ok(c) => print!("{c}"), + Err(e) => die(&e), + }, + "build" => { + let c = driver::compile_file(path).unwrap_or_else(|e| die(&e)); + let default = PathBuf::from(path).with_extension(""); + let out = out.unwrap_or(default); + if let Err(e) = driver::build_exe(&c, &out) { + die(&format!("error: {e}")); + } + } + "run" => { + let c = driver::compile_file(path).unwrap_or_else(|e| die(&e)); + let exe = driver::temp_exe(); + if let Err(e) = driver::build_exe(&c, &exe) { + die(&format!("error: {e}")); + } + let status = process::Command::new(&exe).args(&program_args).status(); + let _ = fs::remove_file(&exe); + match status { + Ok(s) => process::exit(s.code().unwrap_or(1)), + Err(e) => die(&format!("error: cannot run the program: {e}")), + } + } + _ => usage(), + } +} diff --git a/src/moves.rs b/src/moves.rs new file mode 100644 index 0000000..6f13521 --- /dev/null +++ b/src/moves.rs @@ -0,0 +1,156 @@ +//! Move on last use: `var b = a` copies `a`, unless `a` is never used again. +//! Then the value is handed to `b` and `a` is left empty, which saves the deep copy. +//! +//! Conservative on purpose: only plain variables declared with `var`/`const`, only when the +//! copy is the one and only mention of the variable in its statement, nothing after it +//! mentions the variable, and no loop is in the way (a loop would run the copy again). + +use crate::hir::*; +use std::collections::HashMap; + +#[derive(Default)] +struct Use { + last_seq: u32, + last_depth: u32, + count: u32, +} + +#[derive(Default)] +struct State { + seq: u32, + uses: HashMap, + decl_depth: HashMap, +} + +pub fn apply(body: &mut Vec) { + let mut st = State::default(); + scan(body, 0, &mut st); + st.seq = 0; + rewrite(body, 0, &mut st); +} + +fn has_block_match(e: &HExpr) -> bool { + let mut found = false; + walk_expr(e, &mut |_| {}, &mut |x| { + if let HExprKind::Match { arms, .. } = &x.kind { + if arms.iter().any(|a| matches!(a.body, HArmBody::Block(_))) { + found = true; + } + } + }); + found +} + +fn unit_scan(exprs: &[&HExpr], depth: u32, st: &mut State) { + st.seq += 1; + let seq = st.seq; + let poison = exprs.iter().any(|e| has_block_match(e)); + for e in exprs { + walk_expr(e, &mut |_| {}, &mut |x| { + if let HExprKind::Local(id, _) = &x.kind { + let u = st.uses.entry(*id).or_default(); + if u.last_seq != seq { + *u = Use { last_seq: seq, last_depth: depth, count: 0 }; + } + u.count += if poison { 100 } else { 1 }; + } + }); + } +} + +fn scan(stmts: &[HStmt], depth: u32, st: &mut State) { + for s in stmts { + match s { + HStmt::Let { local, init } => { + unit_scan(&[init], depth, st); + st.decl_depth.insert(local.id, depth); + } + HStmt::LetMany { locals, init } => { + unit_scan(&[init], depth, st); + for l in locals { + st.decl_depth.insert(l.id, depth); + } + } + HStmt::Assign { target, value } => unit_scan(&[target, value], depth, st), + HStmt::Expr(e) | HStmt::Return(Some(e)) => unit_scan(&[e], depth, st), + HStmt::If { cond, then, else_ } => { + unit_scan(&[cond], depth, st); + scan(then, depth, st); + scan(else_, depth, st); + } + HStmt::Loop(b) => scan(b, depth + 1, st), + HStmt::Block(b) => scan(b, depth, st), + HStmt::While { cond, body } => { + unit_scan(&[cond], depth + 1, st); + scan(body, depth + 1, st); + } + HStmt::ForRange { start, end, step, body, .. } => { + let mut es: Vec<&HExpr> = vec![start, end]; + es.extend(step.iter()); + unit_scan(&es, depth, st); + scan(body, depth + 1, st); + } + HStmt::ForEach { iter, body, .. } => { + unit_scan(&[iter], depth, st); + scan(body, depth + 1, st); + } + HStmt::Return(None) | HStmt::Break | HStmt::Continue => {} + } + } +} + +fn unit_rewrite(exprs: &mut [&mut HExpr], depth: u32, st: &mut State) { + st.seq += 1; + let seq = st.seq; + for e in exprs.iter_mut() { + movable(e, seq, depth, st); + } +} + +fn movable(e: &mut HExpr, seq: u32, depth: u32, st: &State) { + if let HExprKind::Copy(inner) = &e.kind { + if let HExprKind::Local(id, _) = &inner.kind { + let last = st.uses.get(id); + let ok = matches!(last, Some(u) if u.last_seq == seq && u.count == 1 && u.last_depth == depth) + && st.decl_depth.get(id) == Some(&depth); + if ok { + let HExprKind::Copy(inner) = std::mem::replace(&mut e.kind, HExprKind::Nil) else { unreachable!() }; + e.kind = HExprKind::Move(inner); + } + return; + } + } + children_mut(e, &mut |c| movable(c, seq, depth, st)); +} + +fn rewrite(stmts: &mut [HStmt], depth: u32, st: &mut State) { + for s in stmts { + match s { + HStmt::Let { init, .. } | HStmt::LetMany { init, .. } => unit_rewrite(&mut [init], depth, st), + HStmt::Assign { target, value } => unit_rewrite(&mut [target, value], depth, st), + HStmt::Expr(e) | HStmt::Return(Some(e)) => unit_rewrite(&mut [e], depth, st), + HStmt::If { cond, then, else_ } => { + unit_rewrite(&mut [cond], depth, st); + rewrite(then, depth, st); + rewrite(else_, depth, st); + } + HStmt::Loop(b) => rewrite(b, depth + 1, st), + HStmt::Block(b) => rewrite(b, depth, st), + HStmt::While { cond, body } => { + unit_rewrite(&mut [cond], depth + 1, st); + rewrite(body, depth + 1, st); + } + HStmt::ForRange { start, end, step, body, .. } => { + let mut es: Vec<&mut HExpr> = vec![start, end]; + es.extend(step.iter_mut()); + unit_rewrite(&mut es, depth, st); + rewrite(body, depth + 1, st); + } + HStmt::ForEach { iter, body, .. } => { + unit_rewrite(&mut [iter], depth, st); + rewrite(body, depth + 1, st); + } + HStmt::Return(None) | HStmt::Break | HStmt::Continue => {} + } + } +} diff --git a/src/parser.rs b/src/parser.rs new file mode 100644 index 0000000..fb0ac4c --- /dev/null +++ b/src/parser.rs @@ -0,0 +1,971 @@ +use crate::ast::*; +use crate::error::{Diag, PResult}; +use crate::lexer::{lex, lex_at, StrPart as LexPart, Tok, Token}; + +pub fn parse(src: &str) -> PResult { + let toks = lex(src)?; + Parser::new(toks).program() +} + +pub struct Parser { + toks: Vec, + pos: usize, + /// True while parsing `if`/`for`/`match` headers, where `Name {` starts + /// the body block rather than a struct literal (same rule as Go/Rust). + no_struct: bool, +} + +impl Parser { + pub fn new(toks: Vec) -> Parser { + Parser { toks, pos: 0, no_struct: false } + } + + // ---------- token helpers ---------- + + fn peek(&self) -> &Tok { + self.peek_n(0) + } + + fn peek_n(&self, n: usize) -> &Tok { + let i = (self.pos + n).min(self.toks.len() - 1); + &self.toks[i].tok + } + + fn span(&self) -> Span { + self.toks[self.pos.min(self.toks.len() - 1)].span + } + + fn bump(&mut self) -> Token { + let t = self.toks[self.pos.min(self.toks.len() - 1)].clone(); + if self.pos < self.toks.len() - 1 { + self.pos += 1; + } + t + } + + fn eat(&mut self, t: &Tok) -> bool { + if self.peek() == t { + self.bump(); + true + } else { + false + } + } + + fn unexpected(&self, expected: &str) -> PResult { + Err(Diag::new(format!("expected {expected}, found {}", self.peek()), self.span())) + } + + fn expect(&mut self, t: Tok, what: &str) -> PResult { + if *self.peek() == t { + Ok(self.bump().span) + } else { + self.unexpected(what) + } + } + + fn ident(&mut self) -> PResult { + match self.peek().clone() { + Tok::Ident(s) => { + self.bump(); + Ok(s) + } + _ => self.unexpected("a name"), + } + } + + fn skip_newlines(&mut self) { + while *self.peek() == Tok::Newline { + self.bump(); + } + } + + /// A statement or declaration must end at a line break or just before `}`. + fn terminator(&mut self) -> PResult<()> { + match self.peek() { + Tok::Newline => { + self.bump(); + Ok(()) + } + Tok::RBrace | Tok::RParen | Tok::Eof => Ok(()), + _ => self.unexpected("end of line"), + } + } + + /// Parse `item, item, ...` up to (and including) `close`. The opener is already consumed. + /// Line breaks and a trailing comma are allowed. Struct literals are allowed inside. + fn list( + &mut self, + close: Tok, + mut f: impl FnMut(&mut Self) -> PResult, + ) -> PResult> { + let saved = std::mem::replace(&mut self.no_struct, false); + let mut items = Vec::new(); + loop { + self.skip_newlines(); + if *self.peek() == close { + break; + } + items.push(f(self)?); + if self.eat(&Tok::Comma) { + continue; + } + self.skip_newlines(); + if *self.peek() == close { + break; + } + self.no_struct = saved; + return self.unexpected(&format!("`,` or {close}")); + } + self.no_struct = saved; + self.expect(close.clone(), &close.to_string())?; + Ok(items) + } + + // ---------- items ---------- + + pub fn program(&mut self) -> PResult { + let mut items = Vec::new(); + loop { + self.skip_newlines(); + if *self.peek() == Tok::Eof { + break; + } + items.push(self.item()?); + } + Ok(Program { items }) + } + + fn item(&mut self) -> PResult { + let is_pub = self.eat(&Tok::Pub); + match self.peek() { + Tok::Import if !is_pub => self.import(), + Tok::Struct => self.struct_decl(is_pub), + Tok::Enum => self.enum_decl(is_pub), + Tok::Interface => self.interface_decl(is_pub), + Tok::Func => { + self.bump(); + let name = self.ident()?; + Ok(Item::Func(self.func_rest(is_pub, name, true)?)) + } + Tok::Const => { + let mut v = self.var_decl()?; + v.is_pub = is_pub; + self.terminator()?; + Ok(Item::Const(v)) + } + Tok::Var => Err(Diag::new( + "global `var` is not allowed; use `const`, or keep the variable inside a function", + self.span(), + )), + _ => self.unexpected("a declaration (`func`, `struct`, `enum`, `interface`, `const` or `import`)"), + } + } + + fn import(&mut self) -> PResult { + let span = self.bump().span; + let first = self.ident()?; + let item = if let Tok::Str(parts) = self.peek().clone() { + self.bump(); + let path = match parts.as_slice() { + [LexPart::Text(t)] => t.clone(), + _ => return Err(Diag::new("an import path must be plain text", span)), + }; + Import { alias: Some(first), path, span } + } else { + let mut path = first; + while self.eat(&Tok::Dot) { + path.push('.'); + path.push_str(&self.ident()?); + } + Import { alias: None, path, span } + }; + self.terminator()?; + Ok(Item::Import(item)) + } + + fn type_params(&mut self) -> PResult> { + if !self.eat(&Tok::LBracket) { + return Ok(Vec::new()); + } + self.list(Tok::RBracket, |p| { + let name = p.ident()?; + let constraint = if p.eat(&Tok::Colon) { + Some(p.parse_type()?) + } else if matches!(p.peek(), Tok::Comma | Tok::RBracket) { + None + } else { + return p.unexpected("`:` and then the rule for the type, like `[T: Ordered]`"); + }; + Ok(TypeParam { name, constraint }) + }) + } + + fn struct_decl(&mut self, is_pub: bool) -> PResult { + let span = self.bump().span; + let name = self.ident()?; + let type_params = self.type_params()?; + self.expect(Tok::LBrace, "`{`")?; + let mut fields = Vec::new(); + let mut methods = Vec::new(); + loop { + self.skip_newlines(); + if self.eat(&Tok::RBrace) { + break; + } + let member_pub = self.eat(&Tok::Pub); + if self.eat(&Tok::Func) { + let name = self.ident()?; + methods.push(self.func_rest(member_pub, name, true)?); + } else { + let mut names = vec![self.ident()?]; + while self.eat(&Tok::Comma) { + names.push(self.ident()?); + } + self.expect(Tok::Colon, "`:` and then the type of the field, like `name: string`")?; + let ty = self.parse_type()?; + for name in names { + fields.push(Field { is_pub: member_pub, name, ty: ty.clone() }); + } + self.eat(&Tok::Comma); + } + self.terminator()?; + } + self.terminator()?; + Ok(Item::Struct(StructDecl { is_pub, name, type_params, fields, methods, span })) + } + + fn enum_decl(&mut self, is_pub: bool) -> PResult { + let span = self.bump().span; + let name = self.ident()?; + let type_params = self.type_params()?; + self.expect(Tok::LBrace, "`{`")?; + let mut variants = Vec::new(); + let mut methods = Vec::new(); + loop { + self.skip_newlines(); + if self.eat(&Tok::RBrace) { + break; + } + let member_pub = self.eat(&Tok::Pub); + if self.eat(&Tok::Func) { + let mname = self.ident()?; + methods.push(self.func_rest(member_pub, mname, true)?); + } else { + let vname = self.ident()?; + let fields = if *self.peek() == Tok::LParen { self.params()? } else { Vec::new() }; + variants.push(Variant { name: vname, fields }); + self.eat(&Tok::Comma); + } + self.terminator()?; + } + self.terminator()?; + Ok(Item::Enum(EnumDecl { is_pub, name, type_params, variants, methods, span })) + } + + fn interface_decl(&mut self, is_pub: bool) -> PResult { + let span = self.bump().span; + let name = self.ident()?; + let type_params = self.type_params()?; + self.expect(Tok::LBrace, "`{`")?; + let mut methods = Vec::new(); + loop { + self.skip_newlines(); + if self.eat(&Tok::RBrace) { + break; + } + let mname = self.ident()?; + methods.push(self.func_rest(true, mname, false)?); + self.terminator()?; + } + self.terminator()?; + Ok(Item::Interface(InterfaceDecl { is_pub, name, type_params, methods, span })) + } + + /// Everything in a function after its name: `[T](params) ret { body }`. + fn func_rest(&mut self, is_pub: bool, name: String, has_body: bool) -> PResult { + let span = self.span(); + let type_params = self.type_params()?; + let params = self.params()?; + let ret = self.ret_type()?; + if ret.is_none() && matches!(self.peek(), Tok::Ident(_) | Tok::LBracket | Tok::LParen) { + return self.unexpected("`:` before the return type, like `func area(): float`"); + } + let body = if has_body { + if *self.peek() != Tok::LBrace { + return self.unexpected("`{` to start the function body"); + } + Some(self.block()?) + } else { + None + }; + Ok(FuncDecl { is_pub, name, type_params, params, ret, body, span }) + } + + /// `(a: int, b, c: string)`: names without a type take the next type. + fn params(&mut self) -> PResult> { + self.expect(Tok::LParen, "`(`")?; + let raw = self.list(Tok::RParen, |p| { + let span = p.span(); + let name = p.ident()?; + let ty = if p.eat(&Tok::Colon) { + Some(p.parse_type()?) + } else if matches!(p.peek(), Tok::Comma | Tok::RParen) { + None + } else { + return p.unexpected("`:` and then the type, like `name: int`"); + }; + Ok((name, ty, span)) + })?; + let mut out = Vec::new(); + let mut carry: Option = None; + for (name, ty, span) in raw.into_iter().rev() { + let ty = match ty.or_else(|| carry.clone()) { + Some(t) => t, + None => return Err(Diag::new(format!("parameter `{name}` needs a type"), span)), + }; + carry = Some(ty.clone()); + out.push(Param { name, ty }); + } + out.reverse(); + Ok(out) + } + + fn ret_type(&mut self) -> PResult> { + if !self.eat(&Tok::Colon) { + return Ok(None); + } + if self.eat(&Tok::LParen) { + let mut tys = self.list(Tok::RParen, |p| p.parse_type())?; + return Ok(Some(if tys.len() == 1 { tys.remove(0) } else { Type::Tuple(tys) })); + } + Ok(Some(self.parse_type()?)) + } + + // ---------- types ---------- + + pub fn parse_type(&mut self) -> PResult { + let mut t = self.type_base()?; + while self.eat(&Tok::Question) { + t = Type::Optional(Box::new(t)); + } + Ok(t) + } + + fn type_base(&mut self) -> PResult { + match self.peek().clone() { + Tok::Shared => { + self.bump(); + Ok(Type::Shared(Box::new(self.type_base()?))) + } + Tok::Weak => { + self.bump(); + Ok(Type::Weak(Box::new(self.type_base()?))) + } + Tok::LBracket => { + self.bump(); + self.expect(Tok::RBracket, "`]` (slice types are written `[]T`)")?; + Ok(Type::Slice(Box::new(self.type_base()?))) + } + Tok::Func => { + self.bump(); + self.expect(Tok::LParen, "`(`")?; + let params = self.list(Tok::RParen, |p| p.parse_type())?; + let ret = self.ret_type()?.map(Box::new); + Ok(Type::Func { params, ret }) + } + Tok::Ident(first) => { + self.bump(); + let mut path = vec![first]; + while *self.peek() == Tok::Dot { + self.bump(); + path.push(self.ident()?); + } + let args = if self.eat(&Tok::LBracket) { + self.list(Tok::RBracket, |p| p.parse_type())? + } else { + Vec::new() + }; + Ok(Type::Named { path, args }) + } + _ => self.unexpected("a type"), + } + } + + // ---------- statements ---------- + + fn block(&mut self) -> PResult { + let span = self.expect(Tok::LBrace, "`{`")?; + let saved = std::mem::replace(&mut self.no_struct, false); + let mut stmts = Vec::new(); + loop { + self.skip_newlines(); + if *self.peek() == Tok::RBrace { + break; + } + if *self.peek() == Tok::Eof { + self.no_struct = saved; + return Err(Diag::new("this `{` is never closed", span)); + } + stmts.push(self.stmt()?); + self.terminator()?; + } + self.bump(); + self.no_struct = saved; + Ok(Block { stmts, span }) + } + + fn stmt(&mut self) -> PResult { + let span = self.span(); + match self.peek() { + Tok::Var | Tok::Const => Ok(Stmt::Var(self.var_decl()?)), + Tok::If => self.if_stmt(), + Tok::For => self.for_stmt(), + Tok::LBrace => Ok(Stmt::Block(self.block()?)), + Tok::Return => { + self.bump(); + let mut values = Vec::new(); + if !matches!(self.peek(), Tok::Newline | Tok::RBrace | Tok::Eof) { + values.push(self.expr()?); + while self.eat(&Tok::Comma) { + values.push(self.expr()?); + } + } + Ok(Stmt::Return { values, span }) + } + Tok::Break => { + self.bump(); + Ok(Stmt::Break(span)) + } + Tok::Continue => { + self.bump(); + Ok(Stmt::Continue(span)) + } + _ => { + let target = self.expr()?; + let op = match self.peek() { + Tok::Assign => Some(AssignOp::Set), + Tok::PlusEq => Some(AssignOp::Add), + Tok::MinusEq => Some(AssignOp::Sub), + Tok::StarEq => Some(AssignOp::Mul), + Tok::SlashEq => Some(AssignOp::Div), + Tok::PercentEq => Some(AssignOp::Rem), + _ => None, + }; + match op { + Some(op) => { + self.bump(); + let value = self.expr()?; + Ok(Stmt::Assign { target, op, value, span }) + } + None => Ok(Stmt::Expr(target)), + } + } + } + } + + fn var_decl(&mut self) -> PResult { + let tok = self.bump(); + let is_const = tok.tok == Tok::Const; + let mut names = vec![self.ident()?]; + while self.eat(&Tok::Comma) { + names.push(self.ident()?); + } + let ty = if self.eat(&Tok::Colon) { + Some(self.parse_type()?) + } else if matches!(self.peek(), Tok::Assign | Tok::Newline | Tok::RBrace | Tok::Eof) { + None + } else { + return self.unexpected("`:` and then the type, like `var x: int`"); + }; + let value = if self.eat(&Tok::Assign) { Some(self.expr()?) } else { None }; + if is_const && value.is_none() { + return Err(Diag::new("a `const` needs a value, e.g. `const N = 5`", tok.span)); + } + if ty.is_none() && value.is_none() { + return Err(Diag::new("a `var` needs a type or a value, e.g. `var x = 1`", tok.span)); + } + if names.len() > 1 && ty.is_some() { + return Err(Diag::new("a type annotation works with a single name only", tok.span)); + } + Ok(VarDecl { is_pub: false, is_const, names, ty, value, span: tok.span }) + } + + /// Parse an expression with struct literals disabled (for headers like `if cond {`). + fn header_expr(&mut self) -> PResult { + let saved = std::mem::replace(&mut self.no_struct, true); + let e = self.expr(); + self.no_struct = saved; + e + } + + fn if_stmt(&mut self) -> PResult { + let span = self.bump().span; + let cond = self.header_expr()?; + let then = self.block()?; + let else_ = if self.eat(&Tok::Else) { + if *self.peek() == Tok::If { + Some(Box::new(self.if_stmt()?)) + } else { + Some(Box::new(Stmt::Block(self.block()?))) + } + } else { + None + }; + Ok(Stmt::If { cond, then, else_, span }) + } + + fn for_stmt(&mut self) -> PResult { + let span = self.bump().span; + let is_in = matches!( + (self.peek_n(0), self.peek_n(1), self.peek_n(2), self.peek_n(3)), + (Tok::Ident(_), Tok::In, _, _) | (Tok::Ident(_), Tok::Comma, Tok::Ident(_), Tok::In) + ); + let kind = if *self.peek() == Tok::LBrace { + ForKind::Infinite + } else if is_in { + let mut vars = vec![self.ident()?]; + if self.eat(&Tok::Comma) { + vars.push(self.ident()?); + } + self.expect(Tok::In, "`in`")?; + let saved = std::mem::replace(&mut self.no_struct, true); + let iter = self.expr(); + let step = match (&iter, self.peek()) { + (Ok(_), Tok::Ident(s)) if s == "step" => { + self.bump(); + Some(self.expr()) + } + _ => None, + }; + self.no_struct = saved; + let iter = iter?; + let step = step.transpose()?; + ForKind::In { vars, iter, step } + } else { + ForKind::While(self.header_expr()?) + }; + let body = self.block()?; + Ok(Stmt::For { kind, body, span }) + } + + // ---------- expressions ---------- + + pub fn expr(&mut self) -> PResult { + let span = self.span(); + if *self.peek() == Tok::DotDot { + self.bump(); + let end = if self.starts_expr() { Some(Box::new(self.binary(1)?)) } else { None }; + return Ok(Expr { kind: ExprKind::Range { start: None, end }, span }); + } + let lhs = self.binary(1)?; + if *self.peek() == Tok::DotDot { + self.bump(); + let end = if self.starts_expr() { Some(Box::new(self.binary(1)?)) } else { None }; + return Ok(Expr { kind: ExprKind::Range { start: Some(Box::new(lhs)), end }, span }); + } + Ok(lhs) + } + + fn starts_expr(&self) -> bool { + matches!( + self.peek(), + Tok::Ident(_) + | Tok::Int(_) + | Tok::Float(_) + | Tok::Str(_) + | Tok::Char(_) + | Tok::LParen + | Tok::LBracket + | Tok::Minus + | Tok::Bang + | Tok::Nil + | Tok::True + | Tok::False + | Tok::Func + | Tok::Match + | Tok::Shared + | Tok::Weak + ) + } + + fn binop(tok: &Tok) -> Option<(BinOp, u8)> { + Some(match tok { + Tok::OrOr => (BinOp::Or, 1), + Tok::AndAnd => (BinOp::And, 2), + Tok::EqEq => (BinOp::Eq, 3), + Tok::NotEq => (BinOp::Ne, 3), + Tok::Lt => (BinOp::Lt, 4), + Tok::Gt => (BinOp::Gt, 4), + Tok::Le => (BinOp::Le, 4), + Tok::Ge => (BinOp::Ge, 4), + Tok::Plus => (BinOp::Add, 5), + Tok::Minus => (BinOp::Sub, 5), + Tok::Star => (BinOp::Mul, 6), + Tok::Slash => (BinOp::Div, 6), + Tok::Percent => (BinOp::Rem, 6), + _ => return None, + }) + } + + /// Precedence climbing. All binary operators are left-associative. + fn binary(&mut self, min_prec: u8) -> PResult { + let mut lhs = self.unary()?; + while let Some((op, prec)) = Self::binop(self.peek()) { + if prec < min_prec { + break; + } + self.bump(); + let rhs = self.binary(prec + 1)?; + let span = lhs.span; + lhs = Expr { kind: ExprKind::Binary { op, lhs: Box::new(lhs), rhs: Box::new(rhs) }, span }; + } + Ok(lhs) + } + + fn unary(&mut self) -> PResult { + let span = self.span(); + let kind = match self.peek() { + Tok::Minus => { + self.bump(); + ExprKind::Unary { op: UnOp::Neg, expr: Box::new(self.unary()?) } + } + Tok::Bang => { + self.bump(); + ExprKind::Unary { op: UnOp::Not, expr: Box::new(self.unary()?) } + } + Tok::Shared => { + self.bump(); + ExprKind::Shared(Box::new(self.unary()?)) + } + Tok::Weak => { + self.bump(); + ExprKind::Weak(Box::new(self.unary()?)) + } + _ => return self.postfix(), + }; + Ok(Expr { kind, span }) + } + + /// At `(`: is this `(a, b) =>`? + fn lambda_params_follow(&self) -> bool { + let mut i = 1; + loop { + match self.peek_n(i) { + Tok::RParen => return *self.peek_n(i + 1) == Tok::FatArrow, + Tok::Ident(_) => { + i += 1; + match self.peek_n(i) { + Tok::Comma => i += 1, + Tok::RParen => {} + _ => return false, + } + } + _ => return false, + } + } + } + + /// At `[` after a capitalized name: is this `Stack[int]` (types, then `{` or `.`) rather than an index? + fn generic_args_follow(&mut self, e: &Expr) -> bool { + let last = match &e.kind { + ExprKind::Ident(n) => n, + ExprKind::Field { name, .. } => name, + _ => return false, + }; + if !last.chars().next().map_or(false, |c| c.is_uppercase()) || !Self::is_path(e) { + return false; + } + let saved = self.pos; + self.bump(); // `[` + let ok = self.list(Tok::RBracket, |p| p.parse_type()).is_ok() + && match self.peek() { + Tok::Dot => true, + Tok::LBrace => !self.no_struct, + _ => false, + }; + self.pos = saved; + ok + } + + fn is_path(e: &Expr) -> bool { + match &e.kind { + ExprKind::Ident(_) | ExprKind::Generic { .. } => true, + ExprKind::Field { expr, .. } | ExprKind::Index { expr, .. } => Self::is_path(expr), + _ => false, + } + } + + fn postfix(&mut self) -> PResult { + let mut e = self.primary()?; + loop { + let span = e.span; + match self.peek().clone() { + Tok::LParen => { + self.bump(); + let args = self.list(Tok::RParen, |p| p.expr())?; + e = Expr { kind: ExprKind::Call { callee: Box::new(e), args }, span }; + } + Tok::Dot => { + self.bump(); + let name = self.ident()?; + e = Expr { kind: ExprKind::Field { expr: Box::new(e), name }, span }; + } + Tok::LBracket if self.generic_args_follow(&e) => { + self.bump(); + let args = self.list(Tok::RBracket, |p| p.parse_type())?; + e = Expr { kind: ExprKind::Generic { expr: Box::new(e), args }, span }; + } + Tok::LBracket => { + self.bump(); + let saved = std::mem::replace(&mut self.no_struct, false); + self.skip_newlines(); + let index = self.expr(); + self.no_struct = saved; + let index = index?; + self.expect(Tok::RBracket, "`]`")?; + e = Expr { kind: ExprKind::Index { expr: Box::new(e), index: Box::new(index) }, span }; + } + Tok::Question => { + self.bump(); + e = Expr { kind: ExprKind::Try(Box::new(e)), span }; + } + Tok::LBrace if !self.no_struct && Self::is_path(&e) => { + self.bump(); + let fields = self.list(Tok::RBrace, |p| { + if matches!(p.peek(), Tok::Ident(_)) && *p.peek_n(1) == Tok::Colon { + let name = p.ident()?; + p.bump(); // `:` + Ok(FieldInit { name: Some(name), value: p.expr()? }) + } else { + Ok(FieldInit { name: None, value: p.expr()? }) + } + })?; + e = Expr { kind: ExprKind::StructLit { ty: Box::new(e), fields }, span }; + } + _ => break, + } + } + Ok(e) + } + + /// The `key: value, ...` inside a map literal, after its `{`. + fn map_entries(&mut self) -> PResult> { + self.list(Tok::RBrace, |p| { + let key = p.expr()?; + p.expect(Tok::Colon, "`:` (a map entry is written `key: value`)")?; + let value = p.expr()?; + Ok((key, value)) + }) + } + + fn primary(&mut self) -> PResult { + let span = self.span(); + let kind = match self.peek().clone() { + Tok::Int(n) => { + self.bump(); + ExprKind::Int(n) + } + Tok::Float(x) => { + self.bump(); + ExprKind::Float(x) + } + Tok::True => { + self.bump(); + ExprKind::Bool(true) + } + Tok::False => { + self.bump(); + ExprKind::Bool(false) + } + Tok::Nil => { + self.bump(); + ExprKind::Nil + } + Tok::Ident(name) if *self.peek_n(1) == Tok::FatArrow => { + self.bump(); + self.bump(); + let body = self.expr()?; + ExprKind::Lambda { params: vec![name], body: Box::new(body) } + } + Tok::LParen if self.lambda_params_follow() => { + self.bump(); + let mut params = Vec::new(); + while let Tok::Ident(n) = self.peek().clone() { + self.bump(); + params.push(n); + if !self.eat(&Tok::Comma) { + break; + } + } + self.expect(Tok::RParen, "`)`")?; + self.expect(Tok::FatArrow, "`=>`")?; + let body = self.expr()?; + ExprKind::Lambda { params, body: Box::new(body) } + } + Tok::Ident(s) if s == "map" && *self.peek_n(1) == Tok::LBracket => { + self.bump(); + self.bump(); + let key = self.parse_type()?; + self.expect(Tok::Comma, "`,` (maps are written `map[key, value]`)")?; + let val = self.parse_type()?; + self.expect(Tok::RBracket, "`]`")?; + self.expect(Tok::LBrace, "`{` (for example `map[string, int]{}`)")?; + let entries = self.map_entries()?; + ExprKind::MapLit { types: Some((key, val)), entries } + } + Tok::LBrace => { + self.bump(); + let entries = self.map_entries()?; + ExprKind::MapLit { types: None, entries } + } + Tok::Ident(s) => { + self.bump(); + ExprKind::Ident(s) + } + Tok::Str(parts) => { + self.bump(); + ExprKind::Str(self.string_parts(parts)?) + } + Tok::Char(c) => { + self.bump(); + ExprKind::Char(c) + } + Tok::LParen => { + self.bump(); + let saved = std::mem::replace(&mut self.no_struct, false); + self.skip_newlines(); + let e = self.expr(); + self.no_struct = saved; + let e = e?; + self.skip_newlines(); + self.expect(Tok::RParen, "`)`")?; + return Ok(e); + } + Tok::LBracket => { + self.bump(); + ExprKind::Array(self.list(Tok::RBracket, |p| p.expr())?) + } + Tok::Func => { + self.bump(); + let params = self.params()?; + let ret = self.ret_type()?; + let body = self.block()?; + ExprKind::Closure { params, ret, body } + } + Tok::Match => return self.match_expr(), + _ => return self.unexpected("an expression"), + }; + Ok(Expr { kind, span }) + } + + fn string_parts(&mut self, parts: Vec) -> PResult> { + let mut out = Vec::new(); + for part in parts { + match part { + LexPart::Text(t) => out.push(StrPart::Text(t)), + LexPart::Code { src, span } => { + let toks = lex_at(&src, span.line, span.col)?; + let mut sub = Parser::new(toks); + let e = sub.expr()?; + sub.skip_newlines(); + if *sub.peek() != Tok::Eof { + return sub.unexpected("the end of the `{...}` in this string"); + } + out.push(StrPart::Expr(e)); + } + } + } + Ok(out) + } + + fn match_expr(&mut self) -> PResult { + let span = self.bump().span; + let scrutinee = self.header_expr()?; + self.expect(Tok::LBrace, "`{`")?; + let saved = std::mem::replace(&mut self.no_struct, false); + let mut arms = Vec::new(); + loop { + self.skip_newlines(); + if *self.peek() == Tok::RBrace { + break; + } + let arm_span = self.span(); + let pattern = self.pattern()?; + self.expect(Tok::FatArrow, "`=>`")?; + let body = match self.peek() { + Tok::LBrace => ArmBody::Block(self.block()?), + Tok::Return | Tok::Break | Tok::Continue => { + let s = self.stmt()?; + ArmBody::Block(Block { stmts: vec![s], span: self.span() }) + } + _ => ArmBody::Expr(self.expr()?), + }; + arms.push(MatchArm { span: arm_span, pattern, body }); + self.eat(&Tok::Comma); + if *self.peek() != Tok::RBrace { + self.terminator()?; + } + } + self.bump(); + self.no_struct = saved; + Ok(Expr { kind: ExprKind::Match { scrutinee: Box::new(scrutinee), arms }, span }) + } + + fn pattern(&mut self) -> PResult { + match self.peek().clone() { + Tok::Ident(name) => { + self.bump(); + let mut name = name; + while self.eat(&Tok::Dot) { + name.push('.'); + name.push_str(&self.ident()?); + } + if name == "_" { + Ok(Pattern::Wildcard) + } else if self.eat(&Tok::LParen) { + let args = self.list(Tok::RParen, |p| p.pattern())?; + Ok(Pattern::Variant { name, args }) + } else { + Ok(Pattern::Binding(name)) + } + } + Tok::Int(n) => { + self.bump(); + Ok(Pattern::Int(n)) + } + Tok::Char(c) => { + self.bump(); + Ok(Pattern::Char(c)) + } + Tok::Minus if matches!(self.peek_n(1), Tok::Int(_)) => { + self.bump(); + match self.bump().tok { + Tok::Int(n) => Ok(Pattern::Int(-n)), + _ => unreachable!(), + } + } + Tok::Str(parts) => { + let span = self.bump().span; + match parts.as_slice() { + [] => Ok(Pattern::Str(String::new())), + [LexPart::Text(t)] => Ok(Pattern::Str(t.clone())), + _ => Err(Diag::new("a pattern string cannot contain `{...}`", span)), + } + } + Tok::True => { + self.bump(); + Ok(Pattern::Bool(true)) + } + Tok::False => { + self.bump(); + Ok(Pattern::Bool(false)) + } + Tok::Nil => { + self.bump(); + Ok(Pattern::Nil) + } + _ => self.unexpected("a pattern"), + } + } +} diff --git a/src/runtime.c b/src/runtime.c new file mode 100644 index 0000000..83222d8 --- /dev/null +++ b/src/runtime.c @@ -0,0 +1,610 @@ +/* Dino runtime (generated programs include this verbatim). */ +#define _POSIX_C_SOURCE 200809L +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/* A string: bytes, length, and a reference count for strings built at runtime + (NULL for literals, which live forever). */ +typedef struct { const char *p; int64_t n; int64_t *rc; } dstr; + +static void dino_panic(const char *msg) { + fflush(stdout); + fprintf(stderr, "panic: %s\n", msg); + exit(2); +} + +static void dino_index_panic(int64_t i, int64_t n) { + fflush(stdout); + fprintf(stderr, "panic: index %lld is out of range (the length is %lld)\n", (long long)i, (long long)n); + exit(2); +} + +static void dino_slice_panic(int64_t lo, int64_t hi, int64_t n) { + fflush(stdout); + fprintf(stderr, "panic: slice %lld..%lld is out of range (the length is %lld)\n", (long long)lo, (long long)hi, (long long)n); + exit(2); +} + +static inline dstr dino_lit(const char *p, int64_t n) { dstr s = {p, n, NULL}; return s; } + +/* A new heap string of n bytes (plus a trailing 0), counted once. */ +static char *dino_str_alloc(int64_t n, int64_t **rc) { + int64_t *blk = (int64_t *)malloc(sizeof(int64_t) + (size_t)n + 1); + if (!blk) dino_panic("out of memory"); + *blk = 1; + *rc = blk; + return (char *)(blk + 1); +} + +static inline dstr dino_str_retain(dstr s) { if (s.rc) ++*s.rc; return s; } +static inline void dino_str_release(dstr s) { if (s.rc && --*s.rc == 0) free(s.rc); } + +static inline int64_t dino_div(int64_t a, int64_t b) { + if (b == 0) dino_panic("integer division by zero"); + if (b == -1) return (int64_t)(0 - (uint64_t)a); + return a / b; +} + +static inline int64_t dino_rem(int64_t a, int64_t b) { + if (b == 0) dino_panic("integer remainder by zero"); + if (b == -1) return 0; + return a % b; +} + +static inline int64_t dino_f2i(double x) { + if (x != x) return 0; + if (x >= 9223372036854775807.0) return INT64_MAX; + if (x <= -9223372036854775808.0) return INT64_MIN; + return (int64_t)x; +} + +static dstr dino_cat_n(int n, const dstr *parts) { + int64_t total = 0; + for (int i = 0; i < n; i++) total += parts[i].n; + int64_t *rc; + char *buf = dino_str_alloc(total, &rc); + int64_t at = 0; + for (int i = 0; i < n; i++) { + if (parts[i].n) memcpy(buf + at, parts[i].p, (size_t)parts[i].n); + at += parts[i].n; + } + buf[total] = 0; + dstr r = {buf, total, rc}; + return r; +} + +static inline dstr dino_cat(dstr a, dstr b) { + dstr parts[2] = {a, b}; + return dino_cat_n(2, parts); +} + +static inline bool dino_streq(dstr a, dstr b) { + return a.n == b.n && (a.n == 0 || memcmp(a.p, b.p, (size_t)a.n) == 0); +} + +/// Decode the UTF-8 character at `p` (at most `n` bytes left); stores it in `*out` and returns its width in bytes. +/// Bytes that aren't valid UTF-8 come out one at a time as U+FFFD. +static int64_t dino_utf8_decode(const char *p, int64_t n, uint32_t *out) { + const unsigned char *s = (const unsigned char *)p; + if (s[0] < 0x80) { *out = s[0]; return 1; } + int64_t w = s[0] >= 0xF0 ? 4 : s[0] >= 0xE0 ? 3 : s[0] >= 0xC0 ? 2 : 0; + if (w == 0 || w > n) { *out = 0xFFFD; return 1; } + uint32_t c = s[0] & (0xFF >> (w + 1)); + for (int64_t i = 1; i < w; i++) { + if ((s[i] & 0xC0) != 0x80) { *out = 0xFFFD; return 1; } + c = (c << 6) | (s[i] & 0x3F); + } + *out = c; + return w; +} + +/// `s[i]`: the character that starts at byte `i`. +static uint32_t dino_str_char_at(dstr s, int64_t i) { + if (i < 0 || i >= s.n) dino_index_panic(i, s.n); + if (((unsigned char)s.p[i] & 0xC0) == 0x80) dino_panic("that position is in the middle of a character"); + uint32_t c; + dino_utf8_decode(s.p + i, s.n - i, &c); + return c; +} + +static dstr dino_new_str(const char *src, int64_t n); + +static dstr dino_char_str(uint32_t c) { + char b[4]; + int n; + if (c < 0x80) { b[0] = (char)c; n = 1; } + else if (c < 0x800) { b[0] = (char)(0xC0 | (c >> 6)); b[1] = (char)(0x80 | (c & 0x3F)); n = 2; } + else if (c < 0x10000) { b[0] = (char)(0xE0 | (c >> 12)); b[1] = (char)(0x80 | ((c >> 6) & 0x3F)); b[2] = (char)(0x80 | (c & 0x3F)); n = 3; } + else { b[0] = (char)(0xF0 | (c >> 18)); b[1] = (char)(0x80 | ((c >> 12) & 0x3F)); b[2] = (char)(0x80 | ((c >> 6) & 0x3F)); b[3] = (char)(0x80 | (c & 0x3F)); n = 4; } + return dino_new_str(b, n); +} + +static void dino_print_char(uint32_t c) { + dstr s = dino_char_str(c); + fwrite(s.p, 1, (size_t)s.n, stdout); + if (s.rc && --*s.rc == 0) free(s.rc); +} + +static bool dino_char_is_digit(uint32_t c) { return c >= '0' && c <= '9'; } +static bool dino_char_is_upper(uint32_t c) { return (c >= 'A' && c <= 'Z') || (c >= 0xC0 && c <= 0xDE && c != 0xD7); } +static bool dino_char_is_lower(uint32_t c) { return (c >= 'a' && c <= 'z') || (c >= 0xDF && c <= 0xFF && c != 0xF7); } +static bool dino_char_is_letter(uint32_t c) { return dino_char_is_upper(c) || dino_char_is_lower(c) || c > 0xFF; } +static bool dino_char_is_alnum(uint32_t c) { return dino_char_is_digit(c) || dino_char_is_letter(c); } +static bool dino_char_is_space(uint32_t c) { return c == ' ' || (c >= '\t' && c <= '\r'); } +static uint32_t dino_char_upper(uint32_t c) { return (c >= 'a' && c <= 'z') ? c - 32 : c; } +static uint32_t dino_char_lower(uint32_t c) { return (c >= 'A' && c <= 'Z') ? c + 32 : c; } + +static inline uint64_t dino_hash_i64(int64_t v) { + uint64_t x = (uint64_t)v; + x ^= x >> 33; x *= 0xff51afd7ed558ccdULL; x ^= x >> 33; x *= 0xc4ceb9fe1a85ec53ULL; x ^= x >> 33; + return x; +} + +static uint64_t dino_hash_str(dstr s) { + uint64_t h = 1469598103934665603ULL; + for (int64_t i = 0; i < s.n; i++) { h ^= (unsigned char)s.p[i]; h *= 1099511628211ULL; } + return h; +} + +static int dino_strcmp(dstr a, dstr b) { + int64_t m = a.n < b.n ? a.n : b.n; + int c = m ? memcmp(a.p, b.p, (size_t)m) : 0; + if (c) return c; + return a.n < b.n ? -1 : (a.n > b.n ? 1 : 0); +} + +static int dino_fmt_f64(char *buf, size_t cap, double v) { + int n = snprintf(buf, cap, "%.15g", v); + if (!strpbrk(buf, ".eEn")) { /* looks like an integer: show it as a float */ + buf[n++] = '.'; + buf[n++] = '0'; + buf[n] = 0; + } + return n; +} + +static dstr dino_heap_str(const char *tmp, int n) { + int64_t *rc; + char *buf = dino_str_alloc(n, &rc); + memcpy(buf, tmp, (size_t)n + 1); + dstr r = {buf, n, rc}; + return r; +} + +static dstr dino_i64_str(int64_t v) { + char tmp[32]; + int n = snprintf(tmp, sizeof tmp, "%lld", (long long)v); + return dino_heap_str(tmp, n); +} + +static dstr dino_f64_str(double v) { + char tmp[64]; + int n = dino_fmt_f64(tmp, sizeof tmp, v); + return dino_heap_str(tmp, n); +} + +static inline dstr dino_bool_str(bool v) { return v ? dino_lit("true", 4) : dino_lit("false", 5); } + +static inline void dino_print_str(dstr s) { if (s.n) fwrite(s.p, 1, (size_t)s.n, stdout); } +static inline void dino_print_i64(int64_t v) { printf("%lld", (long long)v); } +static inline void dino_print_f64(double v) { char tmp[64]; dino_fmt_f64(tmp, sizeof tmp, v); fputs(tmp, stdout); } +static inline void dino_print_bool(bool v) { fputs(v ? "true" : "false", stdout); } +static inline void dino_print_sp(void) { putchar(' '); } +static inline void dino_print_nl(void) { putchar('\n'); } + +/* ---- sized numbers ---- */ +static inline int64_t dino_f2i_clamp(double x, int64_t lo, int64_t hi) { + if (x != x) return 0; + if (x <= (double)lo) return lo; + if (x >= (double)hi) return hi; + return (int64_t)x; +} + +static inline uint64_t dino_f2u64(double x) { + if (x != x || x <= 0) return 0; + if (x >= 18446744073709551615.0) return UINT64_MAX; + return (uint64_t)x; +} + +static inline uint64_t dino_udiv(uint64_t a, uint64_t b) { + if (b == 0) dino_panic("integer division by zero"); + return a / b; +} + +static inline uint64_t dino_urem(uint64_t a, uint64_t b) { + if (b == 0) dino_panic("integer remainder by zero"); + return a % b; +} + +/* the shortest text that reads back as the same f32 */ +static int dino_fmt_f32(char *buf, size_t cap, float v) { + int n = 0; + for (int p = 1; p <= 9; p++) { + n = snprintf(buf, cap, "%.*g", p, (double)v); + if ((float)strtod(buf, NULL) == v) break; + } + if (!strpbrk(buf, ".eEn")) { + buf[n++] = '.'; + buf[n++] = '0'; + buf[n] = 0; + } + return n; +} + +static dstr dino_f32_str(float v) { + char tmp[64]; + int n = dino_fmt_f32(tmp, sizeof tmp, v); + return dino_heap_str(tmp, n); +} + +static dstr dino_u64_str(uint64_t v) { + char tmp[32]; + int n = snprintf(tmp, sizeof tmp, "%llu", (unsigned long long)v); + return dino_heap_str(tmp, n); +} + +static inline void dino_print_u64(uint64_t v) { printf("%llu", (unsigned long long)v); } +static inline void dino_print_f32(float v) { char tmp[64]; dino_fmt_f32(tmp, sizeof tmp, v); fputs(tmp, stdout); } + +/* ---- end of runtime ---- */ + +/* error: nil (set == false) or a message */ +typedef struct { bool set; dstr msg; } dino_err; +static dstr dino_err_str(dino_err e) { return e.set ? dino_str_retain(e.msg) : dino_lit("nil", 3); } +static inline dino_err dino_err_retain(dino_err e) { dino_str_retain(e.msg); return e; } +static inline void dino_err_release(dino_err e) { dino_str_release(e.msg); } + +/* ---- standard library ---- */ + +/* The list of strings is defined here so that the functions below can build one; + the generated code uses this same type for `[]string`. */ +typedef struct L_str { dstr *p; int64_t len; int64_t cap; } L_str; + +static dstr dino_new_str(const char *src, int64_t n) { + int64_t *rc; + char *buf = dino_str_alloc(n, &rc); + if (n) memcpy(buf, src, (size_t)n); + buf[n] = 0; + dstr r = {buf, n, rc}; + return r; +} + +/* `s[lo..hi]`: a copy of those bytes; out of range panics */ +static dstr dstr_slice(dstr s, int64_t lo, int64_t hi) { + if (lo < 0 || hi < lo || hi > s.n) dino_slice_panic(lo, hi, s.n); + return dino_new_str(s.p + lo, hi - lo); +} + +static dstr dino_fmt_msg(const char *fmt, dstr a, const char *extra) { + int n = snprintf(NULL, 0, fmt, (int)a.n, a.p, extra); + char *tmp = (char *)malloc((size_t)n + 1); + if (!tmp) dino_panic("out of memory"); + snprintf(tmp, (size_t)n + 1, fmt, (int)a.n, a.p, extra); + dstr r = dino_new_str(tmp, n); + free(tmp); + return r; +} + +static dino_err dino_make_err(dstr msg) { dino_err e = {true, msg}; return e; } + +/* a NUL-terminated copy of a string (for file names and the C library) */ +static char *dino_cstr(dstr s) { + char *c = (char *)malloc((size_t)s.n + 1); + if (!c) dino_panic("out of memory"); + if (s.n) memcpy(c, s.p, (size_t)s.n); + c[s.n] = 0; + return c; +} + +static void dino_strs_push(L_str *l, dstr s) { + if (l->len == l->cap) { + int64_t cap = l->cap ? l->cap * 2 : 4; + dstr *p = (dstr *)realloc(l->p, (size_t)cap * sizeof(dstr)); + if (!p) dino_panic("out of memory"); + l->p = p; + l->cap = cap; + } + l->p[l->len++] = s; +} + +static dstr dino_str_upper(dstr s) { + dstr r = dino_new_str(s.p, s.n); + for (int64_t i = 0; i < r.n; i++) if (r.p[i] >= 'a' && r.p[i] <= 'z') ((char *)r.p)[i] -= 32; + return r; +} + +static dstr dino_str_lower(dstr s) { + dstr r = dino_new_str(s.p, s.n); + for (int64_t i = 0; i < r.n; i++) if (r.p[i] >= 'A' && r.p[i] <= 'Z') ((char *)r.p)[i] += 32; + return r; +} + +static bool dino_is_space(char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == '\f'; } + +static dstr dino_str_trim(dstr s) { + int64_t a = 0, b = s.n; + while (a < b && dino_is_space(s.p[a])) a++; + while (b > a && dino_is_space(s.p[b - 1])) b--; + return dino_new_str(s.p + a, b - a); +} + +static int64_t dino_str_index_of(dstr s, dstr sub) { + if (sub.n == 0) return 0; + for (int64_t i = 0; i + sub.n <= s.n; i++) + if (memcmp(s.p + i, sub.p, (size_t)sub.n) == 0) return i; + return -1; +} + +static bool dino_str_contains(dstr s, dstr sub) { return dino_str_index_of(s, sub) >= 0; } +static bool dino_str_starts_with(dstr s, dstr p) { return p.n <= s.n && (p.n == 0 || memcmp(s.p, p.p, (size_t)p.n) == 0); } +static bool dino_str_ends_with(dstr s, dstr p) { return p.n <= s.n && (p.n == 0 || memcmp(s.p + s.n - p.n, p.p, (size_t)p.n) == 0); } + +static dstr dino_str_replace(dstr s, dstr from, dstr to) { + if (from.n == 0) return dino_new_str(s.p, s.n); + int64_t count = 0; + for (int64_t i = 0; i + from.n <= s.n;) { + if (memcmp(s.p + i, from.p, (size_t)from.n) == 0) { count++; i += from.n; } else i++; + } + int64_t total = s.n + count * (to.n - from.n); + int64_t *rc; + char *buf = dino_str_alloc(total, &rc); + int64_t at = 0; + for (int64_t i = 0; i < s.n;) { + if (i + from.n <= s.n && memcmp(s.p + i, from.p, (size_t)from.n) == 0) { + if (to.n) memcpy(buf + at, to.p, (size_t)to.n); + at += to.n; + i += from.n; + } else { + buf[at++] = s.p[i++]; + } + } + buf[total] = 0; + dstr r = {buf, total, rc}; + return r; +} + +static dstr dino_str_repeat(dstr s, int64_t n) { + if (n < 0) n = 0; + int64_t total = s.n * n; + int64_t *rc; + char *buf = dino_str_alloc(total, &rc); + for (int64_t i = 0; i < n; i++) if (s.n) memcpy(buf + i * s.n, s.p, (size_t)s.n); + buf[total] = 0; + dstr r = {buf, total, rc}; + return r; +} + +static L_str dino_str_split(dstr s, dstr sep) { + L_str out = {0}; + if (sep.n == 0) { /* one piece per character */ + for (int64_t i = 0; i < s.n;) { + int64_t j = i + 1; + while (j < s.n && (s.p[j] & 0xC0) == 0x80) j++; + dino_strs_push(&out, dino_new_str(s.p + i, j - i)); + i = j; + } + return out; + } + int64_t start = 0; + for (int64_t i = 0; i + sep.n <= s.n;) { + if (memcmp(s.p + i, sep.p, (size_t)sep.n) == 0) { + dino_strs_push(&out, dino_new_str(s.p + start, i - start)); + i += sep.n; + start = i; + } else i++; + } + dino_strs_push(&out, dino_new_str(s.p + start, s.n - start)); + return out; +} + +static L_str dino_str_lines(dstr s) { + L_str out = {0}; + int64_t start = 0; + for (int64_t i = 0; i < s.n; i++) { + if (s.p[i] == '\n') { + int64_t end = i; + if (end > start && s.p[end - 1] == '\r') end--; + dino_strs_push(&out, dino_new_str(s.p + start, end - start)); + start = i + 1; + } + } + if (start < s.n) { + int64_t end = s.n; + if (end > start && s.p[end - 1] == '\r') end--; + dino_strs_push(&out, dino_new_str(s.p + start, end - start)); + } + return out; +} + +static void dino_str_chars(dstr s, void **p, int64_t *len, int64_t *cap) { + uint32_t *out = (uint32_t *)malloc((size_t)(s.n ? s.n : 1) * sizeof(uint32_t)); + if (!out) dino_panic("out of memory"); + int64_t n = 0; + for (int64_t i = 0; i < s.n;) { + uint32_t c; + i += dino_utf8_decode(s.p + i, s.n - i, &c); + out[n++] = c; + } + *p = out; + *len = n; + *cap = s.n ? s.n : 1; +} + +static L_str dino_str_fields(dstr s) { + L_str out = {0}; + int64_t i = 0; + while (i < s.n) { + while (i < s.n && dino_is_space(s.p[i])) i++; + int64_t start = i; + while (i < s.n && !dino_is_space(s.p[i])) i++; + if (i > start) dino_strs_push(&out, dino_new_str(s.p + start, i - start)); + } + return out; +} + +static dstr dino_strs_join(L_str l, dstr sep) { + int64_t total = 0; + for (int64_t i = 0; i < l.len; i++) total += l.p[i].n + (i ? sep.n : 0); + int64_t *rc; + char *buf = dino_str_alloc(total, &rc); + int64_t at = 0; + for (int64_t i = 0; i < l.len; i++) { + if (i && sep.n) { memcpy(buf + at, sep.p, (size_t)sep.n); at += sep.n; } + if (l.p[i].n) { memcpy(buf + at, l.p[i].p, (size_t)l.p[i].n); at += l.p[i].n; } + } + buf[total] = 0; + dstr r = {buf, total, rc}; + return r; +} + +static dino_err dino_str_to_int(dstr s, int64_t *out) { + *out = 0; + char *c = dino_cstr(s); + char *end = NULL; + errno = 0; + long long v = strtoll(c, &end, 10); + bool ok = s.n > 0 && *end == 0 && !dino_is_space(c[0]); + int e = errno; + free(c); + if (!ok) return dino_make_err(dino_fmt_msg("\"%.*s\" is not a whole number%s", s, "")); + if (e == ERANGE) return dino_make_err(dino_fmt_msg("\"%.*s\" is too big for an int%s", s, "")); + *out = (int64_t)v; + dino_err none = {0}; + return none; +} + +static dino_err dino_str_to_float(dstr s, double *out) { + *out = 0; + char *c = dino_cstr(s); + char *end = NULL; + double v = strtod(c, &end); + bool ok = s.n > 0 && *end == 0 && !dino_is_space(c[0]); + free(c); + if (!ok) return dino_make_err(dino_fmt_msg("\"%.*s\" is not a number%s", s, "")); + *out = v; + dino_err none = {0}; + return none; +} + +static dino_err dino_os_read_file(dstr path, dstr *out) { + dstr empty = {0}; + *out = empty; + char *c = dino_cstr(path); + FILE *f = fopen(c, "rb"); + free(c); + if (!f) return dino_make_err(dino_fmt_msg("cannot read \"%.*s\": %s", path, strerror(errno))); + size_t cap = 4096, len = 0; + char *buf = (char *)malloc(cap); + if (!buf) dino_panic("out of memory"); + for (;;) { + size_t got = fread(buf + len, 1, cap - len, f); + len += got; + if (len < cap) break; + cap *= 2; + buf = (char *)realloc(buf, cap); + if (!buf) dino_panic("out of memory"); + } + bool bad = ferror(f); + fclose(f); + if (bad) { + free(buf); + return dino_make_err(dino_fmt_msg("cannot read \"%.*s\": %s", path, "read error")); + } + *out = dino_new_str(buf, (int64_t)len); + free(buf); + dino_err none = {0}; + return none; +} + +static dino_err dino_os_write(dstr path, dstr text, const char *mode) { + char *c = dino_cstr(path); + FILE *f = fopen(c, mode); + free(c); + if (!f) return dino_make_err(dino_fmt_msg("cannot write \"%.*s\": %s", path, strerror(errno))); + size_t put = text.n ? fwrite(text.p, 1, (size_t)text.n, f) : 0; + int closed = fclose(f); + dino_err none = {0}; + if (put != (size_t)text.n || closed != 0) return dino_make_err(dino_fmt_msg("cannot write \"%.*s\": %s", path, "write error")); + return none; +} +static dino_err dino_os_write_file(dstr path, dstr text) { return dino_os_write(path, text, "wb"); } +static dino_err dino_os_append_file(dstr path, dstr text) { return dino_os_write(path, text, "ab"); } + +static bool dino_os_exists(dstr path) { + char *c = dino_cstr(path); + bool r = access(c, F_OK) == 0; + free(c); + return r; +} + +static int dino_argc; +static char **dino_argv; +static void dino_set_args(int argc, char **argv) { dino_argc = argc; dino_argv = argv; } + +static L_str dino_os_args(void) { + L_str out = {0}; + for (int i = 0; i < dino_argc; i++) dino_strs_push(&out, dino_new_str(dino_argv[i], (int64_t)strlen(dino_argv[i]))); + return out; +} + +static void dino_os_exit(int64_t code) { + fflush(stdout); + exit((int)code); +} + +static bool dino_os_env(dstr name, dstr *out) { + dstr empty = {0}; + *out = empty; + char *c = dino_cstr(name); + const char *v = getenv(c); + free(c); + if (!v) return false; + *out = dino_new_str(v, (int64_t)strlen(v)); + return true; +} + +static bool dino_os_read_line(dstr *out) { + dstr empty = {0}; + *out = empty; + size_t cap = 128, len = 0; + char *buf = (char *)malloc(cap); + if (!buf) dino_panic("out of memory"); + int ch; + bool any = false; + while ((ch = getchar()) != EOF) { + any = true; + if (ch == '\n') break; + if (len + 1 >= cap) { + cap *= 2; + buf = (char *)realloc(buf, cap); + if (!buf) dino_panic("out of memory"); + } + buf[len++] = (char)ch; + } + if (len > 0 && buf[len - 1] == '\r') len--; + if (any) *out = dino_new_str(buf, (int64_t)len); + free(buf); + return any; +} + +static double dino_time_now(void) { + struct timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9; +} + +static void dino_time_sleep(double secs) { + if (secs <= 0) return; + fflush(stdout); + struct timespec ts; + ts.tv_sec = (time_t)secs; + ts.tv_nsec = (long)((secs - (double)ts.tv_sec) * 1e9); + nanosleep(&ts, NULL); +} diff --git a/src/stdlib.rs b/src/stdlib.rs new file mode 100644 index 0000000..6d04ef1 --- /dev/null +++ b/src/stdlib.rs @@ -0,0 +1,153 @@ +//! The built-in modules (`os`, `math`, `time`) and the methods of strings and lists. +//! This is only a table of names and types; the code is in `runtime.c`. + +use crate::hir::BuiltinShape; +use crate::types::Ty; + +/// Modules that `import` knows without a file. +pub const MODULES: &[&str] = &["os", "math", "time"]; + +pub struct BuiltinFn { + /// The C function (or, for `Min`/`Max`/`Abs`, nothing). + pub c: &'static str, + pub params: Vec, + pub ret: Ty, + pub shape: BuiltinShape, +} + +fn f(c: &'static str, params: Vec, ret: Ty, shape: BuiltinShape) -> Option { + Some(BuiltinFn { c, params, ret, shape }) +} + +fn plain(c: &'static str, params: Vec, ret: Ty) -> Option { + f(c, params, ret, BuiltinShape::Plain) +} + +fn strings() -> Ty { + Ty::list(Ty::Str) +} + +/// `(T, error)` +fn with_err(t: Ty) -> Ty { + Ty::tuple(vec![t, Ty::Error]) +} + +/// `os.read_file`, `math.sqrt`, ... +pub fn module_fn(module: &str, name: &str) -> Option { + use Ty::*; + match (module, name) { + ("os", "read_file") => f("dino_os_read_file", vec![Str], with_err(Str), BuiltinShape::ErrOut), + ("os", "write_file") => plain("dino_os_write_file", vec![Str, Str], Error), + ("os", "append_file") => plain("dino_os_append_file", vec![Str, Str], Error), + ("os", "exists") => plain("dino_os_exists", vec![Str], Bool), + ("os", "exit") => plain("dino_os_exit", vec![Int], Unit), + ("os", "env") => f("dino_os_env", vec![Str], Ty::opt(Str), BuiltinShape::OptOut), + ("os", "read_line") => f("dino_os_read_line", vec![], Ty::opt(Str), BuiltinShape::OptOut), + + ("math", "sqrt") => plain("sqrt", vec![Float], Float), + ("math", "sin") => plain("sin", vec![Float], Float), + ("math", "cos") => plain("cos", vec![Float], Float), + ("math", "tan") => plain("tan", vec![Float], Float), + ("math", "asin") => plain("asin", vec![Float], Float), + ("math", "acos") => plain("acos", vec![Float], Float), + ("math", "atan") => plain("atan", vec![Float], Float), + ("math", "atan2") => plain("atan2", vec![Float, Float], Float), + ("math", "exp") => plain("exp", vec![Float], Float), + ("math", "log") => plain("log", vec![Float], Float), + ("math", "log10") => plain("log10", vec![Float], Float), + ("math", "pow") => plain("pow", vec![Float, Float], Float), + ("math", "hypot") => plain("hypot", vec![Float, Float], Float), + ("math", "floor") => plain("floor", vec![Float], Float), + ("math", "ceil") => plain("ceil", vec![Float], Float), + ("math", "round") => plain("round", vec![Float], Float), + ("math", "trunc") => plain("trunc", vec![Float], Float), + // these work on any number type; the checker decides the type + ("math", "min") => f("", vec![], Unit, BuiltinShape::Min), + ("math", "max") => f("", vec![], Unit, BuiltinShape::Max), + ("math", "abs") => f("", vec![], Unit, BuiltinShape::Abs), + + ("time", "now") => plain("dino_time_now", vec![], Float), + ("time", "sleep") => plain("dino_time_sleep", vec![Float], Unit), + _ => None, + } +} + +/// Values in a module: `math.PI`, `os.args`. +pub enum ModuleValue { + Float(f64), + Call(BuiltinFn), +} + +pub fn module_value(module: &str, name: &str) -> Option { + match (module, name) { + ("math", "PI") => Some(ModuleValue::Float(std::f64::consts::PI)), + ("math", "E") => Some(ModuleValue::Float(std::f64::consts::E)), + ("os", "args") => plain("dino_os_args", vec![], strings()).map(ModuleValue::Call), + _ => None, + } +} + +pub fn module_names(module: &str) -> Vec<&'static str> { + let fns: &[&'static str] = match module { + "os" => &["read_file", "write_file", "append_file", "exists", "exit", "env", "read_line", "args"], + "math" => &[ + "sqrt", "sin", "cos", "tan", "asin", "acos", "atan", "atan2", "exp", "log", "log10", "pow", "hypot", + "floor", "ceil", "round", "trunc", "min", "max", "abs", "PI", "E", + ], + "time" => &["now", "sleep"], + _ => &[], + }; + fns.to_vec() +} + +/// Methods of `string`. `params[0]` is the string itself. +pub fn str_method(name: &str) -> Option { + use Ty::*; + match name { + "upper" => plain("dino_str_upper", vec![Str], Str), + "lower" => plain("dino_str_lower", vec![Str], Str), + "trim" => plain("dino_str_trim", vec![Str], Str), + "contains" => plain("dino_str_contains", vec![Str, Str], Bool), + "starts_with" => plain("dino_str_starts_with", vec![Str, Str], Bool), + "ends_with" => plain("dino_str_ends_with", vec![Str, Str], Bool), + "index_of" => plain("dino_str_index_of", vec![Str, Str], Int), + "replace" => plain("dino_str_replace", vec![Str, Str, Str], Str), + "repeat" => plain("dino_str_repeat", vec![Str, Int], Str), + "split" => plain("dino_str_split", vec![Str, Str], strings()), + "lines" => plain("dino_str_lines", vec![Str], strings()), + "fields" => plain("dino_str_fields", vec![Str], strings()), + "chars" => f("dino_str_chars", vec![Str], Ty::list(Ty::Char), BuiltinShape::ListOut), + "to_int" => f("dino_str_to_int", vec![Str], with_err(Int), BuiltinShape::ErrOut), + "to_float" => f("dino_str_to_float", vec![Str], with_err(Float), BuiltinShape::ErrOut), + _ => None, + } +} + +pub const STR_METHODS: &str = + "len, upper, lower, trim, contains, starts_with, ends_with, index_of, replace, repeat, split, lines, fields, chars, to_int, to_float"; + +/// Methods of `char`. `params[0]` is the character itself. +pub fn char_method(name: &str) -> Option { + use Ty::*; + match name { + "is_digit" => plain("dino_char_is_digit", vec![Char], Bool), + "is_letter" => plain("dino_char_is_letter", vec![Char], Bool), + "is_alnum" => plain("dino_char_is_alnum", vec![Char], Bool), + "is_space" => plain("dino_char_is_space", vec![Char], Bool), + "is_upper" => plain("dino_char_is_upper", vec![Char], Bool), + "is_lower" => plain("dino_char_is_lower", vec![Char], Bool), + "upper" => plain("dino_char_upper", vec![Char], Char), + "lower" => plain("dino_char_lower", vec![Char], Char), + _ => None, + } +} + +pub const CHAR_METHODS: &str = "is_digit, is_letter, is_alnum, is_space, is_upper, is_lower, upper, lower"; + +/// Methods of `[]string`, besides `len`, `push` and `pop`. +pub fn str_list_method(name: &str) -> Option { + match name { + "join" => plain("dino_strs_join", vec![strings(), Ty::Str], Ty::Str), + _ => None, + } +} diff --git a/src/types.rs b/src/types.rs new file mode 100644 index 0000000..1a039bd --- /dev/null +++ b/src/types.rs @@ -0,0 +1,207 @@ +use std::fmt; + +/// The sized number types. (`i64` is the same type as `int`, and `f64` as `float`.) +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Num { + I8, + I16, + I32, + U8, + U16, + U32, + U64, + F32, +} + +impl Num { + pub const ALL: [Num; 8] = [Num::I8, Num::I16, Num::I32, Num::U8, Num::U16, Num::U32, Num::U64, Num::F32]; + + pub fn name(self) -> &'static str { + match self { + Num::I8 => "i8", + Num::I16 => "i16", + Num::I32 => "i32", + Num::U8 => "u8", + Num::U16 => "u16", + Num::U32 => "u32", + Num::U64 => "u64", + Num::F32 => "f32", + } + } + + pub fn is_float(self) -> bool { + self == Num::F32 + } + + pub fn is_signed(self) -> bool { + matches!(self, Num::I8 | Num::I16 | Num::I32 | Num::F32) + } + + /// Smallest and largest value of an integer type. + pub fn range(self) -> (i128, i128) { + match self { + Num::I8 => (i8::MIN as i128, i8::MAX as i128), + Num::I16 => (i16::MIN as i128, i16::MAX as i128), + Num::I32 => (i32::MIN as i128, i32::MAX as i128), + Num::U8 => (0, u8::MAX as i128), + Num::U16 => (0, u16::MAX as i128), + Num::U32 => (0, u32::MAX as i128), + Num::U64 => (0, u64::MAX as i128), + Num::F32 => (0, 0), + } + } +} + +/// Types supported by the compiler so far. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum Ty { + Int, + Float, + Bool, + /// One character (a Unicode code point), written `'a'`. + Char, + Str, + /// A user-defined struct, by name (names are unique and interned for the whole run). + Struct(&'static str), + /// A user-defined enum, by name. + Enum(&'static str), + /// A growable list, `[]T`. + List(&'static Ty), + /// A map from keys to values, `map[K, V]`; it remembers the order entries were added in. + Map(&'static Ty, &'static Ty), + /// A sized number: `i8`, `u8`, `i32`, `f32`, ... + Num(Num), + /// The built-in `error`: either nil (no error) or a message. + Error, + /// `T?`: a `T`, or `nil`. + Opt(&'static Ty), + /// Several return values, `(A, B)`. Only function results have this type. + Tuple(&'static [Ty]), + /// `shared T`: one value with several owners, freed when the last one goes away. + Shared(&'static Ty), + /// `weak T`: points at a shared value without owning it; nil once the value is gone. + Weak(&'static Ty), + /// A value of an `interface` type: any value whose type has the interface's methods. + Iface(&'static str), + /// A function value: `func(int, string): bool`. + Func(&'static FuncTy), + /// The "type" of a call that returns nothing. + Unit, +} + +/// The parameter and result types of a function value. +#[derive(Debug, PartialEq, Eq)] +pub struct FuncTy { + pub params: Vec, + pub ret: Ty, +} + +impl Ty { + /// `[]elem`. The element type is leaked so `Ty` can stay `Copy`; the compiler is short-lived. + pub fn list(elem: Ty) -> Ty { + Ty::List(Box::leak(Box::new(elem))) + } +} + +impl Ty { + /// `int`, or a sized integer type. + pub fn is_int(self) -> bool { + matches!(self, Ty::Int) || matches!(self, Ty::Num(n) if !n.is_float()) + } + + /// `float`, or `f32`. + pub fn is_float(self) -> bool { + matches!(self, Ty::Float) || matches!(self, Ty::Num(n) if n.is_float()) + } + + pub fn is_number(self) -> bool { + self.is_int() || self.is_float() + } + + /// Can values of this type be negative (or negated)? + pub fn is_signed(self) -> bool { + match self { + Ty::Int | Ty::Float => true, + Ty::Num(n) => n.is_signed(), + _ => false, + } + } + + /// Smallest and largest value of an integer type. + pub fn int_range(self) -> (i128, i128) { + match self { + Ty::Num(n) => n.range(), + _ => (i64::MIN as i128, i64::MAX as i128), + } + } + + pub fn map(key: Ty, val: Ty) -> Ty { + Ty::Map(Box::leak(Box::new(key)), Box::leak(Box::new(val))) + } + + pub fn opt(inner: Ty) -> Ty { + Ty::Opt(Box::leak(Box::new(inner))) + } + + pub fn func(params: Vec, ret: Ty) -> Ty { + Ty::Func(Box::leak(Box::new(FuncTy { params, ret }))) + } + + pub fn shared(inner: Ty) -> Ty { + Ty::Shared(Box::leak(Box::new(inner))) + } + + pub fn weak(inner: Ty) -> Ty { + Ty::Weak(Box::leak(Box::new(inner))) + } + + pub fn tuple(items: Vec) -> Ty { + Ty::Tuple(Box::leak(items.into_boxed_slice())) + } +} + +impl fmt::Display for Ty { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + match self { + Ty::Int => f.write_str("int"), + Ty::Float => f.write_str("float"), + Ty::Bool => f.write_str("bool"), + Ty::Char => f.write_str("char"), + Ty::Str => f.write_str("string"), + Ty::Struct(name) | Ty::Enum(name) => f.write_str(name), + Ty::List(elem) => write!(f, "[]{elem}"), + Ty::Map(k, v) => write!(f, "map[{k}, {v}]"), + Ty::Num(n) => f.write_str(n.name()), + Ty::Error => f.write_str("error"), + Ty::Opt(inner) => write!(f, "{inner}?"), + Ty::Iface(name) => f.write_str(name), + Ty::Func(ft) => { + f.write_str("func(")?; + for (i, t) in ft.params.iter().enumerate() { + if i > 0 { + f.write_str(", ")?; + } + write!(f, "{t}")?; + } + f.write_str(")")?; + if ft.ret != Ty::Unit { + write!(f, ": {}", ft.ret)?; + } + Ok(()) + } + Ty::Shared(inner) => write!(f, "shared {inner}"), + Ty::Weak(inner) => write!(f, "weak {inner}"), + Ty::Tuple(items) => { + f.write_str("(")?; + for (i, t) in items.iter().enumerate() { + if i > 0 { + f.write_str(", ")?; + } + write!(f, "{t}")?; + } + f.write_str(")") + } + Ty::Unit => f.write_str("nothing"), + } + } +} diff --git a/tests/fmt.rs b/tests/fmt.rs new file mode 100644 index 0000000..668c01f --- /dev/null +++ b/tests/fmt.rs @@ -0,0 +1,106 @@ +//! Tests of `dino fmt`. + +use dino::fmt::format; + +fn fmt(src: &str) -> String { + format(src).unwrap_or_else(|d| panic!("{}", d.render("test.dino", src))) +} + +#[test] +fn fixes_indentation_and_spaces() { + let messy = "func add( a:int,b :int ):int{\nreturn a+b\n}\n\n\n\nfunc main(){\nvar x=add(1,2)*3 -1\nif x>3{print( \"big\" )}else{print(-x)}\nfor i in 0..10 step 2{ print(i)}\n}\n"; + // line breaks stay where they are: a block written on one line stays on one line + let want = "func add(a: int, b: int): int {\n return a + b\n}\n\nfunc main() {\n var x = add(1, 2) * 3 - 1\n if x > 3 { print(\"big\") } else { print(-x) }\n for i in 0..10 step 2 { print(i) }\n}\n"; + assert_eq!(fmt(messy), want); +} + +#[test] +fn literals_and_types_stay_tight() { + let src = "struct P{x,y:float}\nfunc main(){\nvar p=P{x:1.0,y:2.0}\nvar m=map[string,int]{}\nvar n={\"a\":1,\"b\":2}\nvar xs:[]int=[1,2,3]\nvar o:int?=nil\nvar s=Stack[int]{}\nprint(p,m,n,xs,o,s)\n}\n"; + let out = fmt(src); + // struct bodies are blocks; literals have no inner spaces + assert!(out.contains("var p = P{x: 1.0, y: 2.0}"), "{out}"); + assert!(out.contains("var m = map[string, int]{}"), "{out}"); + assert!(out.contains("var n = {\"a\": 1, \"b\": 2}"), "{out}"); + assert!(out.contains("var xs: []int = [1, 2, 3]"), "{out}"); + assert!(out.contains("var o: int? = nil"), "{out}"); + assert!(out.contains("var s = Stack[int]{}"), "{out}"); +} + +#[test] +fn comments_and_blank_lines_survive() { + let src = "// top\nfunc main() {\n\n\n var a = 1 // one\n var bbb = 2 // two\n\n // standalone\n print(a, bbb)\n\n}\n"; + let want = "// top\nfunc main() {\n var a = 1 // one\n var bbb = 2 // two\n\n // standalone\n print(a, bbb)\n}\n"; + assert_eq!(fmt(src), want); +} + +#[test] +fn closures_and_continuation_lines() { + let src = "func main() {\nrun(func() {\nprint(1)\n})\nvar total = 1 +\n2 +\n3\nvar xs = [\n1,\n2,\n]\n}\n"; + let want = "func main() {\n run(func() {\n print(1)\n })\n var total = 1 +\n 2 +\n 3\n var xs = [\n 1,\n 2,\n ]\n}\n"; + assert_eq!(fmt(src), want); +} + +#[test] +fn strings_are_left_alone() { + let src = "func main() {\nprint(\"a {x+1} b\", 'x', '\\n')\n}\n"; + let out = fmt(src); + assert!(out.contains("\"a {x+1} b\""), "{out}"); +} + +#[test] +fn already_formatted_code_does_not_change() { + let src = "func main() {\n var s = \"é\"\n for i, c in s {\n match c {\n 'a' => print(i)\n _ => {\n print(-i)\n }\n }\n }\n}\n"; + assert_eq!(fmt(src), src); +} + +#[test] +fn broken_code_is_refused() { + assert!(format("func main( {").is_err()); +} + +fn dino_files(dir: &std::path::Path, out: &mut Vec) { + for e in std::fs::read_dir(dir).unwrap() { + let p = e.unwrap().path(); + if p.is_dir() { + dino_files(&p, out); + } else if p.extension().map_or(false, |x| x == "dino") { + out.push(p); + } + } +} + +#[test] +fn examples_are_formatted_and_formatting_is_stable() { + let mut files = Vec::new(); + dino_files(&std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("examples"), &mut files); + assert!(files.len() > 30); + for f in files { + let src = std::fs::read_to_string(&f).unwrap(); + let once = fmt(&src); + assert_eq!(once, src, "{} is not formatted (run `dino fmt examples`)", f.display()); + assert_eq!(fmt(&once), once, "formatting {} twice differs", f.display()); + } +} + +#[test] +fn doc_examples_are_stable() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("docs"); + let mut n = 0; + for e in std::fs::read_dir(dir).unwrap() { + let p = e.unwrap().path(); + let text = std::fs::read_to_string(&p).unwrap(); + let mut rest = text.as_str(); + while let Some(start) = rest.find("```dino\n") { + let after = &rest[start + 8..]; + let end = after.find("```").unwrap(); + let code = &after[..end]; + rest = &after[end + 3..]; + if let Ok(once) = format(code) { + assert_eq!(once, code, "doc example in {} is not formatted:\n{code}", p.display()); + n += 1; + } + } + } + assert!(n > 20, "only {n} doc examples were formattable"); +} diff --git a/tests/lsp.rs b/tests/lsp.rs new file mode 100644 index 0000000..74e5066 --- /dev/null +++ b/tests/lsp.rs @@ -0,0 +1,227 @@ +//! Tests of the language server: feed it editor messages, read what it answers. + +use dino::json::{self, Json}; +use dino::lsp; + +fn frame(v: &Json) -> Vec { + let body = v.to_string(); + format!("Content-Length: {}\r\n\r\n{}", body.len(), body).into_bytes() +} + +fn request(id: i64, method: &str, params: Json) -> Json { + Json::obj(vec![("jsonrpc", Json::str("2.0")), ("id", Json::Num(id as f64)), ("method", Json::str(method)), ("params", params)]) +} + +fn notify(method: &str, params: Json) -> Json { + Json::obj(vec![("jsonrpc", Json::str("2.0")), ("method", Json::str(method)), ("params", params)]) +} + +fn open(uri: &str, text: &str) -> Json { + notify( + "textDocument/didOpen", + Json::obj(vec![("textDocument", Json::obj(vec![("uri", Json::str(uri)), ("text", Json::str(text))]))]), + ) +} + +/// Run a whole conversation and return every message the server sent. +fn talk(messages: Vec) -> (Vec, i32) { + let mut input = Vec::new(); + for m in &messages { + input.extend(frame(m)); + } + let mut output = Vec::new(); + let code = lsp::run(std::io::Cursor::new(input), &mut output); + let text = String::from_utf8(output).unwrap(); + let mut out = Vec::new(); + let mut rest = text.as_str(); + while let Some(i) = rest.find("\r\n\r\n") { + let len: usize = rest[..i].trim().strip_prefix("Content-Length: ").unwrap().parse().unwrap(); + out.push(json::parse(&rest[i + 4..i + 4 + len]).unwrap()); + rest = &rest[i + 4 + len..]; + } + (out, code) +} + +fn exit_messages() -> Vec { + vec![request(99, "shutdown", Json::Null), notify("exit", Json::Null)] +} + +#[test] +fn json_round_trip() { + let src = r#"{"a":[1,2.5,"x\n\"y\" é 😀"],"b":null,"c":true}"#; + let v = json::parse(src).unwrap(); + assert_eq!(json::parse(&v.to_string()).unwrap(), v); + assert_eq!(v.get("a"), Some(&Json::Arr(vec![Json::Num(1.0), Json::Num(2.5), Json::str("x\n\"y\" é 😀")]))); +} + +#[test] +fn initialize_announces_features() { + let mut msgs = vec![request(1, "initialize", Json::obj(vec![]))]; + msgs.extend(exit_messages()); + let (out, code) = talk(msgs); + assert_eq!(code, 0); + let caps = out[0].path(&["result", "capabilities"]).unwrap(); + assert_eq!(caps.get("documentFormattingProvider"), Some(&Json::Bool(true))); + assert_eq!(out[0].get("id"), Some(&Json::Num(1.0))); +} + +#[test] +fn errors_show_up_as_diagnostics() { + let src = "func main() {\n var x: int = \"no\"\n}\n"; + let mut msgs = vec![open("file:///tmp/dino-lsp-test/a.dino", src)]; + msgs.extend(exit_messages()); + let (out, _) = talk(msgs); + let d = match out[0].path(&["params", "diagnostics"]) { + Some(Json::Arr(items)) => items.clone(), + other => panic!("{other:?}"), + }; + assert_eq!(d.len(), 1); + let line = d[0].path(&["range", "start", "line"]).and_then(Json::as_i64).unwrap(); + assert_eq!(line, 1); + assert!(d[0].get("message").and_then(Json::as_str).unwrap().contains("string"), "{:?}", d[0]); +} + +#[test] +fn fixing_the_code_clears_the_diagnostics() { + let uri = "file:///tmp/dino-lsp-test/b.dino"; + let change = notify( + "textDocument/didChange", + Json::obj(vec![ + ("textDocument", Json::obj(vec![("uri", Json::str(uri))])), + ("contentChanges", Json::Arr(vec![Json::obj(vec![("text", Json::str("func main() {\n print(1)\n}\n"))])])), + ]), + ); + let mut msgs = vec![open(uri, "func main() {\n print(nope)\n}\n"), change]; + msgs.extend(exit_messages()); + let (out, _) = talk(msgs); + assert_eq!(out[0].path(&["params", "diagnostics"]).map(|d| matches!(d, Json::Arr(a) if a.len() == 1)), Some(true)); + assert_eq!(out[1].path(&["params", "diagnostics"]), Some(&Json::Arr(vec![]))); +} + +#[test] +fn error_columns_count_utf16_units() { + // the emoji takes two units in the editor's counting + let src = "func main() {\n print(\"😀\", nope)\n}\n"; + let mut msgs = vec![open("file:///tmp/dino-lsp-test/c.dino", src)]; + msgs.extend(exit_messages()); + let (out, _) = talk(msgs); + let d = match out[0].path(&["params", "diagnostics"]) { + Some(Json::Arr(items)) => items[0].clone(), + other => panic!("{other:?}"), + }; + let start = d.path(&["range", "start", "character"]).and_then(Json::as_i64).unwrap(); + let end = d.path(&["range", "end", "character"]).and_then(Json::as_i64).unwrap(); + // ` print("😀", ` is 4 + 6 + 4 + 2 = 16 units before `nope` + assert_eq!((start, end), (16, 20)); +} + +#[test] +fn formatting_returns_the_tidy_text() { + let uri = "file:///tmp/dino-lsp-test/d.dino"; + let fmt_req = request( + 2, + "textDocument/formatting", + Json::obj(vec![("textDocument", Json::obj(vec![("uri", Json::str(uri))]))]), + ); + let mut msgs = vec![open(uri, "func main(){\nprint( 1 )\n}\n"), fmt_req]; + msgs.extend(exit_messages()); + let (out, _) = talk(msgs); + let reply = out.iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap(); + let edit = match reply.get("result") { + Some(Json::Arr(e)) => e[0].clone(), + other => panic!("{other:?}"), + }; + assert_eq!(edit.get("newText").and_then(Json::as_str), Some("func main() {\n print(1)\n}\n")); +} + +#[test] +fn imports_are_read_next_to_the_file() { + let dir = std::env::temp_dir().join(format!("dino-lsp-{}", std::process::id())); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write(dir.join("util.dino"), "pub func twice(x: int): int { return x * 2 }\n").unwrap(); + let uri = format!("file://{}/main.dino", dir.display()); + let src = "import util\nfunc main() {\n print(util.twice(2))\n}\n"; + let mut msgs = vec![open(&uri, src)]; + msgs.extend(exit_messages()); + let (out, _) = talk(msgs); + let _ = std::fs::remove_dir_all(&dir); + assert_eq!(out[0].path(&["params", "diagnostics"]), Some(&Json::Arr(vec![])), "{:?}", out[0]); +} + +#[test] +fn unknown_requests_get_an_error() { + let mut msgs = vec![request(5, "textDocument/codeLens", Json::obj(vec![]))]; + msgs.extend(exit_messages()); + let (out, _) = talk(msgs); + assert!(out[0].path(&["error", "message"]).is_some()); +} + +#[test] +fn editor_files_are_valid_json() { + let dir = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("editors/vscode"); + for f in ["package.json", "language-configuration.json", "syntaxes/dino.tmLanguage.json"] { + let text = std::fs::read_to_string(dir.join(f)).unwrap(); + json::parse(&text).unwrap_or_else(|e| panic!("{f}: {e}")); + } +} + +// ---------- code intelligence ---------- + +const SRC: &str = "// Adds two numbers.\nfunc add(a: int, b: int): int {\n var sum = a + b\n return sum\n}\n\nstruct P {\n x: int\n}\n\nfunc main() {\n var t = add(1, 2)\n print(t)\n}\n"; + +fn at(uri: &str, line: i64, ch: i64) -> Json { + Json::obj(vec![ + ("textDocument", Json::obj(vec![("uri", Json::str(uri))])), + ("position", Json::obj(vec![("line", Json::Num(line as f64)), ("character", Json::Num(ch as f64))])), + ]) +} + +fn ask(method: &str, params: Json) -> Json { + let mut msgs = vec![request(1, "initialize", Json::obj(vec![])), open("file:///x/a.dino", SRC), request(2, method, params)]; + msgs.extend(exit_messages()); + let (out, _) = talk(msgs); + out.into_iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap().get("result").cloned().unwrap_or(Json::Null) +} + +#[test] +fn outline_lists_top_level_names() { + let r = ask("textDocument/documentSymbol", Json::obj(vec![("textDocument", Json::obj(vec![("uri", Json::str("file:///x/a.dino"))]))])); + let text = r.to_string(); + assert!(text.contains("\"add\"") && text.contains("\"P\"") && text.contains("\"main\"") && text.contains("\"x\""), "{text}"); +} + +#[test] +fn definition_jumps_to_the_function() { + let r = ask("textDocument/definition", at("file:///x/a.dino", 11, 14)); + assert_eq!(r.path(&["range", "start", "line"]).and_then(Json::as_i64), Some(1), "{r:?}"); +} + +#[test] +fn definition_finds_a_local() { + let r = ask("textDocument/definition", at("file:///x/a.dino", 3, 12)); + assert_eq!(r.path(&["range", "start", "line"]).and_then(Json::as_i64), Some(2), "{r:?}"); +} + +#[test] +fn hover_shows_the_signature_and_comment() { + let r = ask("textDocument/hover", at("file:///x/a.dino", 11, 14)); + let v = r.path(&["contents", "value"]).and_then(Json::as_str).unwrap_or("").to_string(); + assert!(v.contains("func add(a: int, b: int): int") && v.contains("Adds two numbers."), "{v}"); +} + +#[test] +fn completion_offers_names_and_keywords() { + let r = ask("textDocument/completion", at("file:///x/a.dino", 12, 4)).to_string(); + assert!(r.contains("\"add\"") && r.contains("\"t\"") && r.contains("\"func\"") && r.contains("\"string\""), "{r:?}"); +} + +#[test] +fn signature_help_marks_the_parameter() { + let src = "func add(a: int, b: int): int {\n return a + b\n}\nfunc main() {\n add(1, \n}\n"; + let mut msgs = vec![request(1, "initialize", Json::obj(vec![])), open("file:///x/b.dino", src), request(2, "textDocument/signatureHelp", at("file:///x/b.dino", 4, 11))]; + msgs.extend(exit_messages()); + let (out, _) = talk(msgs); + let r = out.into_iter().find(|m| m.get("id") == Some(&Json::Num(2.0))).unwrap().get("result").cloned().unwrap(); + assert_eq!(r.get("activeParameter").and_then(Json::as_i64), Some(1), "{r:?}"); + assert!(r.to_string().contains("add(a: int, b: int): int"), "{r:?}"); +} diff --git a/tests/parse.rs b/tests/parse.rs new file mode 100644 index 0000000..769a4a2 --- /dev/null +++ b/tests/parse.rs @@ -0,0 +1,256 @@ +use dino::ast::*; +use dino::parser::parse; + +fn body_of(src: &str) -> Vec { + let p = parse(src).unwrap_or_else(|d| panic!("{}", d.render("test", src))); + match &p.items[0] { + Item::Func(f) => f.body.clone().unwrap().stmts, + other => panic!("expected func, got {other:?}"), + } +} + +fn first_expr(src: &str) -> Expr { + match body_of(&format!("func f() {{\n{src}\n}}")).remove(0) { + Stmt::Expr(e) => e, + Stmt::Return { mut values, .. } => values.remove(0), + other => panic!("unexpected {other:?}"), + } +} + +#[test] +fn all_examples_parse() { + let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/examples"); + let mut n = 0; + for entry in std::fs::read_dir(dir).unwrap() { + let path = entry.unwrap().path(); + if path.extension().map_or(false, |e| e == "dino") { + let src = std::fs::read_to_string(&path).unwrap(); + parse(&src).unwrap_or_else(|d| panic!("{}", d.render(path.to_str().unwrap(), &src))); + n += 1; + } + } + assert!(n >= 4, "expected the example programs, found {n}"); +} + +#[test] +fn precedence() { + // 1 + 2 * 3 => 1 + (2 * 3) + let e = first_expr("1 + 2 * 3"); + match e.kind { + ExprKind::Binary { op: BinOp::Add, rhs, .. } => { + assert!(matches!(rhs.kind, ExprKind::Binary { op: BinOp::Mul, .. })) + } + other => panic!("{other:?}"), + } + // a || b && c => a || (b && c) + let e = first_expr("a || b && c"); + match e.kind { + ExprKind::Binary { op: BinOp::Or, rhs, .. } => { + assert!(matches!(rhs.kind, ExprKind::Binary { op: BinOp::And, .. })) + } + other => panic!("{other:?}"), + } + // subtraction is left-associative: 10 - 3 - 2 => (10 - 3) - 2 + let e = first_expr("10 - 3 - 2"); + match e.kind { + ExprKind::Binary { op: BinOp::Sub, lhs, .. } => { + assert!(matches!(lhs.kind, ExprKind::Binary { op: BinOp::Sub, .. })) + } + other => panic!("{other:?}"), + } +} + +#[test] +fn ranges() { + assert!(matches!(first_expr("0..10").kind, ExprKind::Range { start: Some(_), end: Some(_) })); + assert!(matches!(first_expr("args[1..]").kind, ExprKind::Index { .. })); + let e = first_expr("xs[..3]"); + match e.kind { + ExprKind::Index { index, .. } => { + assert!(matches!(index.kind, ExprKind::Range { start: None, end: Some(_) })) + } + other => panic!("{other:?}"), + } +} + +#[test] +fn float_vs_range() { + assert!(matches!(first_expr("1.5").kind, ExprKind::Float(_))); + assert!(matches!(first_expr("1..5").kind, ExprKind::Range { .. })); +} + +#[test] +fn interpolation() { + let e = first_expr(r#""hi {name}, you are {age + 1}!""#); + match e.kind { + ExprKind::Str(parts) => { + assert_eq!(parts.len(), 5); + assert!(matches!(parts[0], StrPart::Text(_))); + assert!(matches!(parts[1], StrPart::Expr(_))); + assert!(matches!(&parts[4], StrPart::Text(t) if t == "!")); + } + other => panic!("{other:?}"), + } +} + +#[test] +fn escaped_brace_is_plain_text() { + let e = first_expr(r#""\{ not code \}""#); + match e.kind { + ExprKind::Str(parts) => assert_eq!(parts, vec![StrPart::Text("{ not code }".into())]), + other => panic!("{other:?}"), + } +} + +#[test] +fn struct_literal_vs_block() { + // `if a == b {` must be a block, not `b { ... }` as a struct literal. + let stmts = body_of("func f() {\nif a == b { x = 1 }\n}"); + assert!(matches!(stmts[0], Stmt::If { .. })); + // But a literal is fine on the right of `=` and inside parens in a header. + let stmts = body_of("func f() {\nvar p = Point{1, 2}\nif p == (Point{1, 2}) { x = 1 }\n}"); + assert!(matches!(stmts[0], Stmt::Var(_))); + assert!(matches!(stmts[1], Stmt::If { .. })); +} + +#[test] +fn for_forms() { + let stmts = body_of( + "func f() {\nfor { break }\nfor x < 3 { x += 1 }\nfor i in 0..10 step 2 { }\nfor i, x in xs { }\n}", + ); + assert!(matches!(&stmts[0], Stmt::For { kind: ForKind::Infinite, .. })); + assert!(matches!(&stmts[1], Stmt::For { kind: ForKind::While(_), .. })); + match &stmts[2] { + Stmt::For { kind: ForKind::In { vars, step, .. }, .. } => { + assert_eq!(vars.len(), 1); + assert!(step.is_some()); + } + other => panic!("{other:?}"), + } + match &stmts[3] { + Stmt::For { kind: ForKind::In { vars, .. }, .. } => assert_eq!(vars, &["i", "x"]), + other => panic!("{other:?}"), + } +} + +#[test] +fn grouped_params() { + let p = parse("func f(a, b: int, s: string) {}").unwrap(); + let Item::Func(f) = &p.items[0] else { panic!() }; + let names: Vec<_> = f.params.iter().map(|p| p.name.as_str()).collect(); + assert_eq!(names, ["a", "b", "s"]); + assert_eq!(f.params[0].ty, f.params[1].ty); + assert_ne!(f.params[1].ty, f.params[2].ty); +} + +#[test] +fn struct_with_methods() { + let p = parse("pub struct P {\n x, y: f32\n pub func len(): f32 { return x }\n}").unwrap(); + let Item::Struct(s) = &p.items[0] else { panic!() }; + assert!(s.is_pub); + assert_eq!(s.fields.len(), 2); + assert_eq!(s.methods.len(), 1); + assert!(s.methods[0].is_pub); +} + +#[test] +fn types() { + let p = parse("struct E {\n a: shared Doc\n b: weak Win?\n c: []int\n d: Stack[int]\n e: ui.Key\n}").unwrap(); + let Item::Struct(s) = &p.items[0] else { panic!() }; + assert!(matches!(s.fields[0].ty, Type::Shared(_))); + assert!(matches!(s.fields[1].ty, Type::Optional(_))); + assert!(matches!(s.fields[2].ty, Type::Slice(_))); + assert!(matches!(&s.fields[3].ty, Type::Named { args, .. } if args.len() == 1)); + assert!(matches!(&s.fields[4].ty, Type::Named { path, .. } if path.len() == 2)); +} + +#[test] +fn multiple_returns() { + let p = parse("func f(): (int, error) { return 1, nil }").unwrap(); + let Item::Func(f) = &p.items[0] else { panic!() }; + assert!(matches!(&f.ret, Some(Type::Tuple(t)) if t.len() == 2)); +} + +#[test] +fn multi_line_call_and_literal() { + parse("func f() {\n g(\n 1,\n 2,\n)\n var p = P{\n x: 1,\n y: 2,\n }\n}").unwrap(); +} + +#[test] +fn else_if_chain() { + let stmts = body_of("func f() {\nif a { } else if b { } else { }\n}"); + let Stmt::If { else_: Some(e), .. } = &stmts[0] else { panic!() }; + assert!(matches!(**e, Stmt::If { .. })); +} + +#[test] +fn semicolons_work() { + parse("func f() { var a = 1; var b = 2 }").unwrap(); +} + +// ---------- error messages ---------- + +fn err(src: &str) -> dino::error::Diag { + parse(src).unwrap_err() +} + +#[test] +fn error_unclosed_brace_points_at_opener() { + let d = err("func main() {\n var x = 1\n"); + assert!(d.msg.contains("never closed"), "{}", d.msg); + assert_eq!((d.span.line, d.span.col), (1, 13)); +} + +#[test] +fn error_unterminated_string() { + let d = err("func main() {\n var x = \"oops\n}"); + assert!(d.msg.contains("never closed"), "{}", d.msg); + assert_eq!(d.span.line, 2); +} + +#[test] +fn error_missing_param_type() { + let d = err("func f(a) {}"); + assert!(d.msg.contains("`a` needs a type"), "{}", d.msg); +} + +#[test] +fn error_global_var() { + let d = err("var x = 1"); + assert!(d.msg.contains("global `var`"), "{}", d.msg); +} + +#[test] +fn error_const_needs_value() { + assert!(err("func f() { const x: int }").msg.contains("const")); +} + +#[test] +fn error_single_ampersand() { + assert!(err("func f() { var a = b & c }").msg.contains("&&")); +} + +#[test] +fn error_two_statements_on_one_line() { + let d = err("func f() { var a = 1 var b = 2 }"); + assert!(d.msg.contains("end of line"), "{}", d.msg); +} + +#[test] +fn render_shows_caret() { + let src = "func main() {\n var x = \n}"; + let out = err(src).render("t.dino", src); + assert!(out.contains("t.dino:3:1"), "{out}"); + assert!(out.contains("^"), "{out}"); +} + +#[test] +fn generic_bound_uses_a_colon() { + let p = parse("func max[T: Ordered](a, b: T): T { return a }").unwrap(); + let Item::Func(f) = &p.items[0] else { panic!() }; + assert_eq!(f.type_params[0].name, "T"); + assert!(f.type_params[0].constraint.is_some()); + assert!(parse("func max[T](a: T) { }").is_ok()); + let e = parse("func max[T Ordered](a: T) { }").unwrap_err(); + assert!(e.msg.contains("`:` and then the rule"), "{}", e.msg); +} diff --git a/tests/run.rs b/tests/run.rs new file mode 100644 index 0000000..1413c26 --- /dev/null +++ b/tests/run.rs @@ -0,0 +1,2250 @@ +//! End-to-end tests: compile Dino programs to C, build them with the system C +//! compiler, run them, and compare the output. + +use std::process::Command; + +use dino::driver; + +struct Output { + stdout: String, + stderr: String, + code: i32, +} + +fn run_source(src: &str) -> Output { + let c = driver::compile_to_c(src).unwrap_or_else(|d| panic!("{}", d.render("test.dino", src))); + run_c(&c, src) +} + +fn run_c(c: &str, src: &str) -> Output { + let c = c.to_string(); + let exe = driver::temp_exe(); + // every test program also runs under the address sanitizer: a double free, + // a use after free or a leak makes the test fail + let sanitized = driver::build_exe_with(&c, &exe, &["-std=c11", "-fwrapv", "-w", "-fsanitize=address", "-g"]); + if let Err(e) = sanitized { + // a compiler without the sanitizer library: run the tests without it + assert!(e.contains("asan"), "{e}"); + driver::build_exe(&c, &exe).unwrap(); + } + let o = Command::new(&exe).env("ASAN_OPTIONS", "detect_leaks=1").output().unwrap(); + let _ = std::fs::remove_file(&exe); + let out = Output { + stdout: String::from_utf8_lossy(&o.stdout).into_owned(), + stderr: String::from_utf8_lossy(&o.stderr).into_owned(), + code: o.status.code().unwrap_or(-1), + }; + assert!( + !out.stderr.contains("Sanitizer"), + "memory error in the generated program:\n{}\n--- program ---\n{src}", + out.stderr + ); + out +} + +fn out_of(src: &str) -> String { + run_source(src).stdout +} + +#[test] +fn golden_examples() { + let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/basics"); + let mut n = 0; + for entry in std::fs::read_dir(dir).unwrap() { + let path = entry.unwrap().path(); + if path.extension().map_or(false, |e| e == "dino") { + let src = std::fs::read_to_string(&path).unwrap(); + let want = std::fs::read_to_string(path.with_extension("out")) + .unwrap_or_else(|_| panic!("missing .out file for {}", path.display())); + let got = out_of(&src); + assert_eq!(got, want, "output of {} changed", path.display()); + n += 1; + } + } + assert!(n >= 21, "found only {n} example programs"); +} + +#[test] +fn division_by_zero_panics() { + let o = run_source("func main() {\n var z = 0\n print(1)\n print(10 / z)\n print(2)\n}"); + assert_eq!(o.stdout, "1\n"); + assert!(o.stderr.contains("division by zero"), "{}", o.stderr); + assert_eq!(o.code, 2); +} + +#[test] +fn remainder_by_zero_panics() { + let o = run_source("func main() {\n var z = 0\n print(10 % z)\n}"); + assert_eq!(o.code, 2); +} + +#[test] +fn min_int_divided_by_minus_one_does_not_crash() { + let o = run_source( + "func main() {\n var m = -9223372036854775807 - 1\n var d = -1\n print(m / d)\n print(m % d)\n}", + ); + assert_eq!(o.code, 0, "{}", o.stderr); + assert_eq!(o.stdout, "-9223372036854775808\n0\n"); +} + +#[test] +fn zero_step_panics() { + let o = run_source("func main() {\n var s = 0\n for i in 0..3 step s { print(i) }\n}"); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("step"), "{}", o.stderr); +} + +#[test] +fn loop_variable_is_a_copy() { + // changing `i` inside the body must not change how many times the loop runs + let out = out_of("func main() {\n for i in 0..3 {\n print(i)\n i += 10\n }\n}"); + assert_eq!(out, "0\n1\n2\n"); +} + +#[test] +fn range_bounds_are_evaluated_once() { + let out = out_of( + "func n(): int {\n print(\"n called\")\n return 2\n}\nfunc main() {\n for i in 0..n() { print(i) }\n}", + ); + assert_eq!(out, "n called\n0\n1\n"); +} + +#[test] +fn while_condition_with_calls() { + let out = out_of( + "func lt(a, b: int): bool {\n print(\"check\")\n return a < b\n}\nfunc main() {\n var i = 0\n for lt(i, 2) {\n i += 1\n }\n print(i)\n}", + ); + assert_eq!(out, "check\ncheck\ncheck\n2\n"); +} + +#[test] +fn shadowing_in_inner_scope() { + let out = out_of("func main() {\n var x = 1\n if true {\n var x = 2\n print(x)\n }\n print(x)\n}"); + assert_eq!(out, "2\n1\n"); +} + +#[test] +fn const_expressions() { + let out = out_of("const A = 3\nconst B = A * 2 + 1\nfunc main() {\n print(B)\n}"); + assert_eq!(out, "7\n"); +} + +#[test] +fn recursion_and_mutual_recursion() { + let out = out_of( + "func even(n: int): bool {\n if n == 0 { return true }\n return odd(n - 1)\n}\nfunc odd(n: int): bool {\n if n == 0 { return false }\n return even(n - 1)\n}\nfunc main() {\n print(even(10), odd(7), even(7))\n}", + ); + assert_eq!(out, "true true false\n"); +} + +#[test] +fn empty_and_unicode_strings() { + let out = out_of("func main() {\n var e = \"\"\n print(\"[{e}]\", \"héllo\" + \"✓\")\n}"); + assert_eq!(out, "[] héllo✓\n"); +} + +#[test] +fn c_string_escapes() { + // quotes, backslashes, `?` (trigraph-safe), and a digit right after a non-ASCII byte + let out = out_of("func main() {\n print(\"a\\\\b \\\"q\\\" ??= é1\")\n}"); + assert_eq!(out, "a\\b \"q\" ??= é1\n"); +} + +// ---------- type errors ---------- + +fn check_err(src: &str) -> String { + match driver::compile_to_c(src) { + Ok(_) => panic!("expected an error for:\n{src}"), + Err(d) => d.msg, + } +} + +fn main_with(body: &str) -> String { + format!("func main() {{\n{body}\n}}") +} + +#[test] +fn error_mismatched_types() { + let m = check_err(&main_with("var a = 1\nvar b = \"x\"\nvar c = a + b")); + assert!(m.contains("`+` between `int` and `string`"), "{m}"); +} + +#[test] +fn error_int_float_hint() { + let m = check_err(&main_with("var a = 1\nvar b = 2.5\nvar c = a + b")); + assert!(m.contains("float(...)"), "{m}"); +} + +#[test] +fn error_wrong_assignment_type() { + let m = check_err(&main_with("var a = 1\na = \"no\"")); + assert!(m.contains("expected `int`, found `string`"), "{m}"); +} + +#[test] +fn error_condition_must_be_bool() { + let m = check_err(&main_with("if 1 { }")); + assert!(m.contains("expected `bool`"), "{m}"); +} + +#[test] +fn error_unknown_name() { + let m = check_err(&main_with("print(nope)")); + assert!(m.contains("unknown name `nope`"), "{m}"); +} + +#[test] +fn error_unknown_function() { + let m = check_err(&main_with("nope(1)")); + assert!(m.contains("unknown function `nope`"), "{m}"); +} + +#[test] +fn error_wrong_argument_count() { + let m = check_err("func f(a: int): int { return a }\nfunc main() {\n f(1, 2)\n}"); + assert!(m.contains("takes 1 argument, but 2 were given"), "{m}"); +} + +#[test] +fn error_wrong_argument_type() { + let m = check_err("func f(a: int): int { return a }\nfunc main() {\n f(true)\n}"); + assert!(m.contains("expected `int`, found `bool`"), "{m}"); +} + +#[test] +fn error_missing_return() { + let m = check_err("func f(a: int): int {\n if a > 0 { return 1 }\n}\nfunc main() { }"); + assert!(m.contains("without returning"), "{m}"); +} + +#[test] +fn error_return_value_from_unit_function() { + let m = check_err("func f() { return 1 }\nfunc main() { }"); + assert!(m.contains("returns nothing"), "{m}"); +} + +#[test] +fn error_assign_to_const() { + let m = check_err(&main_with("const x = 1\nx = 2")); + assert!(m.contains("constant"), "{m}"); +} + +#[test] +fn error_redeclare_in_same_scope() { + let m = check_err(&main_with("var x = 1\nvar x = 2")); + assert!(m.contains("already defined"), "{m}"); +} + +#[test] +fn error_break_outside_loop() { + let m = check_err(&main_with("break")); + assert!(m.contains("only makes sense inside a `for`"), "{m}"); +} + +#[test] +fn error_no_main() { + let m = check_err("func f() { }"); + assert!(m.contains("no `main`"), "{m}"); +} + +#[test] +fn error_unused_value() { + let m = check_err(&main_with("1 + 2")); + assert!(m.contains("never used"), "{m}"); +} + +#[test] +fn error_using_nothing_as_value() { + let m = check_err("func f() { }\nfunc main() {\n var x = f()\n}"); + assert!(m.contains("returns nothing"), "{m}"); +} + +#[test] +fn error_float_modulo() { + let m = check_err(&main_with("var x = 1.5 % 2.0")); + assert!(m.contains("doesn't work on `float`"), "{m}"); +} + +#[test] +fn error_const_with_call() { + let m = check_err("func f(): int { return 1 }\nconst X = f()\nfunc main() { }"); + assert!(m.contains("can't call functions"), "{m}"); +} + +#[test] +fn error_not_supported_yet_is_clear() { + let m = check_err("interface I[T] { f() }\nfunc main() { }"); + assert!(m.contains("not supported yet"), "{m}"); +} + +// ---------- structs and methods ---------- + +#[test] +fn generated_c_is_const_correct() { + // Methods inferred as non-mutating take `const T *self`; if the inference were + // wrong, the C compiler would complain about discarding `const`. + let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/basics"); + for name in ["structs", "value_semantics", "lists", "list_values", "sieve", "enums", "enum_data", "errors", "optionals", "sized", "own_strings", "own_structs", "own_flows"] { + let src = std::fs::read_to_string(format!("{dir}/{name}.dino")).unwrap(); + let c = driver::compile_to_c(&src).unwrap(); + let exe = driver::temp_exe(); + driver::build_exe_with( + &c, + &exe, + &["-std=c11", "-Wall", "-Werror=discarded-qualifiers", "-Werror=incompatible-pointer-types"], + ) + .unwrap_or_else(|e| panic!("{name}: {e}")); + let _ = std::fs::remove_file(&exe); + } +} + +const POINT: &str = "struct P {\n x, y: int\n func sum(): int { return x + y }\n func inc() { x += 1 }\n}\n"; + +#[test] +fn struct_order_does_not_matter() { + let out = out_of( + "struct Outer { inner: Inner }\nstruct Inner { n: int }\nfunc main() {\n var o = Outer{Inner{5}}\n print(o.inner.n)\n}", + ); + assert_eq!(out, "5\n"); +} + +#[test] +fn param_shadows_field_and_self_reaches_it() { + let out = out_of( + "struct S {\n x: int\n func set(x: int) { self.x = x * 2 }\n func get(): int { return x }\n}\nfunc main() {\n var s: S\n s.set(21)\n print(s.get())\n}", + ); + assert_eq!(out, "42\n"); +} + +#[test] +fn method_returning_self_is_a_copy() { + let out = out_of(&format!( + "{POINT}func main() {{\n var a = P{{1, 2}}\n func_check(a)\n}}\nfunc func_check(a: P) {{\n var b = a\n b.inc()\n print(a.x, b.x)\n}}" + )); + assert_eq!(out, "1 2\n"); +} + +#[test] +fn mutation_through_nested_field() { + let out = out_of( + "struct In { n: int\n func up() { n += 1 } }\nstruct Out { a: In\n func go() { a.up()\n a.up() } }\nfunc main() {\n var o: Out\n o.go()\n o.a.up()\n print(o.a.n)\n}", + ); + assert_eq!(out, "3\n"); +} + +#[test] +fn readonly_methods_work_on_constants() { + let out = out_of(&format!("{POINT}const ORIGIN = P{{3, 4}}\nfunc main() {{\n print(ORIGIN.sum(), ORIGIN.x)\n const k = P{{1, 1}}\n print(k.sum())\n}}")); + assert_eq!(out, "7 3\n2\n"); +} + +#[test] +fn struct_values_in_strings_and_print() { + let out = out_of("struct P { x: int\n name: string }\nfunc main() {\n var p = P{1, \"a\"}\n print(\"p = {p}\")\n print(p, 2)\n}"); + assert_eq!(out, "p = P{x: 1, name: a}\nP{x: 1, name: a} 2\n"); +} + +#[test] +fn empty_struct() { + let out = out_of("struct E {}\nfunc main() {\n var e: E\n var f = E{}\n print(e, f)\n}"); + assert_eq!(out, "E{} E{}\n"); +} + +#[test] +fn fields_named_like_c_keywords() { + let out = out_of("struct K { default, register, int: int }\nfunc main() {\n var k = K{1, 2, 3}\n print(k.default + k.register + k.int)\n}"); + assert_eq!(out, "6\n"); +} + +#[test] +fn struct_field_assignment_ops() { + let out = out_of("struct A { n: int\n f: float }\nfunc main() {\n var a: A\n a.n += 5\n a.n *= 3\n a.f = 1\n a.f /= 4\n print(a.n, a.f)\n}"); + assert_eq!(out, "15 0.25\n"); +} + +#[test] +fn call_in_struct_literal_keeps_order() { + let out = out_of( + "struct P { a, b: int }\nfunc say(s: string, v: int): int {\n print(s)\n return v\n}\nfunc main() {\n var p = P{b: say(\"b\", 2), a: say(\"a\", 1)}\n print(p.a, p.b)\n}", + ); + // initializers run in the order written, not declaration order + assert_eq!(out, "b\na\n1 2\n"); +} + +#[test] +fn method_call_on_call_result() { + let out = out_of(&format!("{POINT}func make(): P {{\n return P{{2, 3}}\n}}\nfunc main() {{\n print(make().sum(), make().y)\n}}")); + assert_eq!(out, "5 3\n"); +} + +#[test] +fn error_struct_compare() { + let m = check_err("struct P { x: int }\nfunc main() {\n var a: P\n var b: P\n var c = a == b\n}"); + assert!(m.contains("compare the fields"), "{m}"); +} + +#[test] +fn error_unknown_field_lists_options() { + let m = check_err(&format!("{POINT}func main() {{\n var a: P\n print(a.z)\n}}")); + assert!(m.contains("no field or method `z`") && m.contains("sum"), "{m}"); +} + +#[test] +fn error_method_without_call() { + let m = check_err(&format!("{POINT}func main() {{\n var a: P\n print(a.sum)\n}}")); + assert!(m.contains("is a method"), "{m}"); +} + +#[test] +fn error_unknown_method() { + let m = check_err(&format!("{POINT}func main() {{\n var a: P\n a.nope()\n}}")); + assert!(m.contains("no field or method `nope`"), "{m}"); +} + +#[test] +fn error_fields_on_a_non_struct() { + let m = check_err("func main() {\n var a = 1\n print(a.x)\n}"); + assert!(m.contains("`int` has no fields"), "{m}"); +} + +#[test] +fn error_literal_wrong_count() { + let m = check_err(&format!("{POINT}func main() {{\n var a = P{{1}}\n}}")); + assert!(m.contains("has 2 fields, but 1 value was given"), "{m}"); +} + +#[test] +fn error_literal_mixed_names() { + let m = check_err(&format!("{POINT}func main() {{\n var a = P{{x: 1, 2}}\n}}")); + assert!(m.contains("not a mix"), "{m}"); +} + +#[test] +fn error_literal_unknown_and_duplicate_field() { + let m = check_err(&format!("{POINT}func main() {{\n var a = P{{z: 1}}\n}}")); + assert!(m.contains("no field `z`"), "{m}"); + let m = check_err(&format!("{POINT}func main() {{\n var a = P{{x: 1, x: 2}}\n}}")); + assert!(m.contains("given twice"), "{m}"); +} + +#[test] +fn error_literal_wrong_field_type() { + let m = check_err(&format!("{POINT}func main() {{\n var a = P{{x: \"s\"}}\n}}")); + assert!(m.contains("expected `int`, found `string`"), "{m}"); +} + +#[test] +fn error_struct_contains_itself() { + let m = check_err("struct A { b: B }\nstruct B { a: A }\nfunc main() { }"); + assert!(m.contains("contains itself"), "{m}"); + let m = check_err("struct A { a: A }\nfunc main() { }"); + assert!(m.contains("contains itself"), "{m}"); +} + +#[test] +fn error_method_name_clashes() { + let m = check_err("struct A {\n x: int\n func x() { }\n}\nfunc main() { }"); + assert!(m.contains("already a field"), "{m}"); + let m = check_err("struct A {\n func f() { }\n func f() { }\n}\nfunc main() { }"); + assert!(m.contains("two methods"), "{m}"); + let m = check_err("struct A { x: int\n x: int }\nfunc main() { }"); + assert!(m.contains("two fields"), "{m}"); +} + +#[test] +fn error_self_parameter() { + let m = check_err("struct A {\n func f(self: int) { }\n}\nfunc main() { }"); + assert!(m.contains("`self` is added automatically"), "{m}"); +} + +#[test] +fn error_mutating_method_on_constant() { + let m = check_err(&format!("{POINT}func main() {{\n const k = P{{1, 2}}\n k.inc()\n}}")); + assert!(m.contains("`inc` changes `k`, but `k` is a constant"), "{m}"); +} + +#[test] +fn error_mutating_method_on_temporary() { + let m = check_err(&format!("{POINT}func make(): P {{\n return P{{1, 2}}\n}}\nfunc main() {{\n make().inc()\n}}")); + assert!(m.contains("temporary or a constant"), "{m}"); +} + +#[test] +fn error_mutating_method_on_global_const() { + let m = check_err(&format!("{POINT}const G = P{{1, 2}}\nfunc main() {{\n G.inc()\n}}")); + assert!(m.contains("temporary or a constant"), "{m}"); +} + +#[test] +fn error_indirect_mutation_is_found() { + // `outer` calls `inner`, which assigns: so `outer` mutates too + let m = check_err( + "struct A {\n n: int\n func inner() { n = 1 }\n func outer() { inner() }\n}\nfunc main() {\n const a = A{0}\n a.outer()\n}", + ); + assert!(m.contains("`outer` changes `a`"), "{m}"); +} + +#[test] +fn error_assign_field_of_constant() { + let m = check_err(&format!("{POINT}func main() {{\n const k = P{{1, 2}}\n k.x = 5\n}}")); + assert!(m.contains("part of `k` because it is a constant"), "{m}"); +} + +#[test] +fn error_type_used_as_value() { + let m = check_err(&format!("{POINT}func main() {{\n var a = P\n}}")); + assert!(m.contains("is a type, not a value"), "{m}"); +} + +#[test] +fn error_struct_name_clash() { + let m = check_err("struct int { }\nfunc main() { }"); + assert!(m.contains("built-in type"), "{m}"); + let m = check_err("struct A { }\nfunc A() { }\nfunc main() { }"); + assert!(m.contains("already defined"), "{m}"); +} + +// ---------- lists ---------- + +#[test] +fn index_out_of_range_panics() { + let o = run_source("func main() {\n var a = [1, 2]\n print(a[0])\n print(a[2])\n print(3)\n}"); + assert_eq!(o.stdout, "1\n"); + assert!(o.stderr.contains("index 2 is out of range (the length is 2)"), "{}", o.stderr); + assert_eq!(o.code, 2); +} + +#[test] +fn negative_index_panics() { + let o = run_source("func main() {\n var a = [1]\n var i = 0 - 1\n print(a[i])\n}"); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("index -1"), "{}", o.stderr); +} + +#[test] +fn pop_from_empty_panics() { + let o = run_source("func main() {\n var a: []int\n a.pop()\n}"); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("empty list"), "{}", o.stderr); +} + +#[test] +fn bad_slice_panics() { + let o = run_source("func main() {\n var a = [1, 2, 3]\n var n = 5\n print(a[1..n])\n}"); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("slice 1..5"), "{}", o.stderr); + let o = run_source("func main() {\n var a = [1, 2, 3]\n var lo = 2\n var hi = 1\n print(a[lo..hi])\n}"); + assert_eq!(o.code, 2); +} + +#[test] +fn slices_are_copies() { + let out = out_of("func main() {\n var a = [1, 2, 3, 4]\n var b = a[1..3]\n b[0] = 99\n print(a, b, a[0..0], a[4..])\n}"); + assert_eq!(out, "[1, 2, 3, 4] [99, 3] [] []\n"); +} + +#[test] +fn list_growth_keeps_values() { + let out = out_of( + "func main() {\n var a: []int\n for i in 0..1000 { a.push(i * 2) }\n var s = 0\n for x in a { s += x }\n print(a.len(), a[999], s)\n}", + ); + assert_eq!(out, "1000 1998 999000\n"); +} + +#[test] +fn lists_of_structs() { + let out = out_of( + "struct P { x, y: int\n func norm1(): int { return x + y } }\nfunc main() {\n var ps = [P{1, 2}, P{3, 4}]\n ps.push(P{5, 6})\n ps[0].x = 10\n var t = 0\n for p in ps { t += p.norm1() }\n print(ps, t)\n}", + ); + assert_eq!(out, "[P{x: 10, y: 2}, P{x: 3, y: 4}, P{x: 5, y: 6}] 30\n"); +} + +#[test] +fn methods_that_change_list_fields_are_mutating() { + let out = out_of( + "struct Stack {\n items: []int\n func push_all(n: int) { for i in 0..n { items.push(i) } }\n func top(): int { return items[items.len() - 1] }\n func take(): int { return items.pop() }\n}\nfunc main() {\n var s: Stack\n s.push_all(4)\n print(s.top(), s.take(), s.top(), s.items)\n}", + ); + assert_eq!(out, "3 3 2 [0, 1, 2]\n"); +} + +#[test] +fn index_with_side_effects_runs_once() { + let out = out_of( + "func idx(): int {\n print(\"idx\")\n return 1\n}\nfunc main() {\n var a = [10, 20, 30]\n a[idx()] += 5\n a[idx()] = a[idx()] * 2\n print(a)\n}", + ); + assert_eq!(out, "idx\nidx\nidx\n[10, 50, 30]\n"); +} + +#[test] +fn read_before_push_in_one_expression() { + // the earlier read of the list sees the list as it was + let out = out_of("func main() {\n var a = [1]\n print(a, a.pop(), a)\n}"); + assert_eq!(out, "[1] 1 []\n"); +} + +#[test] +fn returning_a_local_list_does_not_alias() { + let out = out_of( + "func make(): []int {\n var a = [1, 2]\n return a\n}\nfunc pass(xs: []int): []int {\n return xs\n}\nfunc main() {\n var a = make()\n var b = pass(a)\n b[0] = 9\n print(a, b)\n}", + ); + assert_eq!(out, "[1, 2] [9, 2]\n"); +} + +#[test] +fn borrowed_parameter_is_not_copied() { + let src = "func total(xs: []int): int {\n var t = 0\n for x in xs { t += x }\n return t\n}\nfunc main() {\n var a = [1, 2, 3]\n print(total(a))\n}"; + let c = driver::compile_to_c(src).unwrap(); + assert!(c.contains("d_total(&a_"), "expected the list to be passed by address:\n{c}"); + assert!(!c.contains("L_i64_copy(a_"), "the argument should not be copied"); +} + +#[test] +fn changed_parameter_is_copied() { + let src = "func grow(xs: []int): int {\n xs.push(1)\n return xs.len()\n}\nfunc main() {\n var a = [1, 2, 3]\n print(grow(a), a.len())\n}"; + assert_eq!(out_of(src), "4 3\n"); +} + +#[test] +fn list_of_list_of_strings() { + let out = out_of("func main() {\n var g = [[\"a\"], [\"b\", \"c\"]]\n g[1].push(\"d\")\n print(g, g[1].len(), g[1][2])\n}"); + assert_eq!(out, "[[a], [b, c, d]] 3 d\n"); +} + +#[test] +fn string_len_counts_bytes() { + let out = out_of("func main() {\n print(\"abc\".len(), \"\".len(), \"é\".len())\n}"); + assert_eq!(out, "3 0 2\n"); +} + +#[test] +fn list_in_interpolation() { + let out = out_of("func main() {\n var a = [1, 2]\n print(\"a = {a}, n = {a.len()}\")\n}"); + assert_eq!(out, "a = [1, 2], n = 2\n"); +} + +#[test] +fn loop_over_call_result() { + let out = out_of("func f(): []int {\n print(\"f\")\n return [7, 8]\n}\nfunc main() {\n for x in f() { print(x) }\n}"); + assert_eq!(out, "f\n7\n8\n"); +} + +#[test] +fn error_list_element_type_mismatch() { + let m = check_err(&main_with("var a = [1, \"x\"]")); + assert!(m.contains("expected `int`, found `string`"), "{m}"); +} + +#[test] +fn error_empty_list_needs_a_type() { + let m = check_err(&main_with("var a = []")); + assert!(m.contains("write the type"), "{m}"); +} + +#[test] +fn error_wrong_push_type() { + let m = check_err(&main_with("var a = [1]\na.push(\"s\")")); + assert!(m.contains("expected `int`, found `string`"), "{m}"); +} + +#[test] +fn error_push_on_constant() { + let m = check_err(&main_with("const a = [1]\na.push(2)")); + assert!(m.contains("`push` changes `a`, but `a` is a constant"), "{m}"); +} + +#[test] +fn error_assign_index_of_constant() { + let m = check_err(&main_with("const a = [1]\na[0] = 2")); + assert!(m.contains("constant"), "{m}"); +} + +#[test] +fn error_push_on_temporary() { + let m = check_err("func f(): []int { return [1] }\nfunc main() {\n f().push(2)\n}"); + assert!(m.contains("temporary"), "{m}"); +} + +#[test] +fn error_unknown_list_method() { + let m = check_err(&main_with("var a = [1]\na.reverse()")); + assert!(m.contains("no method `reverse`") && m.contains("len, push, pop"), "{m}"); +} + +#[test] +fn error_index_a_non_list() { + let m = check_err(&main_with("var a = 1\nprint(a[0])")); + assert!(m.contains("can't index a `int`"), "{m}"); + // strings can be indexed since `char` exists + assert_eq!(out_of("func main() {\n var s = \"abc\"\n print(s[1])\n}"), "b\n"); +} + +#[test] +fn error_index_must_be_int() { + let m = check_err(&main_with("var a = [1]\nprint(a[1.5])")); + assert!(m.contains("expected `int`, found `float`"), "{m}"); +} + +#[test] +fn error_assign_to_slice() { + let m = check_err(&main_with("var a = [1, 2]\na[0..1] = [5]")); + assert!(m.contains("can't be assigned to"), "{m}"); +} + +#[test] +fn error_loop_over_int() { + let m = check_err(&main_with("var n = 3\nfor x in n { }")); + assert!(m.contains("can't loop over a `int`"), "{m}"); +} + +#[test] +fn error_step_with_list() { + let m = check_err(&main_with("var a = [1]\nfor x in a step 2 { }")); + assert!(m.contains("`step` only works with ranges"), "{m}"); +} + +#[test] +fn error_list_equality() { + let m = check_err(&main_with("var a = [1]\nvar b = [1]\nvar c = a == b")); + assert!(m.contains("doesn't work on `[]int`"), "{m}"); +} + +#[test] +fn error_const_list_not_yet() { + let m = check_err("const PRIMES = [2, 3, 5]\nfunc main() { }"); + assert!(m.contains("not supported yet"), "{m}"); +} + +#[test] +fn error_wrong_list_argument() { + let m = check_err("func f(xs: []int) { }\nfunc main() {\n f([\"a\"])\n}"); + assert!(m.contains("expected `int`, found `string`"), "{m}"); +} + +// ---------- enums and match ---------- + +const COLOR: &str = "enum Color { Red\n Green\n Blue }\n"; +const SHAPE: &str = "enum Shape {\n Circle(r: float)\n Rect(w, h: float)\n Point\n}\n"; + +#[test] +fn match_evaluates_the_value_once() { + let out = out_of(&format!( + "{COLOR}func pick(): Color {{\n print(\"pick\")\n return Color.Blue\n}}\nfunc main() {{\n match pick() {{\n Red => print(1)\n Green => print(2)\n Blue => print(3)\n }}\n}}" + )); + assert_eq!(out, "pick\n3\n"); +} + +#[test] +fn return_inside_match_arms_counts_as_returning() { + // every arm returns, so the function needs no `return` after the match + let out = out_of(&format!( + "{COLOR}func f(c: Color): int {{\n match c {{\n Red => {{ return 1 }}\n Green => {{ return 2 }}\n Blue => return 3\n }}\n}}\nfunc main() {{\n print(f(Color.Red), f(Color.Green), f(Color.Blue))\n}}" + )); + assert_eq!(out, "1 2 3\n"); +} + +#[test] +fn return_arm_inside_a_value_match() { + let out = out_of( + "enum R { Ok(v: int)\n Err(m: string) }\nfunc f(r: R): int {\n var x = match r {\n Ok(v) => v * 2\n Err(_) => return -1\n }\n return x + 1\n}\nfunc main() {\n print(f(R.Ok(5)), f(R.Err(\"no\")))\n}", + ); + assert_eq!(out, "11 -1\n"); +} + +#[test] +fn break_and_continue_inside_match_arms() { + let out = out_of(&format!( + "{COLOR}func main() {{\n var seen = 0\n for i in 0..10 {{\n var c = Color.Red\n if i % 3 == 1 {{ c = Color.Green }}\n if i == 7 {{ c = Color.Blue }}\n match c {{\n Red => continue\n Green => {{ seen += 1 }}\n Blue => break\n }}\n }}\n print(seen)\n}}" + )); + assert_eq!(out, "2\n"); +} + +#[test] +fn enum_method_can_replace_self() { + let out = out_of( + "enum Light {\n Red\n Green\n Yellow\n func advance() {\n self = match self {\n Red => Light.Green\n Green => Light.Yellow\n Yellow => Light.Red\n }\n }\n}\nfunc main() {\n var l = Light.Red\n l.advance()\n l.advance()\n print(l)\n l.advance()\n print(l)\n}", + ); + assert_eq!(out, "Light.Yellow\nLight.Red\n"); +} + +#[test] +fn enum_methods_call_each_other() { + let out = out_of( + "enum Shape {\n Circle(r: float)\n Rect(w, h: float)\n Point\n func area(): float {\n return match self {\n Circle(r) => r * r\n Rect(w, h) => w * h\n Point => 0\n }\n }\n func double_area(): float {\n return area() * 2\n }\n}\nfunc main() {\n print(Shape.Rect(2, 3).double_area())\n}", + ); + assert_eq!(out, "12.0\n"); +} + +#[test] +fn list_of_enums_and_enum_in_struct() { + let out = out_of(&format!( + "{COLOR}struct Pixel {{ c: Color\n x: int }}\nfunc main() {{\n var ps = [Pixel{{Color.Blue, 1}}, Pixel{{x: 2}}]\n print(ps)\n var cs: []Color\n cs.push(Color.Green)\n print(cs, cs[0] == Color.Green)\n}}" + )); + assert_eq!(out, "[Pixel{c: Color.Blue, x: 1}, Pixel{c: Color.Red, x: 2}]\n[Color.Green] true\n"); +} + +#[test] +fn match_binding_shadows_and_arms_have_their_own_scope() { + let out = out_of(&format!( + "{SHAPE}func main() {{\n var r = 100.0\n match Shape.Circle(1.5) {{\n Circle(r) => print(r)\n Rect(r, _) => print(r)\n Point => print(0)\n }}\n print(r)\n}}" + )); + assert_eq!(out, "1.5\n100.0\n"); +} + +#[test] +fn match_on_negative_numbers_and_strings() { + let out = out_of( + "func sign(n: int): string {\n return match n {\n -1 => \"minus one\"\n 0 => \"zero\"\n _ => \"other\"\n }\n}\nfunc main() {\n print(sign(-1), sign(0), sign(5))\n var s = \"b\"\n print(match s {\n \"a\" => 1\n \"b\" => 2\n _ => 3\n })\n}", + ); + assert_eq!(out, "minus one zero other\n2\n"); +} + +#[test] +fn catch_all_binding_takes_the_value() { + let out = out_of("func f(n: int): int {\n return match n {\n 0 => 100\n other => other * 2\n }\n}\nfunc main() {\n print(f(0), f(21))\n}"); + assert_eq!(out, "100 42\n"); +} + +#[test] +fn value_match_arms_unify_int_literal_into_float() { + let out = out_of(&format!("{COLOR}func main() {{\n var x: float = match Color.Red {{\n Red => 1\n _ => 2.5\n }}\n print(x)\n}}")); + assert_eq!(out, "1.0\n"); +} + +#[test] +fn match_in_condition_with_side_effects_keeps_order() { + let out = out_of(&format!( + "{COLOR}func say(s: string): Color {{\n print(s)\n return Color.Red\n}}\nfunc main() {{\n var ok = match say(\"a\") {{\n Red => true\n _ => false\n }} && match say(\"b\") {{\n Red => true\n _ => false\n }}\n print(ok)\n}}" + )); + assert_eq!(out, "a\nb\ntrue\n"); +} + +#[test] +fn payload_data_is_copied_when_changed() { + let out = out_of( + "enum E { L(xs: []int)\n N }\nfunc main() {\n var a = E.L([1, 2])\n match a {\n L(xs) => {\n xs.push(3)\n xs[0] = 9\n print(xs)\n }\n N => print(0)\n }\n print(a)\n}", + ); + assert_eq!(out, "[9, 2, 3]\nE.L([1, 2])\n"); +} + +#[test] +fn changing_the_matched_variable_inside_an_arm() { + // the arm keeps seeing the value as it was when the match started + let out = out_of( + "enum E { L(xs: []int)\n N }\nfunc main() {\n var a = E.L([1, 2])\n match a {\n L(xs) => {\n a = E.N\n print(xs, a)\n }\n N => print(0)\n }\n}", + ); + assert_eq!(out, "[1, 2] E.N\n"); +} + +#[test] +fn returning_a_match_binding_copies_it() { + let out = out_of( + "enum E { L(xs: []int) }\nfunc get(e: E): []int {\n return match e {\n L(xs) => xs\n }\n}\nfunc main() {\n var e = E.L([1])\n var a = get(e)\n a.push(2)\n print(a, e)\n}", + ); + assert_eq!(out, "[1, 2] E.L([1])\n"); +} + +#[test] +fn enum_printing_with_strings_and_nesting() { + let out = out_of( + "enum In { A(n: int)\n B }\nenum Out { W(i: In, s: string)\n Z }\nfunc main() {\n print(Out.W(In.A(3), \"x\"), Out.W(In.B, \"y\"), Out.Z)\n}", + ); + assert_eq!(out, "Out.W(In.A(3), x) Out.W(In.B, y) Out.Z\n"); +} + +#[test] +fn enums_declared_after_use_and_inside_each_other() { + let out = out_of( + "struct Holder { s: Shape }\nfunc main() {\n var h = Holder{Shape.Circle(2)}\n print(h)\n}\nenum Shape { Circle(r: float) }", + ); + assert_eq!(out, "Holder{s: Shape.Circle(2.0)}\n"); +} + +// ---------- enum and match errors ---------- + +#[test] +fn error_match_missing_cases_are_listed() { + let m = check_err(&format!("{COLOR}func main() {{\n match Color.Red {{\n Red => print(1)\n }}\n}}")); + assert!(m.contains("doesn't handle Color.Green, Color.Blue"), "{m}"); +} + +#[test] +fn error_match_int_needs_catch_all() { + let m = check_err("func main() {\n match 1 {\n 1 => print(1)\n }\n}"); + assert!(m.contains("add a `_` case"), "{m}"); +} + +#[test] +fn error_match_bool_incomplete() { + let m = check_err("func main() {\n match true {\n true => print(1)\n }\n}"); + assert!(m.contains("doesn't handle false"), "{m}"); +} + +#[test] +fn error_match_duplicate_case() { + let m = check_err(&format!("{COLOR}func main() {{\n match Color.Red {{\n Red => print(1)\n Red => print(2)\n _ => print(3)\n }}\n}}")); + assert!(m.contains("already handled"), "{m}"); + let m = check_err("func main() {\n match 1 {\n 1 => print(1)\n 1 => print(2)\n _ => print(3)\n }\n}"); + assert!(m.contains("already handled"), "{m}"); +} + +#[test] +fn error_match_unreachable_case() { + let m = check_err(&format!("{COLOR}func main() {{\n match Color.Red {{\n _ => print(1)\n Red => print(2)\n }}\n}}")); + assert!(m.contains("can never match"), "{m}"); + let m = check_err("func main() {\n match 1 {\n x => print(x)\n 2 => print(2)\n }\n}"); + assert!(m.contains("can never match"), "{m}"); +} + +#[test] +fn error_match_unknown_case_is_not_a_silent_binding() { + let m = check_err(&format!("{COLOR}func main() {{\n match Color.Red {{\n Red => print(1)\n Gren => print(2)\n Blue => print(3)\n }}\n}}")); + assert!(m.contains("no case `Gren`") && m.contains("Red, Green, Blue"), "{m}"); +} + +#[test] +fn error_match_case_with_data_needs_parentheses() { + let m = check_err(&format!("{SHAPE}func main() {{\n match Shape.Point {{\n Circle => print(1)\n _ => print(2)\n }}\n}}")); + assert!(m.contains("carries data") && m.contains("Circle(_)"), "{m}"); +} + +#[test] +fn error_match_wrong_number_of_pieces() { + let m = check_err(&format!("{SHAPE}func main() {{\n match Shape.Point {{\n Rect(w) => print(w)\n _ => print(2)\n }}\n}}")); + assert!(m.contains("carries w: float, h: float"), "{m}"); + let m = check_err(&format!("{SHAPE}func main() {{\n match Shape.Point {{\n Point(x) => print(x)\n _ => print(2)\n }}\n}}")); + assert!(m.contains("has no data"), "{m}"); +} + +#[test] +fn error_match_nested_pattern_not_yet() { + let m = check_err(&format!("{SHAPE}enum Wrap {{ W(s: Shape) }}\nfunc main() {{\n match Wrap.W(Shape.Point) {{\n W(Point) => print(1)\n _ => print(2)\n }}\n}}")); + assert!(m.contains("not supported yet") || m.contains("lowercase"), "{m}"); +} + +#[test] +fn error_match_arms_of_different_types() { + let m = check_err(&format!("{COLOR}func main() {{\n var x = match Color.Red {{\n Red => 1\n _ => \"s\"\n }}\n}}")); + assert!(m.contains("expected `int`, found `string`"), "{m}"); +} + +#[test] +fn error_match_value_arm_must_be_one_expression() { + let m = check_err(&format!("{COLOR}func main() {{\n var x = match Color.Red {{\n Red => {{\n print(1)\n }}\n _ => 2\n }}\n}}")); + assert!(m.contains("single expression"), "{m}"); +} + +#[test] +fn error_match_on_unsupported_type() { + let m = check_err("func main() {\n match 1.5 {\n _ => print(1)\n }\n}"); + assert!(m.contains("can't `match` on a `float`"), "{m}"); + let m = check_err("struct P { x: int }\nfunc main() {\n var p: P\n match p {\n _ => print(1)\n }\n}"); + assert!(m.contains("can't `match` on a `P`"), "{m}"); +} + +#[test] +fn error_match_pattern_of_wrong_kind() { + let m = check_err("func main() {\n match 1 {\n true => print(1)\n _ => print(2)\n }\n}"); + assert!(m.contains("this is a bool"), "{m}"); + let m = check_err(&format!("{COLOR}func main() {{\n match Color.Red {{\n 1 => print(1)\n _ => print(2)\n }}\n}}")); + assert!(m.contains("this is a number"), "{m}"); +} + +#[test] +fn error_enum_construction() { + let m = check_err(&format!("{SHAPE}func main() {{\n var s = Shape.Circle()\n}}")); + assert!(m.contains("`Shape.Circle` needs 1 value: (r: float)"), "{m}"); + let m = check_err(&format!("{SHAPE}func main() {{\n var s = Shape.Point(1)\n}}")); + assert!(m.contains("has no data, so write it without parentheses"), "{m}"); + let m = check_err(&format!("{SHAPE}func main() {{\n var s = Shape.Circle\n}}")); + assert!(m.contains("needs 1 value"), "{m}"); + let m = check_err(&format!("{SHAPE}func main() {{\n var s = Shape.Triangle(1)\n}}")); + assert!(m.contains("no case `Triangle`") && m.contains("Circle, Rect, Point"), "{m}"); + let m = check_err(&format!("{SHAPE}func main() {{\n var s = Shape.Circle(\"x\")\n}}")); + assert!(m.contains("expected `float`, found `string`"), "{m}"); +} + +#[test] +fn error_enum_name_alone_is_a_type() { + let m = check_err(&format!("{SHAPE}func main() {{\n var s = Shape\n}}")); + assert!(m.contains("is a type, not a value") && m.contains("Shape.Circle"), "{m}"); +} + +#[test] +fn error_enum_has_no_fields() { + let m = check_err(&format!("{SHAPE}func main() {{\n var s = Shape.Point\n print(s.r)\n}}")); + assert!(m.contains("use `match`"), "{m}"); +} + +#[test] +fn error_enum_equality_only_without_data() { + let m = check_err(&format!("{SHAPE}func main() {{\n var a = Shape.Point\n print(a == Shape.Point)\n}}")); + assert!(m.contains("doesn't work on `Shape`") && m.contains("use `match`"), "{m}"); +} + +#[test] +fn error_enum_declarations() { + let m = check_err("enum E { }\nfunc main() { }"); + assert!(m.contains("at least one case"), "{m}"); + let m = check_err("enum E { A\n A }\nfunc main() { }"); + assert!(m.contains("two cases named `A`"), "{m}"); + let m = check_err("enum E { A(x: int, x: int) }\nfunc main() { }"); + assert!(m.contains("two values named `x`"), "{m}"); + let m = check_err("enum E { A(e: E) }\nfunc main() { }"); + assert!(m.contains("enum `E` contains itself"), "{m}"); + let m = check_err("struct S { e: E }\nenum E { A(s: S) }\nfunc main() { }"); + assert!(m.contains("contains itself"), "{m}"); + let m = check_err("enum E { A }\nstruct E { }\nfunc main() { }"); + assert!(m.contains("already defined"), "{m}"); +} + +#[test] +fn error_mutating_enum_method_on_constant() { + let m = check_err( + "enum L { A\n B\n func flip() { self = L.B } }\nfunc main() {\n const l = L.A\n l.flip()\n}", + ); + assert!(m.contains("`flip` changes `l`, but `l` is a constant"), "{m}"); +} + +#[test] +fn error_unknown_enum_method() { + let m = check_err(&format!("{COLOR}func main() {{\n var c = Color.Red\n c.paint()\n}}")); + assert!(m.contains("enum `Color` has no method `paint`"), "{m}"); +} + +// ---------- errors and optionals ---------- + +#[test] +fn main_error_prints_and_exits_1() { + let o = run_source("func main(): error {\n print(1)\n return error(\"boom {2}\")\n}"); + assert_eq!(o.stdout, "1\n"); + assert_eq!(o.stderr, "error: boom 2\n"); + assert_eq!(o.code, 1); +} + +#[test] +fn main_error_nil_exits_0() { + let o = run_source("func main(): error {\n print(1)\n return nil\n}"); + assert_eq!(o.code, 0, "{}", o.stderr); +} + +#[test] +fn error_shorthand_fills_zero_values_for_several_results() { + let out = out_of( + "func a(n: int): (int, string, error) {\n if n > 0 { return n, \"x\", nil }\n return error(\"neg\")\n}\n\ + func b(n: int): (int, error) {\n var v, s, e = a(n)\n if e != nil { return e }\n return v + s.len(), nil\n}\n\ + func main() {\n var r, e = b(-1)\n print(r, e)\n var r2, e2 = b(4)\n print(r2, e2)\n}", + ); + assert_eq!(out, "0 neg\n5 nil\n"); +} + +#[test] +fn try_in_the_middle_of_an_expression_keeps_order() { + let out = out_of( + "func f(n: int): (int, error) {\n print(\"f\", n)\n if n == 2 { return error(\"two\") }\n return n, nil\n}\n\ + func g(): (int, error) {\n var s = f(1)? + f(2)? + f(3)?\n return s, nil\n}\n\ + func main() {\n var v, e = g()\n print(v, e)\n}", + ); + assert_eq!(out, "f 1\nf 2\n0 two\n"); +} + +#[test] +fn try_inside_a_condition_and_loop() { + let out = out_of( + "func ok(n: int): (bool, error) {\n if n == 3 { return error(\"three\") }\n return n < 5, nil\n}\n\ + func run(): error {\n for i in 0..10 {\n if ok(i)? { print(i) }\n }\n return nil\n}\n\ + func main() {\n var e = run()\n print(e)\n}", + ); + assert_eq!(out, "0\n1\n2\nthree\n"); +} + +#[test] +fn optional_struct_and_list_values_are_copied() { + let out = out_of( + "func pick(xs: []int): []int? {\n return xs\n}\n\ + func main() {\n var a = [1, 2]\n var b = pick(a)\n a.push(3)\n print(a, b)\n\ + var c: []int? = a\n a.push(4)\n print(a, c)\n}", + ); + assert_eq!(out, "[1, 2, 3] [1, 2]\n[1, 2, 3, 4] [1, 2, 3]\n"); +} + +#[test] +fn narrowing_covers_if_early_exit_and_and_or() { + let out = out_of( + "func f(x: int?): int {\n if x == nil { return -1 }\n return x * 2\n}\n\ + func g(x: int?): string {\n if x != nil && x > 5 { return \"big\" }\n if x == nil || x < 0 { return \"none or negative\" }\n return \"small\"\n}\n\ + func main() {\n print(f(4), f(nil), g(9), g(nil), g(-1), g(2))\n}", + ); + assert_eq!(out, "8 -1 big none or negative none or negative small\n"); +} + +#[test] +fn narrowing_is_forgotten_after_assignment() { + let m = check_err("func main() {\n var x: int? = 3\n if x != nil {\n x = nil\n print(x + 1)\n }\n}"); + assert!(m.contains("can't use `+` between `int?` and `int`"), "{m}"); +} + +#[test] +fn narrowing_in_a_loop_stays_safe_at_runtime() { + // x is assigned later in the body, so the use at the top of the next round sees nil + let o = run_source( + "func main() {\n var x: int? = 1\n for i in 0..3 {\n if x == nil { break }\n print(x)\n x = nil\n }\n}", + ); + assert_eq!(o.stdout, "1\n"); + assert_eq!(o.code, 0, "{}", o.stderr); +} + +#[test] +fn narrowing_reaches_fields_and_methods() { + let out = out_of( + "struct S {\n n: int\n func twice(): int { return n * 2 }\n}\n\ + func main() {\n var s: S? = S{4}\n if s != nil {\n print(s.n, s.twice())\n }\n}", + ); + assert_eq!(out, "4 8\n"); +} + +#[test] +fn discard_with_underscore_and_error_values_print() { + let out = out_of( + "func f(): (int, error) {\n return 1, error(\"x\")\n}\n\ + func main() {\n var _, e = f()\n print(e)\n var n: error\n print(n == nil, e != nil)\n}", + ); + assert_eq!(out, "x\ntrue true\n"); +} + +#[test] +fn optional_as_struct_return_through_match() { + let out = out_of( + "enum Shape { Circle(r: float)\n Dot }\n\ + func find(n: int): Shape? {\n if n > 0 { return Shape.Circle(1.5) }\n return nil\n}\n\ + func main() {\n var s = find(1)\n if s != nil {\n match s {\n Circle(r) => print(\"circle\", r)\n Dot => print(\"dot\")\n }\n }\n}", + ); + assert_eq!(out, "circle 1.5\n"); +} + +#[test] +fn generated_c_for_errors_is_clean() { + let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/basics"); + for name in ["errors", "optionals", "sized", "own_strings", "own_structs", "own_flows"] { + let src = std::fs::read_to_string(format!("{dir}/{name}.dino")).unwrap(); + let c = driver::compile_to_c(&src).unwrap(); + let exe = driver::temp_exe(); + driver::build_exe_with(&c, &exe, &["-std=c11", "-Wall", "-Werror=incompatible-pointer-types"]) + .unwrap_or_else(|e| panic!("{name}: {e}")); + let _ = std::fs::remove_file(&exe); + } +} + +#[test] +fn error_try_needs_an_error_returning_function() { + let m = check_err("func f(): (int, error) { return 1, nil }\nfunc main() {\n var x = f()?\n}"); + assert!(m.contains("this function must return an `error`"), "{m}"); +} + +#[test] +fn error_try_on_non_error() { + let m = check_err("func f(): int { return 1 }\nfunc g(): error {\n var x = f()?\n return nil\n}\nfunc main() { }"); + assert!(m.contains("`?` needs a call that returns an error"), "{m}"); +} + +#[test] +fn error_ignored_error() { + let m = check_err("func f(): error { return nil }\nfunc main() {\n f()\n}"); + assert!(m.contains("its error is ignored"), "{m}"); +} + +#[test] +fn error_several_values_used_as_one() { + let m = check_err("func f(): (int, error) { return 1, nil }\nfunc main() {\n var x = f()\n}"); + assert!(m.contains("returns several values"), "{m}"); +} + +#[test] +fn error_wrong_number_of_names_and_returns() { + let m = check_err("func f(): (int, error) { return 1, nil }\nfunc main() {\n var a, b, c = f()\n}"); + assert!(m.contains("but 3 names are listed"), "{m}"); + let m = check_err("func f(): (int, error) { return 1 }\nfunc main() { }"); + assert!(m.contains("needs 2 values"), "{m}"); +} + +#[test] +fn error_nil_without_a_type() { + let m = check_err("func main() {\n var x = nil\n}"); + assert!(m.contains("can't tell what `nil` stands for"), "{m}"); +} + +#[test] +fn error_nil_for_plain_type() { + let m = check_err("func main() {\n var x: int = nil\n}"); + assert!(m.contains("can't tell what `nil`") || m.contains("expected `int`"), "{m}"); + let m = check_err("func main() {\n var x = 1\n if x == nil { }\n}"); + assert!(m.contains("can never be nil"), "{m}"); +} + +#[test] +fn error_optional_used_without_check() { + let m = check_err("func main() {\n var x: int? = 3\n print(x + 1)\n}"); + assert!(m.contains("`+` between `int?` and `int`") || m.contains("can't use `+`"), "{m}"); +} + +#[test] +fn error_optional_in_struct_not_supported_yet() { + let m = check_err("struct S { x: int? }\nfunc main() { }"); + assert!(m.contains("not supported yet"), "{m}"); +} + +// ---------- colon syntax for types ---------- + +#[test] +fn colon_syntax_everywhere() { + let out = out_of( + "struct P { x, y: int\n tag: string }\nenum E { A(n: int)\n B(a, b: float) }\n\ + func add(a, b: int): int { return a + b }\n\ + func name(p: P): string { return p.tag }\n\ + func main() {\n var n: int = add(1, 2)\n var xs: []int\n xs.push(n)\n var e: E = E.A(4)\n var p: P = P{1, 2, \"t\"}\n print(xs, e, name(p))\n}", + ); + assert_eq!(out, "[3] E.A(4) t\n"); +} + +#[test] +fn error_missing_colons_are_explained() { + let m = check_err("func f(a int) { }\nfunc main() { }"); + assert!(m.contains("`:` and then the type"), "{m}"); + let m = check_err("func f(): int { return 1 }\nfunc g() int { return 1 }\nfunc main() { }"); + assert!(m.contains("`:` before the return type"), "{m}"); + let m = check_err("func main() {\n var x int = 1\n}"); + assert!(m.contains("`:` and then the type"), "{m}"); + let m = check_err("struct S { x int }\nfunc main() { }"); + assert!(m.contains("`:` and then the type of the field"), "{m}"); +} + +// ---------- sized numbers ---------- + +#[test] +fn i64_and_f64_are_int_and_float() { + let out = out_of( + "func f(a: i64): f64 { return float(a) / 2 }\nfunc main() {\n var x: int = 5\n var y: float = f(x)\n print(y)\n}", + ); + assert_eq!(out, "2.5\n"); +} + +#[test] +fn sized_integers_wrap_around() { + let out = out_of( + "func main() {\n var a: u8 = 255\n a += 1\n var b: i8 = 127\n b += 1\n var c: u16 = 0\n c -= 1\n var d: u32 = 4000000000\n d += d\n var e: i16 = -32768\n e = -e\n print(a, b, c, d, e)\n}", + ); + assert_eq!(out, "0 -128 65535 3705032704 -32768\n"); +} + +#[test] +fn sized_division_checks_zero_and_min() { + let o = run_source("func main() {\n var m: i32 = -2147483647 - 1\n var d: i32 = -1\n print(m / d, m % d)\n var z: u64 = 0\n print(1 / z)\n}"); + assert_eq!(o.stdout, "-2147483648 0\n"); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("division by zero"), "{}", o.stderr); +} + +#[test] +fn big_u64_values() { + let out = out_of( + "func main() {\n var x: u64 = 9223372036854775807\n x = x * 2 + 1\n print(x, x / 3, x > 5)\n print(u64(-1), u64(1000000000000000000000000.0), float(x))\n}", + ); + assert_eq!(out, "18446744073709551615 6148914691236517205 true\n18446744073709551615 18446744073709551615 1.84467440737096e+19\n"); +} + +#[test] +fn f32_arithmetic_and_printing() { + let out = out_of( + "func main() {\n var a: f32 = 0.1\n var b: f32 = 0.2\n print(a + b, f64(a + b) == 0.3, a * 3)\n var big: f32 = 16777216\n print(big + 1)\n print(f32(1) / f32(3))\n}", + ); + assert_eq!(out, "0.3 false 0.3\n16777216.0\n0.33333334\n"); +} + +#[test] +fn literals_adapt_in_every_place() { + let out = out_of( + "struct P { hp: u8\n speed: f32 }\nfunc heal(p: P, n: u8): u8 { return p.hp + n }\n\ + func main() {\n var p = P{100, 2}\n print(heal(p, 200), p.speed * 2)\n}", + ); + assert_eq!(out, "44 4.0\n"); +} + +#[test] +fn sized_values_in_lists_options_and_errors() { + let out = out_of( + "func half(n: u8): (u8, error) {\n if n % 2 == 1 { return error(\"odd\") }\n return n / 2, nil\n}\n\ + func main() {\n var xs: []u8 = [4, 7]\n for x in xs {\n var h, e = half(x)\n print(h, e)\n }\n var o: i16? = 5\n if o != nil { print(o + 1) }\n}", + ); + assert_eq!(out, "2 nil\n0 odd\n6\n"); +} + +#[test] +fn error_mixing_number_types() { + let m = check_err("func main() {\n var a: i32 = 1\n var b: i64 = 2\n print(a + b)\n}"); + assert!(m.contains("can't use `+` between `i32` and `int`"), "{m}"); + let m = check_err("func main() {\n var a: u8 = 1\n var b: int = a\n}"); + assert!(m.contains("expected `int`, found `u8` (convert it with `int(...)`)"), "{m}"); +} + +#[test] +fn error_literal_out_of_range() { + let m = check_err("func main() {\n var a: u8 = 256\n}"); + assert!(m.contains("256 doesn't fit in a `u8` (it holds 0 to 255)"), "{m}"); + let m = check_err("func main() {\n var a: i8 = -129\n}"); + assert!(m.contains("-129 doesn't fit in a `i8`"), "{m}"); + let m = check_err("func main() {\n var a: u32 = -1\n}"); + assert!(m.contains("-1 doesn't fit in a `u32`"), "{m}"); + let m = check_err("func main() {\n var a: u8 = 1\n var b = a + 300\n}"); + assert!(m.contains("300 doesn't fit in a `u8`"), "{m}"); +} + +#[test] +fn error_negating_unsigned_and_float_remainder() { + let m = check_err("func main() {\n var a: u8 = 1\n print(-a)\n}"); + assert!(m.contains("can't be negative"), "{m}"); + let m = check_err("func main() {\n var a: f32 = 1\n print(a % 2)\n}"); + assert!(m.contains("`%` doesn't work on `f32`"), "{m}"); +} + +#[test] +fn error_float_literal_for_integer_type() { + let m = check_err("func main() {\n var a: u8 = 1.5\n}"); + assert!(m.contains("expected `u8`, found `float`"), "{m}"); +} + +// ---------- modules: `import utils` ---------- + +fn project_c(main: &str, modules: &[(&str, &str)]) -> Result { + driver::compile_project("main.dino", main, |key| { + modules.iter().find(|(k, _)| *k == key).map(|(k, src)| (format!("{k}.dino"), src.to_string())) + }) +} + +fn run_project(main: &str, modules: &[(&str, &str)]) -> String { + let c = project_c(main, modules).unwrap_or_else(|e| panic!("{e}")); + run_c(&c, main).stdout +} + +const UTILS: &str = "pub const SCALE = 10 +pub struct Vec2 { + pub x, y: float + pub func len2(): float { return x * x + y * y } + func secret(): int { return 1 } +} +pub enum Shape { + Circle(r: float) + Point +} +pub func double(n: int): int { return n * 2 * SCALE / 10 } +pub func shout(s: string): string { return s.upper() + helper() } +func helper(): string { return \"!\" } +pub func area(s: Shape): float { + return match s { + Circle(r) => r * r * 3.0 + Point => 0.0 + } +} +pub func make(): []Vec2 { return [Vec2{1.0, 2.0}] } +"; + +#[test] +fn import_user_module() { + let main = "import utils +func main() { + print(utils.double(21), utils.SCALE) + var v = utils.Vec2{3.0, 4.0} + print(v.len2()) + print(utils.shout(\"hey\")) + var s = utils.Shape.Circle(2.0) + print(utils.area(s), utils.area(utils.Shape.Point)) + var xs = utils.make() + print(xs.len()) +}"; + assert_eq!(run_project(main, &[("utils", UTILS)]), "42 10\n25.0\nHEY!\n12.0 0.0\n1\n"); +} + +#[test] +fn module_types_in_signatures() { + let main = "import utils +func total(v: utils.Vec2, s: utils.Shape): float { return v.len2() + utils.area(s) } +func main() { print(total(utils.Vec2{1.0, 1.0}, utils.Shape.Circle(1.0))) }"; + assert_eq!(run_project(main, &[("utils", UTILS)]), "5.0\n"); +} + +#[test] +fn modules_can_use_each_other_and_share_names() { + let a = "import b +pub func f(): int { return b.f() + 1 }"; + let b = "pub func f(): int { return 10 }"; + let main = "import a +import b +func f(): int { return 100 } +func main() { print(f(), a.f(), b.f()) }"; + assert_eq!(run_project(main, &[("b", b), ("a", a)]), "100 11 10\n"); +} + +#[test] +fn nested_module_path() { + let main = "import tools.math2 +func main() { print(math2.twice(4)) }"; + let m = "pub func twice(n: int): int { return n * 2 }"; + assert_eq!(run_project(main, &[("tools.math2", m)]), "8\n"); +} + +fn project_err(main: &str, modules: &[(&str, &str)]) -> String { + project_c(main, modules).unwrap_err() +} + +#[test] +fn error_private_function() { + let e = project_err("import utils\nfunc main() { print(utils.helper()) }", &[("utils", UTILS)]); + assert!(e.contains("`helper` is private to `utils`"), "{e}"); + assert!(e.contains("main.dino:2"), "{e}"); +} + +#[test] +fn error_private_method() { + let e = project_err( + "import utils\nfunc main() { var v = utils.Vec2{1.0, 2.0}\nprint(v.secret()) }", + &[("utils", UTILS)], + ); + assert!(e.contains("method `secret` is private"), "{e}"); +} + +#[test] +fn error_private_type() { + let m = "struct Hidden { x: int }\npub func f() {}"; + let e = project_err("import m\nfunc main() { var h = m.Hidden{1} }", &[("m", m)]); + assert!(e.contains("`Hidden` is private to `m`"), "{e}"); +} + +#[test] +fn error_in_module_names_that_file() { + let e = project_err("import bad\nfunc main() { print(bad.f()) }", &[("bad", "pub func f(): int { return \"x\" }")]); + assert!(e.contains("bad.dino:1"), "{e}"); + assert!(e.contains("expected `int`"), "{e}"); +} + +#[test] +fn error_syntax_error_in_module() { + let e = project_err("import bad\nfunc main() {}", &[("bad", "func f( {")]); + assert!(e.contains("bad.dino"), "{e}"); +} + +#[test] +fn error_missing_module() { + let e = project_err("import nope\nfunc main() {}", &[]); + assert!(e.contains("cannot find module `nope`") && e.contains("nope.dino"), "{e}"); +} + +#[test] +fn error_circular_import() { + let e = project_err("import a\nfunc main() {}", &[("a", "import b"), ("b", "import a")]); + assert!(e.contains("circular import: a -> b -> a"), "{e}"); +} + +#[test] +fn error_unknown_name_in_module() { + let e = project_err("import utils\nfunc main() { print(utils.nothing(1)) }", &[("utils", UTILS)]); + assert!(e.contains("unknown function `nothing`"), "{e}"); +} + +#[test] +fn error_module_does_not_see_main_or_other_imports() { + // `b` imports nothing, so it can't use `os` + let e = project_err("import b\nfunc main() { print(b.f()) }", &[("b", "pub func f(): int { return os.exists(\"x\") }")]); + assert!(e.contains("b.dino"), "{e}"); +} + +#[test] +fn error_private_field() { + let m = "pub struct P {\n pub a: int\n b: int\n}\npub func make(): P { return P{1, 2} }"; + let e = project_err("import m\nfunc main() { var p = m.make()\nprint(p.b) }", &[("m", m)]); + assert!(e.contains("field `b` is private to `m`"), "{e}"); + let e = project_err("import m\nfunc main() { var p = m.P{a: 1, b: 2} }", &[("m", m)]); + assert!(e.contains("field `b` is private"), "{e}"); + assert_eq!(run_project("import m\nfunc main() { var p = m.P{a: 5}\nprint(p.a, m.make().a) }", &[("m", m)]), "5 1\n"); +} + +// ---------- move on last use ---------- + +fn c_of(src: &str) -> String { + driver::compile_to_c(src).unwrap_or_else(|d| panic!("{}", d.render("test.dino", src))) +} + +#[test] +fn last_use_moves_instead_of_copying() { + let src = "func main() { + var a = [1, 2, 3] + var b = a + b.push(4) + print(b) +}"; + let c = c_of(src); + let main_fn = &c[c.rfind("void d_main(void) {").unwrap()..]; + assert!(!main_fn.contains("_copy("), "the list should have been moved, not copied"); + assert_eq!(out_of(src), "[1, 2, 3, 4]\n"); +} + +#[test] +fn later_use_still_copies() { + let src = "func main() { + var a = [1, 2, 3] + var b = a + b.push(4) + print(a, b) +}"; + let c = c_of(src); + assert!(c[c.rfind("void d_main(void) {").unwrap()..].contains("_copy(")); + assert_eq!(out_of(src), "[1, 2, 3] [1, 2, 3, 4]\n"); +} + +#[test] +fn moves_inside_loops_are_not_done() { + let src = "func main() { + var a = [1] + for i in 0..3 { + var b = a + b.push(i) + print(b) + } +}"; + assert_eq!(out_of(src), "[1, 0]\n[1, 1]\n[1, 2]\n"); +} + +#[test] +fn moves_after_a_loop_and_in_branches() { + let src = "struct Box { + xs: []int + tag: string +} +func main() { + var a = Box{[1, 2], \"t\"} + for i in 0..2 { print(a.xs.len()) } + var s = \"hello\" + var t = s + var flag = true + if flag { + var c = a + c.xs.push(9) + print(c.xs, c.tag, t) + } else { + print(\"no\") + } +}"; + assert_eq!(out_of(src), "2\n2\n[1, 2, 9] t hello\n"); +} + +#[test] +fn same_statement_twice_is_not_moved() { + let src = "func join(a: []int, b: []int): []int { + var r = a + for x in b { r.push(x) } + return r +} +func main() { + var a = [1, 2] + print(join(a, a)) + var s = \"x\" + var p = [s, s] + print(p) +}"; + assert_eq!(out_of(src), "[1, 2, 1, 2]\n[x, x]\n"); +} + +#[test] +fn moved_value_in_calls_and_structs() { + let src = "struct W { xs: []int } +func take(xs: []int): int { xs.push(1) + return xs.len() } +func main() { + var a = [5, 6] + var w = W{a} + var b = [7] + print(take(b)) + var e = \"err\" + var r = error(e) + print(w.xs, r) +}"; + assert_eq!(out_of(src), "2\n[5, 6] err\n"); +} + +// ---------- generic functions ---------- + +const GENERICS: &str = "func max[T: Ordered](a, b: T): T { + if a > b { return a } + return b +} +func first[T](xs: []T): T { return xs[0] } +func pair[A, B](a: A, b: B): string { return \"{a}/{b}\" } +func sum[T: Number](xs: []T): T { + var total: T = 0 + for x in xs { total += x } + return total +} +func count_down[T: Number](n: T): T { + if n <= 0 { return n } + return count_down(n - 1) +} +struct P { x: int } +"; + +fn generic_err(body: &str) -> String { + let src = format!("{GENERICS}func main() {{\n{body}\n}}"); + driver::compile_to_c(&src).map(|_| String::new()).unwrap_or_else(|d| d.msg) +} + +#[test] +fn generic_functions_work_for_each_type() { + let src = format!( + "{GENERICS}func main() {{ + print(max(3, 9), max(2.5, 1), max(\"a\", \"b\")) + var small: u8 = 7 + print(max(small, 200)) + print(first([10, 20]), first([\"x\", \"y\"]), first([P{{4}}])) + print(pair(1, \"z\"), pair(true, 2.5)) + print(sum([1, 2, 3]), sum([1.5, 2.5]), count_down(3), count_down(2.0)) + var names = [\"b\", \"a\"] + var f = first(names) + names.push(\"c\") + print(f, names.len()) +}}" + ); + assert_eq!( + out_of(&src), + "9 2.5 b\n200\n10 x P{x: 4}\n1/z true/2.5\n6 4.0 0 0.0\nb 3\n" + ); +} + +#[test] +fn generic_errors_are_clear() { + let e = generic_err("print(max(1, 2.5, 3))"); + assert!(e.contains("takes 2 arguments"), "{e}"); + let e = generic_err("var a: u8 = 1\nvar b: i32 = 2\nprint(max(a, b))"); + assert!(e.contains("`T` is `u8` from an earlier argument, but this one is `i32`"), "{e}"); + let e = generic_err("print(max(P{1}, P{2}))"); + assert!(e.contains("needs `T` to be a number or a string"), "{e}"); + let e = generic_err("print(sum([\"a\"]))"); + assert!(e.contains("needs `T` to be a number"), "{e}"); + let e = generic_err("var e: []int\nprint(max(e, e))"); + assert!(e.contains("needs `T`"), "{e}"); +} + +#[test] +fn generic_declaration_errors() { + let e = driver::compile_to_c("func f[T: Whatever](a: T) {}\nfunc main() {}").unwrap_err().msg; + assert!(e.contains("unknown rule for `T`"), "{e}"); + let e = driver::compile_to_c("func f[T](a: int) {}\nfunc main() {}").unwrap_err().msg; + assert!(e.contains("needs at least one parameter that uses its type"), "{e}"); +} + +#[test] +fn generic_error_inside_body_names_the_types() { + let src = "func bad[T](a: T): T { return a + a }\nfunc main() { print(bad(true)) }"; + let e = driver::compile_to_c(src).unwrap_err().msg; + assert!(e.contains("bool"), "{e}"); +} + +#[test] +fn generic_functions_across_modules() { + let m = "pub func twice[T: Number](x: T): T { return x + x }\npub func helper[T](x: T): T { return x }"; + let main = "import m\nfunc main() { print(m.twice(4), m.twice(1.5)) }"; + assert_eq!(run_project(main, &[("m", m)]), "8 3.0\n"); + let e = project_err("import m\nfunc main() { print(m.twice(true)) }", &[("m", m)]); + assert!(e.contains("needs `T` to be a number"), "{e}"); +} + +// ---------- generic structs and enums ---------- + +fn type_err(src: &str) -> String { + driver::compile_to_c(src).map(|_| String::new()).unwrap_or_else(|d| d.msg) +} + +const BOX: &str = "struct Box[T] { + v: T + func get(): T { return v } + func set(x: T) { v = x } +} +enum Opt2[T] { + Has(x: T) + Not +} +"; + +#[test] +fn generic_struct_with_different_types_and_nesting() { + let src = format!( + "{BOX}func main() {{ + var a = Box[int]{{5}} + var b = Box[string]{{v: \"hi\"}} + var c = Box[Box[int]]{{a}} + a.set(6) + print(a.get(), b.get(), c.get().get(), c) + var l = Box[[]int]{{[1, 2]}} + l.v.push(3) + print(l.get()) + var o = Opt2[float].Has(1.5) + print(o, Opt2[float].Not) +}}" + ); + assert_eq!(out_of(&src), "6 hi 5 Box[Box[int]]{v: Box[int]{v: 5}}\n[1, 2, 3]\nOpt2[float].Has(1.5) Opt2[float].Not\n"); +} + +#[test] +fn generic_type_errors() { + let e = type_err(&format!("{BOX}func main() {{ var a = Box{{1}} }}")); + assert!(e.contains("`Box` needs types"), "{e}"); + let e = type_err(&format!("{BOX}func main() {{ var a: Box }}")); + assert!(e.contains("`Box` needs types"), "{e}"); + let e = type_err(&format!("{BOX}func main() {{ var a: Box[int, int] }}")); + assert!(e.contains("takes 1 type"), "{e}"); + let e = type_err(&format!("{BOX}func main() {{ var a = Box[int]{{\"x\"}} }}")); + assert!(e.contains("expected `int`"), "{e}"); + let e = type_err("struct P { x: int }\nfunc main() { var a: P[int] }"); + assert!(e.contains("not a generic type"), "{e}"); + let e = type_err("struct S[T: Number] { v: T }\nfunc main() { var a: S[string] }"); + assert!(e.contains("needs `T` to be a number"), "{e}"); + let e = type_err("struct S[T] { v: T }\nstruct S[U] { v: U }\nfunc main() {}"); + assert!(e.contains("already defined"), "{e}"); +} + +#[test] +fn indexing_still_works_next_to_generics() { + let src = format!( + "{BOX}func main() {{ + var Xs = [1, 2, 3] + var i = 1 + print(Xs[i], Xs[0] + Xs[2]) + var b = Box[int]{{Xs[i]}} + print(b.v) +}}" + ); + assert_eq!(out_of(&src), "2 4\n2\n"); +} + +#[test] +fn generic_types_across_modules() { + let m = "pub struct Stack[T] { + pub items: []T + pub func push_item(x: T) { items.push(x) } + func hidden(): int { return 1 } +} +pub enum Maybe[T] { Some(v: T)\n None } +struct Secret[T] { v: T } +pub func make[T](x: T): Stack[T] { var s = Stack[T]{}\n s.push_item(x)\n return s }"; + let main = "import m +func main() { + var s = m.Stack[int]{} + s.push_item(4) + print(s.items, m.make(\"z\").items) + print(m.Maybe[int].Some(2), m.Maybe[string].None) +}"; + assert_eq!(run_project(main, &[("m", m)]), "[4] [z]\nm.Maybe[int].Some(2) m.Maybe[string].None\n"); + let e = project_err("import m\nfunc main() { var s = m.Stack[int]{}\nprint(s.hidden()) }", &[("m", m)]); + assert!(e.contains("method `hidden` is private"), "{e}"); + let e = project_err("import m\nfunc main() { var s: m.Secret[int] }", &[("m", m)]); + assert!(e.contains("`Secret` is private"), "{e}"); +} + +// ---------- shared and weak ---------- + +const SHARED_TYPES: &str = "struct Node { + name: string + parent: weak Node? + kids: []shared Node + + func add(k: shared Node) { kids.push(k) } + func path(): string { + if parent == nil { return name } + return parent.path() + \"/\" + name + } +} +enum Light { + On(level: int) + Off +} +"; + +#[test] +fn shared_values_are_one_value_with_many_owners() { + let src = format!( + "{SHARED_TYPES}func main() {{ + var a = shared Node{{name: \"a\"}} + var b = a + b.name = \"changed\" + print(a.name, b.name) + var xs = [a, b, a] + xs[0].name = \"again\" + print(a.name, xs.len()) + var l = shared Light.On(3) + var m = l + match m {{ + On(v) => print(\"on\", v) + Off => print(\"off\") + }} +}}" + ); + assert_eq!(out_of(&src), "changed changed\nagain 3\non 3\n"); +} + +#[test] +fn weak_parents_do_not_leak_and_go_nil() { + let src = format!( + "{SHARED_TYPES}func make(): shared Node {{ + var root = shared Node{{name: \"root\"}} + var kid = shared Node{{name: \"kid\", parent: weak root}} + root.add(kid) + var leaf = shared Node{{name: \"leaf\", parent: weak kid}} + kid.add(leaf) + return leaf +}} +func main() {{ + var leaf = make() + print(leaf.parent == nil) + print(leaf.name) + var keep = shared Node{{name: \"top\"}} + var k2 = shared Node{{name: \"mid\", parent: weak keep}} + keep.add(k2) + var l2 = shared Node{{name: \"low\", parent: weak k2}} + k2.add(l2) + print(l2.path()) + var w = weak k2 + print(w == nil) + keep = shared Node{{name: \"new\"}} + k2 = shared Node{{name: \"x\"}} + print(w == nil, l2.path()) +}}" + ); + // `make` frees root and kid when it returns: leaf's parent (kid) is gone + assert_eq!(out_of(&src), "true\nleaf\ntop/mid/low\nfalse\ntrue mid/low\n".replace("true mid/low", "true low")); +} + +#[test] +fn shared_passed_returned_and_optional() { + let src = format!( + "{SHARED_TYPES}func rename(n: shared Node, to: string) {{ n.name = to }} +func pick(a, b: shared Node, first: bool): shared Node {{ + if first {{ return a }} + return b +}} +func main() {{ + var a = shared Node{{name: \"a\"}} + var b = shared Node{{name: \"b\"}} + rename(pick(a, b, false), \"B\") + print(a.name, b.name) + var maybe: shared Node? = nil + print(maybe == nil) + maybe = a + if maybe != nil {{ print(maybe.name) }} + var s = \"{{a.name}} and {{b.name}}\" + print(s) +}}" + ); + assert_eq!(out_of(&src), "a B\ntrue\na\na and B\n"); +} + +#[test] +fn shared_errors() { + let e = type_err(&format!("{SHARED_TYPES}func main() {{ var x = shared 5 }}")); + assert!(e.contains("only structs and enums"), "{e}"); + let e = type_err(&format!("{SHARED_TYPES}func main() {{ var n = Node{{name: \"a\"}}\nvar w = weak n }}")); + assert!(e.contains("`weak` points at a `shared` value"), "{e}"); + let e = type_err(&format!("{SHARED_TYPES}func main() {{ var n = shared Node{{name: \"a\"}}\nvar x: weak Node = n }}")); + assert!(e.contains("expected `weak Node`, found `shared Node`"), "{e}"); + let e = type_err(&format!("{SHARED_TYPES}func main() {{ var n = shared Node{{name: \"a\"}}\nvar x = n == nil }}")); + assert!(e.contains("can never be nil"), "{e}"); +} + +#[test] +fn empty_shared_and_gone_weak_panic_cleanly() { + let src = format!( + "{SHARED_TYPES}func main() {{ + var w: weak Node + print(w == nil) + print(w.name) +}}" + ); + let o = run_source(&src); + assert_eq!(o.stdout, "true\n"); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("gone"), "{}", o.stderr); +} + +// ---------- interfaces ---------- + +const SHAPES: &str = "interface Shape { + area(): float +} +struct Sq { + s: float + func area(): float { return s * s } +} +struct Wrong { + func area(): int { return 1 } +} +struct Nothing { + x: int +} +func show[T: Shape](v: T): float { return v.area() } +"; + +#[test] +fn interface_rule_accepts_any_type_with_the_methods() { + let src = format!("{SHAPES}func main() {{ print(show(Sq{{3.0}})) }}"); + assert_eq!(out_of(&src), "9.0\n"); +} + +#[test] +fn interface_errors_say_what_is_missing() { + let e = type_err(&format!("{SHAPES}func main() {{ print(show(Nothing{{1}})) }}")); + assert!(e.contains("`show` needs `T` to be something with the methods of `Shape`, but `Nothing` has no method `area`"), "{e}"); + let e = type_err(&format!("{SHAPES}func main() {{ print(show(Wrong{{}})) }}")); + assert!(e.contains("`Wrong` has `area(): int`, but `Shape` needs `area(): float`"), "{e}"); + let e = type_err(&format!("{SHAPES}func main() {{ print(show(5)) }}")); + assert!(e.contains("`int` has no method `area`"), "{e}"); + let e = type_err(&format!("{SHAPES}func main() {{ var s: Shape = Nothing{{1}} }}")); + assert!(e.contains("expected `Shape`, found `Nothing`: `Nothing` has no method `area`"), "{e}"); + let e = type_err("func f[T: Missing](x: T) {}\nfunc main() {}"); + assert!(e.contains("unknown rule for `T`"), "{e}"); + let e = type_err("interface I { f() }\nstruct I { x: int }\nfunc main() {}"); + assert!(e.contains("already defined"), "{e}"); +} + +#[test] +fn interfaces_across_modules() { + let m = "pub interface Named { name(): string }\nstruct Hidden { x: int }\n\ + pub func greet[T: Named](x: T): string { return \"hi \" + x.name() }"; + let main = "import m +struct Dog { + func name(): string { return \"rex\" } +} +func call[T: m.Named](x: T): string { return x.name() } +func main() { print(m.greet(Dog{}), call(Dog{})) }"; + assert_eq!(run_project(main, &[("m", m)]), "hi rex rex\n"); + let m2 = "interface Secret { f() }"; + let e = project_err("import m\nfunc g[T: m.Secret](x: T) {}\nfunc main() {}", &[("m", m2)]); + assert!(e.contains("`Secret` is private"), "{e}"); +} + +// ---------- closures ---------- + +#[test] +fn closures_capture_copies_and_keep_their_own_state() { + let src = "func make(): func(): int { + var n = 0 + return func(): int { + n += 1 + return n + } +} +func main() { + var x = 5 + var f = func(): int { return x * 2 } + x = 100 + print(f(), x) + var a = make() + var b = make() + print(a(), a(), b()) + var c = a + print(a(), c(), c()) + var xs = [1, 2] + var g = func(): int { xs.push(3) + return xs.len() } + print(g(), g(), xs.len()) +}"; + assert_eq!(out_of(src), "10 100\n1 2 1\n3 3 4\n3 4 2\n"); +} + +#[test] +fn closures_nest_and_capture_strings_lists_structs() { + let src = "struct P { name: string + tags: []string } +func main() { + var p = P{\"bob\", [\"a\", \"b\"]} + var greet = func(g: string): func(): string { + return func(): string { return g + \" \" + p.name + \" \" + p.tags.join(\",\") } + } + var f = greet(\"hi\") + p.name = \"changed\" + print(f()) + var fs: []func(int): int + for i in 0..3 { fs.push(func(x: int): int { return x + i }) } + for f2 in fs { print(f2(10)) } +}"; + assert_eq!(out_of(src), "hi bob a,b\n10\n11\n12\n"); +} + +#[test] +fn closures_in_methods_see_fields() { + let src = "struct Counter { + n: int + func adder(): func(int): int { + return func(k: int): int { return n + k } + } + func bump() { n += 1 } +} +func main() { + var c = Counter{10} + var add = c.adder() + c.bump() + print(add(1), c.n) +}"; + assert_eq!(out_of(src), "11 11\n"); +} + +#[test] +fn generic_functions_take_short_closures() { + let src = "func map[T, U](xs: []T, f: func(T): U): []U { + var out: []U + for x in xs { out.push(f(x)) } + return out +} +func fold[T, A](xs: []T, start: A, f: func(A, T): A): A { + var acc = start + for x in xs { acc = f(acc, x) } + return acc +} +func main() { + print(map([1, 2, 3], x => x + 1)) + print(map([1.5, 2.5], x => int(x))) + print(fold([1, 2, 3], 0, (a, x) => a + x)) + print(fold([\"a\", \"b\"], \"\", (s, x) => s + x)) +}"; + assert_eq!(out_of(src), "[2, 3, 4]\n[1, 2]\n6\nab\n"); +} + +#[test] +fn closure_errors() { + let e = type_err("func main() { var f = x => x }"); + assert!(e.contains("can't tell what type `x` is"), "{e}"); + let e = type_err("func main() { var f: func(int): int = func(x: int): string { return \"a\" } }"); + assert!(e.contains("expected `func(int): int`, found `func(int): string`"), "{e}"); + let e = type_err("func main() { var f = func(x: int): int { print(x) } }"); + assert!(e.contains("can reach its end without returning"), "{e}"); + let e = type_err("func main() { var f = func(x: int): int { return x }\nprint(f(1, 2)) }"); + assert!(e.contains("takes 1 argument"), "{e}"); + let e = type_err("func main() { var n = 5\nprint(n(1)) }"); + assert!(e.contains("is a variable, not a function"), "{e}"); + let e = type_err("func call(f: func(int): int): int { return f(1) }\nfunc main() { print(call((a, b) => a)) }"); + assert!(e.contains("has 2 parameters, but 1 is expected"), "{e}"); +} + +#[test] +fn empty_function_value_panics_cleanly() { + let src = "func main() { + var f: func(): int + print(f()) +}"; + let o = run_source(src); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("empty function value"), "{}", o.stderr); +} + +// ---------- interface values ---------- + +#[test] +fn interface_values_mix_types_and_copy() { + let src = format!( + "{SHAPES}func main() {{ + var xs: []Shape = [Sq{{2.0}}, Sq{{3.0}}] + var total = 0.0 + for x in xs {{ total += x.area() }} + print(total, xs.len()) + var a: Shape = Sq{{1.0}} + var b = a + print(a.area(), b.area(), show(a)) + var maybe: Shape + print(maybe == nil) + maybe = Sq{{4.0}} + print(maybe != nil, maybe.area()) +}}" + ); + assert_eq!(out_of(&src), "13.0 2\n1.0 1.0 1.0\ntrue\ntrue 16.0\n"); +} + +#[test] +fn interface_value_methods_can_change_the_value() { + let src = "interface Counter { bump(): int } +struct C { n: int + func bump(): int { n += 1 + return n } } +func run(c: Counter): int { c.bump() + return c.bump() } +func main() { + var c: Counter = C{0} + print(c.bump(), c.bump()) + print(run(c), c.bump()) +}"; + assert_eq!(out_of(src), "1 2\n4 3\n"); +} + +#[test] +fn empty_interface_value_panics_cleanly() { + let src = "interface I { f(): int }\nfunc main() { var x: I\nprint(x.f()) }"; + let o = run_source(src); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("empty interface value"), "{}", o.stderr); +} + +#[test] +fn interface_values_across_modules() { + let m = "pub interface Named { name(): string } +pub struct Dog { + pub func name(): string { return \"rex\" } +} +pub func all(): []Named { return [Dog{}] }"; + let main = "import m +struct Cat { func name(): string { return \"tom\" } } +func main() { + var xs = m.all() + xs.push(Cat{}) + for x in xs { print(x.name()) } + var one: m.Named = Cat{} + print(one.name()) +}"; + assert_eq!(run_project(main, &[("m", m)]), "rex\ntom\ntom\n"); +} + +// ---------- weak -> shared ---------- + +#[test] +fn weak_upgrade_gives_a_shared_value_or_nil() { + let src = format!( + "{SHARED_TYPES}func main() {{ + var root = shared Node{{name: \"root\"}} + var w = weak root + var p = w.upgrade() + print(p == nil) + if p != nil {{ + p.name = \"seen through upgrade\" + print(p.name) + }} + print(root.name) + // the upgraded pointer is an owner: the value stays alive while it exists + var keep = shared Node{{name: \"keep\"}} + var w2 = weak keep + var strong = w2.upgrade() + keep = shared Node{{name: \"other\"}} + print(w2 == nil) + strong = nil + print(w2 == nil, w2.upgrade() == nil) +}}" + ); + assert_eq!(out_of(&src), "false\nseen through upgrade\nseen through upgrade\nfalse\ntrue true\n"); +} + +#[test] +fn weak_upgrade_of_an_empty_weak_is_nil() { + let src = format!("{SHARED_TYPES}func main() {{ var w: weak Node\nprint(w.upgrade() == nil) }}"); + assert_eq!(out_of(&src), "true\n"); +} + +#[test] +fn string_slicing() { + let o = out_of("func main() {\n var s = \"hello world\"\n print(s[0..5])\n print(s[6..])\n print(s[..5])\n print(s[3..3] == \"\")\n}"); + assert_eq!(o, "hello\nworld\nhello\ntrue\n"); +} + +#[test] +fn string_slice_out_of_range_panics() { + let o = run_source("func main() {\n var s = \"abc\"\n var n = 5\n print(s[1..n])\n}"); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("the length is 3"), "{}", o.stderr); +} + +#[test] +fn calc_example_session() { + let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/examples/calc"); + let main = format!("{dir}/main.dino"); + let c = driver::compile_file(&main).unwrap_or_else(|e| panic!("{e}")); + let exe = driver::temp_exe(); + driver::build_exe_with(&c, &exe, &["-std=c11", "-fwrapv", "-w", "-fsanitize=address", "-g"]).unwrap(); + let o = Command::new(&exe) + .arg(format!("{dir}/session.txt")) + .env("ASAN_OPTIONS", "detect_leaks=1") + .output() + .unwrap(); + let _ = std::fs::remove_file(&exe); + let err = String::from_utf8_lossy(&o.stderr); + assert!(!err.contains("Sanitizer"), "{err}"); + let want = std::fs::read_to_string(format!("{dir}/session.out")).unwrap(); + assert_eq!(String::from_utf8_lossy(&o.stdout), want); +} + +#[test] +fn map_missing_key_panics() { + let o = run_source("func main() {\n var m = {\"a\": 1}\n print(m[\"b\"])\n}"); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("not in the map"), "{}", o.stderr); +} + +#[test] +fn map_counts_from_zero_and_keeps_order() { + let o = out_of("func main() {\n var m: map[int, int] = {}\n m[5] += 2\n m[1] += 1\n m[5] += 3\n print(m)\n}"); + assert_eq!(o, "{5: 5, 1: 1}\n"); +} + +#[test] +fn map_errors() { + assert!(check_err(&main_with("var m = {1.5: 2}")).contains("map key")); + assert!(check_err(&main_with("var m = {}")).contains("can't tell what kind of map")); + assert!(check_err(&main_with("var m = {\"a\": 1}\n m[1] = 2")).contains("string")); + assert!(check_err(&main_with("var m = {\"a\": 1}\n m.bogus()")).contains("maps have no method")); +} + +/// Every complete program in the docs folder must compile, and print what the text says. +#[test] +fn docs_examples_run() { + let dir = concat!(env!("CARGO_MANIFEST_DIR"), "/docs"); + let mut checked = 0; + for entry in std::fs::read_dir(dir).unwrap() { + let path = entry.unwrap().path(); + if path.extension().map_or(true, |e| e != "md") { + continue; + } + let text = std::fs::read_to_string(&path).unwrap(); + let mut rest = text.as_str(); + while let Some(start) = rest.find("```dino\n") { + let after = &rest[start + 8..]; + let end = after.find("```").unwrap(); + let code = &after[..end]; + rest = &after[end + 3..]; + if !code.contains("func main") || code.contains("import utils") { + continue; + } + // the output block, if one follows right away + let expected = rest.strip_prefix("\n```text\n").and_then(|r| r.find("```").map(|e| &r[..e])); + let o = run_source(code); + assert_eq!(o.code, 0, "{} failed:\n{code}\n{}", path.display(), o.stderr); + if let Some(want) = expected { + assert_eq!(o.stdout, want, "wrong output in {}:\n{code}", path.display()); + } + checked += 1; + } + } + assert!(checked >= 40, "only {checked} doc examples found"); +} + +#[test] +fn char_in_the_middle_of_a_character_panics() { + let o = run_source("func main() {\n var s = \"é\"\n print(s[1])\n}"); + assert_eq!(o.code, 2); + assert!(o.stderr.contains("middle of a character"), "{}", o.stderr); +} + +#[test] +fn char_errors() { + assert!(check_err(&main_with("var c = 'ab'")).contains("exactly one character")); + assert!(check_err(&main_with("var c = ''")).contains("empty character")); + assert!(check_err(&main_with("var c: char = 65")).contains("char")); + assert!(check_err(&main_with("var c = 'a' + 'b'")).contains("doesn't work on `char`")); + assert!(check_err(&main_with("var s = \"abc\"\n s[0] = 'x'")).contains("can't be changed in place")); + assert!(check_err(&main_with("var c = 'a'\n c.bogus()")).contains("characters have no method")); +} + +#[test] +fn string_chars_list() { + let out = out_of( + "func main() {\n var cs = \"héllo, 世界\".chars()\n print(cs.len(), cs[1], cs[7])\n var up: []char = \"abc\".chars()\n up.push('d')\n print(up, \"\".chars().len())\n for c in \"xy\".chars() { print(c) }\n}\n", + ); + assert_eq!(out, "9 é 世\n[a, b, c, d] 0\nx\ny\n"); +}