import "zlib.h" reads the header (cc -E + a small C declaration parser), links via pkg-config or link "name", and builds .c/.cpp files next to the program. C structs become Dino structs, pointers become pointer types (nil = NULL), T* parameters take variables or lists. Adds dino header, editor completion/hover for C names, docs chapter 13, a raylib example, and tests. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
19 KiB
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 (
selfavailable for the whole value). No receiver, nomutkeyword: the compiler infers whether a method mutates. -
Variables:
var x = 1creates a changeable variable (type inferred;var x: int = 1with a type).x = 2changes it.const X = 1is 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 dataCircle(r: float), generic bounds[T: Ordered], several resultsfunc 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;i64isintandf64isfloat(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 withu8(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.f32prints 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?withnil. Compiler requires a nil check before use. -
Strings: interpolation with
"Hello {name}"; literal brace is\{. -
Visibility: private by default,
pubmakes it public. -
Loops:
foronly:for i in 0..10,for x in items,for cond { },for { }(infinite), withstepfor strides. No C-style three-part loop. -
Numbers:
int(64-bit) andfloat(64-bit) for everyday code; sized typesi8 i16 i32 i64 u8 u16 u32 u64 f32 f64when exact control is needed.var a = 42isint,var b = 3.14isfloat. -
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). Useint(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
constmay not call functions; uses are replaced by the value. -
Structs are values:
var b = acopies; passing to a function copies. Methods go inside the struct and see fields directly;selfis 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 aconstor on a temporary likemake().update(). -
Struct literals: positional
Vec2{1, 2}needs every field; namedPlayer{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 = aand 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.printshows[1, 2]. Out-of-range index/slice andpop()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 showsShape.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 maybreak/continue/return). As a value (var x = match ...,return match ...), every arm is a single expression, or a lonereturn/break/continue. Arms of a valuematchmust have the same type. -
Errors:
erroris a built-in type: nil (no error) or a message. Make one witherror("msg {x}"); print it or use it in"{err}"(nil printsnil). A function may return several values:func f(): (int, error); take them withvar n, err = f()(_throws one away).return v, nilor the shorthandreturn error("x")(zero values are filled in). The error ignored by a baref()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 iserror; works on(T, error)anderrorcalls.func main(): erroris allowed: an error printserror: msgto stderr, exit code 1. -
Optionals:
T?holds aTornil. ATconverts toT?implicitly. Compare withx == nil/x != nil. To use the value, check first: the compiler knowsxis set insideif x != nil { }, afterif x == nil { return }(or break/continue), and on the right ofx != nil &&/x == nil ||. Assigning toxforgets 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!,matchon nil,?on optionals. -
Last use moves:
var b = acopiesa, unlessais never used again (and no loop is in the way): thenbtakes 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 becomeint, orfloatif any is a float); there is nomax[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) andNumber; 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 }, thenfunc show[T: Shape](s: T)(alsostruct 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), ands.area()calls the method of whatever is inside. Interface values are values like any other:var t = scopies the inside, and a method that changes the value (s.scale(2.0)) changes only that variable's copy.nilis 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 filem.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 byvar b = a, so two owners really are one value. -
Weak pointers:
weak wingives a non-owning pointer to a shared value (typeweak 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 isweak).w == nil/w != niltell 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 asnil(Button{label: "x"}) and can be assignednil. To use the value as an owner,w.upgrade()gives ashared T?: the shared value (now with one more owner, so it stays alive while you hold it) ornilif 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: resultmeans nothing is returned). Write one asfunc(x: int): int { return x + 1 }, or short asx => x + 1/(a, b) => a + bwhere 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 += 1inside 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 changesself. 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,continueand?). 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) orimport utils(the fileutils.dinonext to the main file;import tools.fsistools/fs.dino, used asfs.name). Use things asutils.double(2),utils.Vec2{1.0, 2.0},utils.Shape.Circle(1.0),utils.SCALE, andutils.Vec2in types. Onlypubfunctions, consts, types, methods and fields can be used from another file (fields are private too unless writtenpub x: int). Each file has its own imports and its own names, so two files may both definef. 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{...}). C headers: see C libraries below. -
Built-in modules:
os(read_file write_file append_file exists exit env read_line, and the valueargs),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;[]stringhasjoin. -
Maps
map[K, V]:{"a": 1}(types guessed),map[string, int]{}orvar m: map[string, int] = {}(empty). Keys: int/whole-number types, string, bool. Values: anything exceptT?.m[k] = vadds or replaces,m[k]panics if the key is missing,m[k] += 1treats a missing key as zero, methodshas get get_or remove keys values len,for k, v in m/for k in m. Entries keep insertion order (hash index + ordered arrays;removeis O(n)). Maps are values (var b = acopies). Not yet:==, genericmap[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== != < > <= >=,matchon 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 swalk characters (i= byte position, invalid UTF-8 comes out as U+FFFD).s[i]is the character starting at bytei(panics out of range or in the middle of a character); strings can't be assigned to. Methodsis_digit is_letter is_alnum is_space is_upper is_lower upper lower(ASCII plus Latin-1 letters).s.chars()gives a[]char. Not yet:[]char→string. -
Formatter
dino fmt [--check] <file|folder>..(src/fmt.rs): text based, so comments and blank lines survive. 4-space indent, spaces around binary operators and after,:, tightPoint{...}/ 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 lspis a language server (src/lsp.rs, own JSON insrc/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). Also outline, go to definition, hover, completion and signature help from a token index (src/index.rs), including names from C headers. Not yet: references, rename; the compiler reports only the first error. -
C libraries (
src/cheader.rs):import "zlib.h"reads the header (no hand-written declarations): it runscc -E -dDand parses the declarations, enum values and object-like#defines from the header and the headers it includes itself (in a shared folder like /usr/include: the header and what it includes, minus the C library). Names are used aszlib.name(prefix = file name without.h;import z "zlib.h"picks one);import "zlib.h" { a, b }makes only those names visible, without a prefix. Linking:pkg-configwith names guessed from the header path, elseimport "x.h" link "x"(-lx). A header next to the program adds-Iand builds the.c/.cpp/.ccfile with the same name into the program (C++ withc++, linked with-lstdc++). Types: C ints -> sized numbers (inti32,longint,size_tu64),doublefloat,chari8 (takes'a'),char *-> string (copied both ways, NULL -> ""), enums ->i32constants, plain structs -> Dino structs with the C field names (raylib.Color; converted field by field at calls), other pointers -> pointer types (Ty::CPtr,stdio.FILE,nil= NULL,==compares);void *(pointer) mixes with every pointer type.T *parameters take a variable (the address is passed; counts as a change for inferred mutation) or a list (its items);const T *also takes any value. Constants: numbers, strings, and struct literals likeCLITERAL(Color){...}(emitted as the macro). Programs that import headers are built with-std=gnu11. Not supported (a clear error when called): variadic functions, callbacks, unions/arrays by value, lists of structs,long double, function-like macros. Dino never frees C memory.dino header x.h [word]lists what Dino sees. -
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 cpassesa b cto the program (os.args). -
Example:
examples/calc/is a multi-file calculator (tokenizer, parser, evaluator, shared scope);session.txt/session.outare 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).