Step 6: first library, examples, check and bench lib/Rt.jed (fail, eq), lib/Sys.linux-x86_64.jed (write, exit) and lib/Out.jed. Examples: hello, args, variants and slices, volatile, a code procedure. test/run holds programs with their expected output and status, test/err programs that must not compile and what they must say. make check runs them all; make bench times the compiler on a generated program of 348,000 lines and fails below 400,000 lines a second; make loc counts the compiler's lines.
| @@ -5,7 +5,7 @@ | ||
| 5 | 5 | FPCFLAGS = -O2 -Mobjfpc -Sh- -vew -FUbuild |
| 6 | 6 | |
| 7 | 7 | jed: src/*.pas | build |
| 8 | - $(FPC) $(FPCFLAGS) -ojed src/jed.pas | |
| 8 | + $(FPC) $(FPCFLAGS) -FE. -ojed src/jed.pas | |
| 9 | 9 | |
| 10 | 10 | units: src/*.pas | build |
| 11 | 11 | for u in scan sym x64 link elf; do $(FPC) $(FPCFLAGS) src/$$u.pas || exit 1; done |
| @@ -22,5 +22,23 @@ | ||
| 22 | 22 | |
| 23 | 23 | clean: |
| 24 | 24 | rm -rf build jed |
| 25 | + | |
| 26 | +# every example and test, against what it should print | |
| 27 | +check: jed | |
| 28 | + test/check.sh | |
| 25 | 29 | |
| 26 | -.PHONY: units tests clean | |
| 30 | +# the compiler's speed, on a generated program of about 350,000 lines; | |
| 31 | +# fails below 400,000 lines a second | |
| 32 | +bench: jed | build | |
| 33 | + python3 test/genbench.py 12000 > build/Bench.jed | |
| 34 | + @lines=$$(wc -l < build/Bench.jed); \ | |
| 35 | + start=$$(date +%s%N); ./jed build/Bench.jed || exit 1; end=$$(date +%s%N); \ | |
| 36 | + rate=$$((lines * 1000000000 / (end - start))); \ | |
| 37 | + echo "$$lines lines in $$(((end - start) / 1000000)) ms: $$rate lines a second"; \ | |
| 38 | + test $$rate -ge 400000 | |
| 39 | + | |
| 40 | +# lines of the compiler: all of them, and those that aren't blank or comments | |
| 41 | +loc: | |
| 42 | + @python3 test/loc.py src/*.pas | |
| 43 | + | |
| 44 | +.PHONY: units tests check bench loc clean |
Binary file; the patch has it.
| @@ -0,0 +1,2 @@ | ||
| 1 | +one | |
| 2 | +two words |
| @@ -0,0 +1,2 @@ | ||
| 1 | +one | |
| 2 | +two words |
| @@ -0,0 +1,25 @@ | ||
| 1 | +(* Prints its arguments, one per line, and exits with how many. *) | |
| 2 | +module Args; | |
| 3 | +import Out; | |
| 4 | + | |
| 5 | +(* The length of a C string. *) | |
| 6 | +proc len(s: ^U8): I64; | |
| 7 | +var n: I64; | |
| 8 | +begin | |
| 9 | + n := 0; | |
| 10 | + while s[n] # 0 do n := n + 1; end; | |
| 11 | + return n; | |
| 12 | +end; | |
| 13 | + | |
| 14 | +proc main*: I64; | |
| 15 | +var i: I64; s: ^U8; | |
| 16 | +begin | |
| 17 | + for i := 1 to ARGC() - 1 do | |
| 18 | + s := ARGV()[i]; | |
| 19 | + Out.str(s[0..len(s)]); | |
| 20 | + Out.ln; | |
| 21 | + end; | |
| 22 | + return ARGC() - 1; | |
| 23 | +end; | |
| 24 | + | |
| 25 | +end Args. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
Binary file; the patch has it.
| @@ -0,0 +1 @@ | ||
| 1 | +vendor is printable |
| @@ -0,0 +1,29 @@ | ||
| 1 | +(* A code procedure: machine code written as bytes, called like any | |
| 2 | + other procedure. cpuid's vendor string, from leaf 0. *) | |
| 3 | +module Cpu; | |
| 4 | +import Out; | |
| 5 | + | |
| 6 | +(* cpuid leaf 0: puts the 12 bytes of the vendor in buf, rdi. *) | |
| 7 | +proc vendor(buf: ^U8); | |
| 8 | +code 53H, (* push rbx *) | |
| 9 | + 49H, 89H, 0F8H, (* mov r8, rdi *) | |
| 10 | + 31H, 0C0H, (* xor eax, eax *) | |
| 11 | + 0FH, 0A2H, (* cpuid *) | |
| 12 | + 41H, 89H, 18H, (* mov [r8], ebx *) | |
| 13 | + 41H, 89H, 50H, 04H, (* mov [r8+4], edx *) | |
| 14 | + 41H, 89H, 48H, 08H, (* mov [r8+8], ecx *) | |
| 15 | + 5BH, (* pop rbx *) | |
| 16 | + 0C3H; (* ret *) | |
| 17 | + | |
| 18 | +proc main*; | |
| 19 | +var buf: [12]U8; i: I64; ok: BOOL; | |
| 20 | +begin | |
| 21 | + vendor(ADR(buf[0])); | |
| 22 | + ok := true; | |
| 23 | + for i := 0 to 11 do | |
| 24 | + if (buf[i] < ' ') or (buf[i] > '~') then ok := false; end; | |
| 25 | + end; | |
| 26 | + if ok then Out.str("vendor is printable\n"); end; | |
| 27 | +end; | |
| 28 | + | |
| 29 | +end Cpu. |
Binary file; the patch has it.
| @@ -0,0 +1 @@ | ||
| 1 | +hello, world |
| @@ -0,0 +1,9 @@ | ||
| 1 | +module Hello; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +proc main*; | |
| 5 | +begin | |
| 6 | + Out.str("hello, world\n"); | |
| 7 | +end; | |
| 8 | + | |
| 9 | +end Hello. |
Binary file; the patch has it.
| @@ -0,0 +1,3 @@ | ||
| 1 | +all: 334 | |
| 2 | +last two: 2 | |
| 3 | +none: 0 |
| @@ -0,0 +1,39 @@ | ||
| 1 | +(* Variants and slices: a list of shapes, measured. *) | |
| 2 | +module Shapes; | |
| 3 | +import Out; | |
| 4 | + | |
| 5 | +type | |
| 6 | + Shape = variant | |
| 7 | + circle(r: I64); | |
| 8 | + rect(w, h: I64); | |
| 9 | + dot; | |
| 10 | + end; | |
| 11 | + | |
| 12 | +(* Twice the area, rounded down, of a circle taken as pi = 22/7. *) | |
| 13 | +proc area(s: Shape): I64; | |
| 14 | +begin | |
| 15 | + match s of | |
| 16 | + | circle(r): return 44 * r * r div 7; | |
| 17 | + | rect(w, h): return 2 * w * h; | |
| 18 | + | dot: return 0; | |
| 19 | + end; | |
| 20 | +end; | |
| 21 | + | |
| 22 | +proc total(shapes: []Shape): I64; | |
| 23 | +var i, sum: I64; | |
| 24 | +begin | |
| 25 | + sum := 0; | |
| 26 | + for i := 0 to LEN(shapes) - 1 do sum := sum + area(shapes[i]); end; | |
| 27 | + return sum; | |
| 28 | +end; | |
| 29 | + | |
| 30 | +proc main*; | |
| 31 | +var shapes: [4]Shape; | |
| 32 | +begin | |
| 33 | + shapes := [circle(7), rect(3, 4), dot, rect(1, 1)]; | |
| 34 | + Out.str("all: "); Out.int(total(shapes)); Out.ln; | |
| 35 | + Out.str("last two: "); Out.int(total(shapes[2..4])); Out.ln; | |
| 36 | + Out.str("none: "); Out.int(total(shapes[0..0])); Out.ln; | |
| 37 | +end; | |
| 38 | + | |
| 39 | +end Shapes. |
Binary file; the patch has it.
| @@ -0,0 +1,2 @@ | ||
| 1 | +abc | |
| 2 | +1 255 |
| @@ -0,0 +1,32 @@ | ||
| 1 | +(* A record laid over memory that something else may change: here a | |
| 2 | + page of the program's own, as a device's registers would be. Every | |
| 3 | + access through the ^VOLATILE pointer is one instruction of its | |
| 4 | + size, in the order written. *) | |
| 5 | +module Volatile; | |
| 6 | +import Out; | |
| 7 | + | |
| 8 | +type | |
| 9 | + Uart = record | |
| 10 | + data: U32; | |
| 11 | + status: U32; | |
| 12 | + control: U8; | |
| 13 | + end; | |
| 14 | + | |
| 15 | +var page: [64]U8; | |
| 16 | + | |
| 17 | +proc main*; | |
| 18 | +var dev: ^VOLATILE Uart; i: I64; | |
| 19 | +begin | |
| 20 | + dev := ^VOLATILE Uart(ADR(page)); | |
| 21 | + dev^.control := 1; | |
| 22 | + dev^.status := 0FFH; | |
| 23 | + for i := 0 to 2 do | |
| 24 | + dev^.data := U32('a' + i); | |
| 25 | + Out.char(U8(dev^.data)); | |
| 26 | + end; | |
| 27 | + Out.ln; | |
| 28 | + FENCE; | |
| 29 | + Out.int(I64(page[8])); Out.str(" "); Out.int(I64(dev^.status)); Out.ln; | |
| 30 | +end; | |
| 31 | + | |
| 32 | +end Volatile. |
| @@ -0,0 +1,57 @@ | ||
| 1 | +(* Writing text and numbers to standard output. Nothing is buffered: | |
| 2 | + each call is one write. *) | |
| 3 | +module Out; | |
| 4 | +import Sys; | |
| 5 | + | |
| 6 | +proc str*(s: []U8); | |
| 7 | +begin | |
| 8 | + Sys.write(Sys.stdout, s); | |
| 9 | +end; | |
| 10 | + | |
| 11 | +proc char*(c: U8); | |
| 12 | +var b: [1]U8; | |
| 13 | +begin | |
| 14 | + b[0] := c; | |
| 15 | + Sys.write(Sys.stdout, b); | |
| 16 | +end; | |
| 17 | + | |
| 18 | +proc ln*; | |
| 19 | +begin | |
| 20 | + str("\n"); | |
| 21 | +end; | |
| 22 | + | |
| 23 | +(* n in decimal *) | |
| 24 | +proc int*(n: I64); | |
| 25 | +var buf: [20]U8; i: I64; u: U64; | |
| 26 | +begin | |
| 27 | + u := U64(n); | |
| 28 | + if n < 0 then u := 0 - u; end; | |
| 29 | + i := 20; | |
| 30 | + repeat | |
| 31 | + i := i - 1; | |
| 32 | + buf[i] := U8(u mod 10) + '0'; | |
| 33 | + u := u div 10; | |
| 34 | + until u = 0; | |
| 35 | + if n < 0 then | |
| 36 | + i := i - 1; | |
| 37 | + buf[i] := '-'; | |
| 38 | + end; | |
| 39 | + str(buf[i..20]); | |
| 40 | +end; | |
| 41 | + | |
| 42 | +(* n in hexadecimal, digits digits at least *) | |
| 43 | +proc hex*(n: U64; digits: I64); | |
| 44 | +var buf: [16]U8; i: I64; d: U8; | |
| 45 | +begin | |
| 46 | + i := 16; | |
| 47 | + repeat | |
| 48 | + i := i - 1; | |
| 49 | + d := U8(n and 15); | |
| 50 | + if d < 10 then buf[i] := d + '0'; else buf[i] := d - 10 + 'A'; end; | |
| 51 | + n := n shr 4; | |
| 52 | + digits := digits - 1; | |
| 53 | + until (n = 0) and (digits <= 0) or (i = 0); | |
| 54 | + str(buf[i..16]); | |
| 55 | +end; | |
| 56 | + | |
| 57 | +end Out. |
| @@ -0,0 +1,37 @@ | ||
| 1 | +(* What the compiler's code calls: fail when a check fails, eq when = | |
| 2 | + or # compares two strings. The compiler finds them by these names. *) | |
| 3 | +module Rt; | |
| 4 | +import Sys; | |
| 5 | + | |
| 6 | +(* A check failed at line of file: says so on standard error and stops | |
| 7 | + the program with status 2. *) | |
| 8 | +proc fail*(file: ^U8; line: I64); | |
| 9 | +var buf: [20]U8; i, n: I64; | |
| 10 | +begin | |
| 11 | + n := 0; | |
| 12 | + while file[n] # 0 do n := n + 1; end; | |
| 13 | + Sys.write(Sys.stderr, file[0..n]); | |
| 14 | + i := 20; | |
| 15 | + repeat | |
| 16 | + i := i - 1; | |
| 17 | + buf[i] := U8(line mod 10) + '0'; | |
| 18 | + line := line div 10; | |
| 19 | + until line = 0; | |
| 20 | + i := i - 1; | |
| 21 | + buf[i] := ':'; | |
| 22 | + Sys.write(Sys.stderr, buf[i..20]); | |
| 23 | + Sys.write(Sys.stderr, ": index out of range\n"); | |
| 24 | + Sys.exit(2); | |
| 25 | +end; | |
| 26 | + | |
| 27 | +proc eq*(a, b: []U8): BOOL; | |
| 28 | +var i: I64; | |
| 29 | +begin | |
| 30 | + if LEN(a) # LEN(b) then return false; end; | |
| 31 | + for i := 0 to LEN(a) - 1 do | |
| 32 | + if a[i] # b[i] then return false; end; | |
| 33 | + end; | |
| 34 | + return true; | |
| 35 | +end; | |
| 36 | + | |
| 37 | +end Rt. |
| @@ -0,0 +1,24 @@ | ||
| 1 | +(* System calls on Linux for x86-64. Other targets supply their own | |
| 2 | + Sys, with the same exports. *) | |
| 3 | +module Sys; | |
| 4 | + | |
| 5 | +const | |
| 6 | + stdin* = 0; | |
| 7 | + stdout* = 1; | |
| 8 | + stderr* = 2; | |
| 9 | + | |
| 10 | +(* Writes s to the file descriptor fd; the number of bytes written, or | |
| 11 | + minus an errno. *) | |
| 12 | +proc write*(fd: I64; s: []U8): I64; | |
| 13 | +begin | |
| 14 | + if LEN(s) = 0 then return 0; end; | |
| 15 | + return SYSCALL(1, fd, ADR(s[0]), LEN(s)); | |
| 16 | +end; | |
| 17 | + | |
| 18 | +(* Ends the program, at once, with status. *) | |
| 19 | +proc exit*(status: I64); | |
| 20 | +begin | |
| 21 | + SYSCALL(231, status); | |
| 22 | +end; | |
| 23 | + | |
| 24 | +end Sys. |
| @@ -0,0 +1,58 @@ | ||
| 1 | +#!/bin/sh | |
| 2 | +# Compiles and runs every example and test, and compares what each | |
| 3 | +# prints, and its exit status, with what is expected: | |
| 4 | +# X.jed the program | |
| 5 | +# X.expect its standard output | |
| 6 | +# X.args its arguments, one per line (optional) | |
| 7 | +# X.status its exit status (optional; 0 if missing) | |
| 8 | +# Programs in test/err must not compile, and must print X.err. | |
| 9 | + | |
| 10 | +jed=./jed | |
| 11 | +out=build/check | |
| 12 | +mkdir -p $out | |
| 13 | +pass=0 | |
| 14 | +fail=0 | |
| 15 | + | |
| 16 | +run() { # run dir name | |
| 17 | + dir=$1; name=$2 | |
| 18 | + if ! $jed -I $dir/mods -o $out/$name $dir/$name.jed 2>$out/$name.cerr; then | |
| 19 | + echo "FAIL $dir/$name: doesn't compile: $(cat $out/$name.cerr)" | |
| 20 | + fail=$((fail + 1)); return | |
| 21 | + fi | |
| 22 | + set -- | |
| 23 | + if [ -f $dir/$name.args ]; then | |
| 24 | + while IFS= read -r a; do set -- "$@" "$a"; done < $dir/$name.args | |
| 25 | + fi | |
| 26 | + $out/$name "$@" > $out/$name.out 2>/dev/null | |
| 27 | + status=$? | |
| 28 | + want=0 | |
| 29 | + [ -f $dir/$name.status ] && want=$(cat $dir/$name.status) | |
| 30 | + if ! cmp -s $out/$name.out $dir/$name.expect; then | |
| 31 | + echo "FAIL $dir/$name: output differs" | |
| 32 | + diff $out/$name.out $dir/$name.expect | head -5 | |
| 33 | + fail=$((fail + 1)) | |
| 34 | + elif [ $status != $want ]; then | |
| 35 | + echo "FAIL $dir/$name: status $status, not $want" | |
| 36 | + fail=$((fail + 1)) | |
| 37 | + else | |
| 38 | + pass=$((pass + 1)) | |
| 39 | + fi | |
| 40 | +} | |
| 41 | + | |
| 42 | +for f in examples/*.jed test/run/*.jed; do | |
| 43 | + run $(dirname $f) $(basename $f .jed) | |
| 44 | +done | |
| 45 | +for f in test/err/*.jed; do | |
| 46 | + name=$(basename $f .jed) | |
| 47 | + if $jed -I test/err/mods -I test/run/mods -o $out/$name $f 2>$out/$name.cerr; then | |
| 48 | + echo "FAIL $f: compiles" | |
| 49 | + fail=$((fail + 1)) | |
| 50 | + elif ! grep -qF "$(cat test/err/$name.err)" $out/$name.cerr; then | |
| 51 | + echo "FAIL $f: says $(cat $out/$name.cerr)" | |
| 52 | + fail=$((fail + 1)) | |
| 53 | + else | |
| 54 | + pass=$((pass + 1)) | |
| 55 | + fi | |
| 56 | +done | |
| 57 | +echo "$pass passed, $fail failed" | |
| 58 | +[ $fail = 0 ] |
| @@ -0,0 +1 @@ | ||
| 1 | +can't use I32 as I64 |
| @@ -0,0 +1,7 @@ | ||
| 1 | +module Assign; | |
| 2 | +proc main*; | |
| 3 | +var a: I64; b: I32; | |
| 4 | +begin | |
| 5 | + a := b; | |
| 6 | +end; | |
| 7 | +end Assign. |
| @@ -0,0 +1 @@ | ||
| 1 | +break outside a loop |
| @@ -0,0 +1,6 @@ | ||
| 1 | +module Break; | |
| 2 | +proc main*; | |
| 3 | +begin | |
| 4 | + break; | |
| 5 | +end; | |
| 6 | +end Break. |
| @@ -0,0 +1 @@ | ||
| 1 | +Cycle imports itself |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module Cycle; | |
| 2 | +import CycleB; | |
| 3 | +end Cycle. |
| @@ -0,0 +1 @@ | ||
| 1 | +field extra isn't exported |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module Field; | |
| 2 | +import Plat; | |
| 3 | +proc main*; | |
| 4 | +var i: Plat.Info; | |
| 5 | +begin | |
| 6 | + i.extra := 1; | |
| 7 | +end; | |
| 8 | +end Field. |
| @@ -0,0 +1 @@ | ||
| 1 | +no arm for c |
| @@ -0,0 +1,11 @@ | ||
| 1 | +module Match; | |
| 2 | +type S = variant a; b; c; end; | |
| 3 | +proc main*; | |
| 4 | +var s: S; | |
| 5 | +begin | |
| 6 | + match s of | |
| 7 | + | a: s := b; | |
| 8 | + | b: | |
| 9 | + end; | |
| 10 | +end; | |
| 11 | +end Match. |
| @@ -0,0 +1 @@ | ||
| 1 | +more isn't exported by |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module UsesExtra; | |
| 2 | +import Extra; | |
| 3 | +end UsesExtra. |
| @@ -0,0 +1 @@ | ||
| 1 | +f isn't exported as in |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module UsesIface; | |
| 2 | +import Iface; | |
| 3 | +end UsesIface. |
| @@ -0,0 +1 @@ | ||
| 1 | +R isn't exported as in |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module UsesRec; | |
| 2 | +import Rec; | |
| 3 | +end UsesRec. |
| @@ -0,0 +1 @@ | ||
| 1 | +Utf.jed:2:5: text outside strings, characters and comments must be ASCII |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module Utf; | |
| 2 | +var é: I64; | |
| 3 | +end Utf. |
| @@ -0,0 +1 @@ | ||
| 1 | +can't use ^VOLATILE I64 as ^I64 |
| @@ -0,0 +1,8 @@ | ||
| 1 | +module Volatile; | |
| 2 | +proc main*; | |
| 3 | +var x: I64; p: ^VOLATILE I64; q: ^I64; | |
| 4 | +begin | |
| 5 | + p := ADR(x); | |
| 6 | + q := p; | |
| 7 | +end; | |
| 8 | +end Volatile. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module Cycle; | |
| 2 | +import CycleB; | |
| 3 | +end Cycle. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module CycleB; | |
| 2 | +import Cycle; | |
| 3 | +end CycleB. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +module Extra; | |
| 2 | +type R* = record a*: I64; secret: [4]I64; end; | |
| 3 | +const more* = 1; | |
| 4 | +end Extra. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module Extra; | |
| 2 | +type R* = record a*: I64; hidden: U8; end; | |
| 3 | +end Extra. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module Iface; | |
| 2 | +proc f*(x: I32): I64; begin return 0; end; | |
| 3 | +end Iface. |
| @@ -0,0 +1,3 @@ | ||
| 1 | +module Iface; | |
| 2 | +proc f*(x: I64): I64; begin return x; end; | |
| 3 | +end Iface. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +module Rec; | |
| 2 | +type R* = record a*: I32; end; | |
| 3 | +proc f*(r: ^R); begin end; | |
| 4 | +end Rec. |
| @@ -0,0 +1,4 @@ | ||
| 1 | +module Rec; | |
| 2 | +type R* = record a*: I64; b: I64; end; | |
| 3 | +proc f*(r: ^R); begin end; | |
| 4 | +end Rec. |
| @@ -0,0 +1,61 @@ | ||
| 1 | +"""Writes a large jed program to standard output, for timing the | |
| 2 | +compiler: n procedures of loops, conditions, records, slices and calls, | |
| 3 | +each calling the one before, so that none is left out.""" | |
| 4 | +import sys | |
| 5 | + | |
| 6 | +n = int(sys.argv[1]) | |
| 7 | +print("""module Bench; | |
| 8 | +import Out; | |
| 9 | + | |
| 10 | +type | |
| 11 | + Pair = record x, y: I64; end; | |
| 12 | + Shape = variant circle(r: I64); rect(w, h: I64); dot; end; | |
| 13 | + | |
| 14 | +var | |
| 15 | + g: [16]I64; | |
| 16 | + data: [64]I64; | |
| 17 | +""") | |
| 18 | +print("""proc p0(a, b: I64; s: []I64): I64; | |
| 19 | +begin | |
| 20 | + return a + b; | |
| 21 | +end; | |
| 22 | +""") | |
| 23 | +for i in range(1, n + 1): | |
| 24 | + print(f"""(* procedure {i} *) | |
| 25 | +proc p{i}(a, b: I64; s: []I64): I64; | |
| 26 | +var i, t: I64; r: Pair; sh: Shape; | |
| 27 | +begin | |
| 28 | + t := 0; | |
| 29 | + for i := 0 to LEN(s) - 1 do | |
| 30 | + if s[i] > a then | |
| 31 | + t := t + s[i] * b; | |
| 32 | + elsif s[i] = b then | |
| 33 | + t := t - 1; | |
| 34 | + else | |
| 35 | + t := t xor i; | |
| 36 | + end; | |
| 37 | + end; | |
| 38 | + r.x := t; | |
| 39 | + r.y := a + b * {i}; | |
| 40 | + while r.x > 100 do | |
| 41 | + r.x := r.x div 2; | |
| 42 | + end; | |
| 43 | + sh := rect(r.x, r.y); | |
| 44 | + match sh of | |
| 45 | + | circle(c): t := t + c; | |
| 46 | + | rect(w, h): t := t + w * h; | |
| 47 | + | dot: t := t - 1; | |
| 48 | + end; | |
| 49 | + g[a and 15] := r.x + r.y; | |
| 50 | + return p{i - 1}(t and 1023, b, s[0..LEN(s) div 2]) + r.x; | |
| 51 | +end; | |
| 52 | +""") | |
| 53 | +print(f"""proc main*; | |
| 54 | +var i: I64; | |
| 55 | +begin | |
| 56 | + for i := 0 to 63 do data[i] := i; end; | |
| 57 | + Out.int(p{n}(1, 2, data[0..4])); | |
| 58 | + Out.ln; | |
| 59 | +end; | |
| 60 | + | |
| 61 | +end Bench.""") |
| @@ -0,0 +1,14 @@ | ||
| 1 | +"""Counts the lines of the compiler's units: all of them, and the lines | |
| 2 | +of code, those that are neither blank nor only comment.""" | |
| 3 | +import re, sys | |
| 4 | + | |
| 5 | +total = code = 0 | |
| 6 | +for path in sys.argv[1:]: | |
| 7 | + text = open(path).read() | |
| 8 | + bare = re.sub(r"\{[^}]*\}|\(\*.*?\*\)", "", text, flags=re.S) | |
| 9 | + n = text.count("\n") | |
| 10 | + c = sum(1 for line in bare.split("\n") if line.strip()) | |
| 11 | + print(f"{path:18} {n:6} {c:6}") | |
| 12 | + total += n | |
| 13 | + code += c | |
| 14 | +print(f"{'total':18} {total:6} {code:6}") |
Binary file; the patch has it.
| @@ -0,0 +1,3 @@ | ||
| 1 | +one | |
| 2 | +two words | |
| 3 | +é |
| @@ -0,0 +1,3 @@ | ||
| 1 | +one | |
| 2 | +two words | |
| 3 | +é |
| @@ -0,0 +1,18 @@ | ||
| 1 | +module Args; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +proc main*: I64; | |
| 5 | +var i, n: I64; a: ^^U8; s: ^U8; | |
| 6 | +begin | |
| 7 | + a := ARGV(); | |
| 8 | + for i := 1 to ARGC() - 1 do | |
| 9 | + s := a[i]; | |
| 10 | + n := 0; | |
| 11 | + while s[n] # 0 do n := n + 1; end; | |
| 12 | + Out.str(s[0..n]); | |
| 13 | + Out.ln; | |
| 14 | + end; | |
| 15 | + return ARGC(); | |
| 16 | +end; | |
| 17 | + | |
| 18 | +end Args. |
| @@ -0,0 +1 @@ | ||
| 1 | +4 |
Binary file; the patch has it.
| @@ -0,0 +1,35 @@ | ||
| 1 | +M 32 | |
| 2 | +add 22 | |
| 3 | +sub 12 | |
| 4 | +mul 85 | |
| 5 | +div -3 | |
| 6 | +mod -1 | |
| 7 | +divv -3 | |
| 8 | +modv -2 | |
| 9 | +prec 14 | |
| 10 | +neg -85 | |
| 11 | +u8wrap 44 | |
| 12 | +i8wrap -128 | |
| 13 | +u32wrap 1 | |
| 14 | +u64>i 1 | |
| 15 | +i64< 1 | |
| 16 | +shl 1024 | |
| 17 | +shr -4 | |
| 18 | +ushr 15 | |
| 19 | +and 8 | |
| 20 | +or 15 | |
| 21 | +xor 6 | |
| 22 | +not -1 | |
| 23 | +real 85 | |
| 24 | +trunc -2 | |
| 25 | +nan 0 | |
| 26 | +rlt 1 | |
| 27 | +u64real 18 | |
| 28 | +bool 1 | |
| 29 | +shortcut 1 | |
| 30 | +global 42 | |
| 31 | +char 65 | |
| 32 | +utf8 233 | |
| 33 | +hexconst 255 | |
| 34 | +cut 44 | |
| 35 | +sext -1 |
| @@ -0,0 +1,74 @@ | ||
| 1 | +module Arith; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +const | |
| 5 | + K = 10; | |
| 6 | + M = K * 3 + 2; | |
| 7 | + Big = 0FFFFFFFFFFFFFFFFH; | |
| 8 | + | |
| 9 | +var g: I64; | |
| 10 | + | |
| 11 | +proc show(s: []U8; n: I64); | |
| 12 | +begin | |
| 13 | + Out.str(s); Out.str(" "); Out.int(n); Out.ln; | |
| 14 | +end; | |
| 15 | + | |
| 16 | +proc showb(s: []U8; t: BOOL); | |
| 17 | +begin | |
| 18 | + if t then show(s, 1); else show(s, 0); end; | |
| 19 | +end; | |
| 20 | + | |
| 21 | +proc main*; | |
| 22 | +var a, b: I64; u: U8; i8: I8; w: U32; x: U64; r: F64; t: BOOL; | |
| 23 | +begin | |
| 24 | + show("M", M); | |
| 25 | + a := 17; b := 5; | |
| 26 | + show("add", a + b); | |
| 27 | + show("sub", a - b); | |
| 28 | + show("mul", a * b); | |
| 29 | + show("div", -7 div 2); | |
| 30 | + show("mod", -7 mod 2); | |
| 31 | + show("divv", (0 - a) div b); | |
| 32 | + show("modv", (0 - a) mod b); | |
| 33 | + show("prec", 2 + 3 * 4); | |
| 34 | + show("neg", -a * b); | |
| 35 | + u := 200; | |
| 36 | + u := u + 100; | |
| 37 | + show("u8wrap", I64(u)); | |
| 38 | + i8 := 127; | |
| 39 | + i8 := i8 + 1; | |
| 40 | + show("i8wrap", I64(i8)); | |
| 41 | + w := 0FFFFFFFFH; | |
| 42 | + w := w + 2; | |
| 43 | + show("u32wrap", I64(w)); | |
| 44 | + x := Big; | |
| 45 | + showb("u64>i", x > 5); | |
| 46 | + showb("i64<", -1 < 5); | |
| 47 | + show("shl", 1 shl 10); | |
| 48 | + show("shr", -16 shr 2); | |
| 49 | + show("ushr", I64(U64(-16) shr 60)); | |
| 50 | + show("and", 12 and 10); | |
| 51 | + show("or", 12 or 3); | |
| 52 | + show("xor", 12 xor 10); | |
| 53 | + show("not", not 0); | |
| 54 | + r := F64(a) / 2.0; | |
| 55 | + show("real", I64(r * 10.0)); | |
| 56 | + r := -2.75; | |
| 57 | + show("trunc", I64(r)); | |
| 58 | + showb("nan", 0.0 / 0.0 = 0.0 / 0.0); | |
| 59 | + showb("rlt", 1.5 < 2.5); | |
| 60 | + show("u64real", I64(F64(x) / 1.0E18)); | |
| 61 | + t := (a > 3) and (b < 3); | |
| 62 | + if not t then show("bool", 1); end; | |
| 63 | + t := (a > 3) or (g div 0 = 1); | |
| 64 | + if t then show("shortcut", 1); end; | |
| 65 | + g := 42; | |
| 66 | + show("global", g); | |
| 67 | + show("char", 'A'); | |
| 68 | + show("utf8", 'é'); | |
| 69 | + show("hexconst", 0FFH); | |
| 70 | + show("cut", I64(U8(300))); | |
| 71 | + show("sext", I64(I8(U8(255)))); | |
| 72 | +end; | |
| 73 | + | |
| 74 | +end Arith. |
Binary file; the patch has it.
| @@ -0,0 +1,6 @@ | ||
| 1 | +5 7 | |
| 2 | +cas1 cas2 100 | |
| 3 | +0 9 | |
| 4 | +321 | |
| 5 | +6 | |
| 6 | +100 |
| @@ -0,0 +1,35 @@ | ||
| 1 | +module Atomic; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +var counter: I64; flag: U64; p: ^I64; | |
| 5 | + | |
| 6 | +proc add3*(a, b, c: I64): I64; | |
| 7 | +code 48H, 8DH, 04H, 37H, (* lea rax, [rdi+rsi] *) | |
| 8 | + 48H, 01H, 0D0H, (* add rax, rdx *) | |
| 9 | + 0C3H; (* ret *) | |
| 10 | + | |
| 11 | +proc main*; | |
| 12 | +var old: I64; ok: BOOL; v: ^VOLATILE I64; x: I64; | |
| 13 | +begin | |
| 14 | + old := XADD(ADR(counter), 5); | |
| 15 | + old := XADD(ADR(counter), 2); | |
| 16 | + Out.int(old); Out.str(" "); Out.int(counter); Out.ln; | |
| 17 | + ok := CAS(ADR(counter), 7, 100); | |
| 18 | + if ok then Out.str("cas1 "); end; | |
| 19 | + ok := CAS(ADR(counter), 7, 200); | |
| 20 | + if not ok then Out.str("cas2 "); end; | |
| 21 | + Out.int(counter); Out.ln; | |
| 22 | + Out.int(I64(XCHG(ADR(flag), 9))); Out.str(" "); | |
| 23 | + Out.int(I64(XCHG(ADR(flag), 3))); Out.ln; | |
| 24 | + FENCE; | |
| 25 | + FENCE(); | |
| 26 | + Out.int(add3(1, 20, 300)); Out.ln; | |
| 27 | + v := ADR(x); | |
| 28 | + v^ := 5; | |
| 29 | + v^ := v^ + 1; | |
| 30 | + Out.int(x); Out.ln; | |
| 31 | + v := ^VOLATILE I64(ADR(counter)); | |
| 32 | + Out.int(v^); Out.ln; | |
| 33 | +end; | |
| 34 | + | |
| 35 | +end Atomic. |
Binary file; the patch has it.
| @@ -0,0 +1 @@ | ||
| 1 | +First Second Bodies main 12 |
| @@ -0,0 +1,11 @@ | ||
| 1 | +module Bodies; | |
| 2 | +import Out, A := First, Second; | |
| 3 | + | |
| 4 | +proc main*; | |
| 5 | +begin | |
| 6 | + Out.str("main "); Out.int(A.count); Out.int(Second.count); Out.ln; | |
| 7 | +end; | |
| 8 | + | |
| 9 | +begin | |
| 10 | + Out.str("Bodies "); | |
| 11 | +end Bodies. |
Binary file; the patch has it.
| @@ -0,0 +1 @@ | ||
| 1 | +0123 | |
| \ No newline at end of file |
| @@ -0,0 +1,13 @@ | ||
| 1 | +module Bounds; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +proc main*; | |
| 5 | +var a: [4]I64; i: I64; | |
| 6 | +begin | |
| 7 | + for i := 0 to 4 do | |
| 8 | + a[i] := i; | |
| 9 | + Out.int(a[i]); | |
| 10 | + end; | |
| 11 | +end; | |
| 12 | + | |
| 13 | +end Bounds. |
| @@ -0,0 +1 @@ | ||
| 1 | +2 |
Binary file; the patch has it.
| @@ -0,0 +1,8 @@ | ||
| 1 | +12345 | |
| 2 | +10 7 4 1 | |
| 3 | +0 4 8 | |
| 4 | +6 | |
| 5 | +3 | |
| 6 | +0 1 10 11 20 21 | |
| 7 | +4 | |
| 8 | +one two three many |
| @@ -0,0 +1,42 @@ | ||
| 1 | +module Control; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +proc main*; | |
| 5 | +var i, j, n: I64; u: U8; k: I8; | |
| 6 | +begin | |
| 7 | + for i := 1 to 5 do Out.int(i); end; Out.ln; | |
| 8 | + for i := 10 to 1 by -3 do Out.int(i); Out.str(" "); end; Out.ln; | |
| 9 | + for i := 0 to 10 by 4 do Out.int(i); Out.str(" "); end; Out.ln; | |
| 10 | + for i := 5 to 1 do Out.str("never"); end; | |
| 11 | + n := 0; | |
| 12 | + for u := 250 to 255 do n := n + 1; end; | |
| 13 | + Out.int(n); Out.ln; | |
| 14 | + n := 0; | |
| 15 | + for k := 120 to 127 by 3 do n := n + 1; end; | |
| 16 | + Out.int(n); Out.ln; | |
| 17 | + i := 0; | |
| 18 | + while i < 3 do | |
| 19 | + j := 0; | |
| 20 | + while true do | |
| 21 | + if j = 2 then break; end; | |
| 22 | + Out.int(i * 10 + j); Out.str(" "); | |
| 23 | + j := j + 1; | |
| 24 | + end; | |
| 25 | + i := i + 1; | |
| 26 | + end; | |
| 27 | + Out.ln; | |
| 28 | + i := 0; | |
| 29 | + repeat i := i + 1; if i = 4 then break; end; until i = 10; | |
| 30 | + Out.int(i); Out.ln; | |
| 31 | + for i := 1 to 4 do | |
| 32 | + if i = 1 then Out.str("one"); | |
| 33 | + elsif i = 2 then Out.str("two"); | |
| 34 | + elsif i = 3 then Out.str("three"); | |
| 35 | + else Out.str("many"); | |
| 36 | + end; | |
| 37 | + Out.str(" "); | |
| 38 | + end; | |
| 39 | + Out.ln; | |
| 40 | +end; | |
| 41 | + | |
| 42 | +end Control. |
Binary file; the patch has it.
| @@ -0,0 +1,12 @@ | ||
| 1 | +2 24 11 48 16 | |
| 2 | +120 7 | |
| 3 | +30 | |
| 4 | +23 10 | |
| 5 | +500 5 300 | |
| 6 | +4 200 500 | |
| 7 | +2 300 | |
| 8 | +200 | |
| 9 | +8 | |
| 10 | +3 4 | |
| 11 | +-2 65535 | |
| 12 | +16 |
| @@ -0,0 +1,72 @@ | ||
| 1 | +module Data; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +type | |
| 5 | + Cell = record ch: U8; attr: U8; end; | |
| 6 | + Mixed = record a: U8; b: I64; c: U16; end; | |
| 7 | + Packed = packed record a: U8; b: I64; c: U16; end; | |
| 8 | + Node = record next: ^Node; val: I64; end; | |
| 9 | + Grid = [3][4]I32; | |
| 10 | + Pair = record x, y: I64; end; | |
| 11 | + | |
| 12 | +var | |
| 13 | + nodes: [5]Node; | |
| 14 | + grid: Grid; | |
| 15 | + gp: Pair; | |
| 16 | + | |
| 17 | +proc sum(p: ^Node): I64; | |
| 18 | +var s: I64; | |
| 19 | +begin | |
| 20 | + s := 0; | |
| 21 | + while p # nil do | |
| 22 | + s := s + p^.val; | |
| 23 | + p := p^.next; | |
| 24 | + end; | |
| 25 | + return s; | |
| 26 | +end; | |
| 27 | + | |
| 28 | +proc main*; | |
| 29 | +var | |
| 30 | + c: Cell; cells: [4]Cell; m: Mixed; i, j: I64; p, q: ^I64; arr: [8]I64; | |
| 31 | + s: []I64; pp: Pair; bp: ^U8; | |
| 32 | +begin | |
| 33 | + Out.int(SIZE(Cell)); Out.str(" "); | |
| 34 | + Out.int(SIZE(Mixed)); Out.str(" "); | |
| 35 | + Out.int(SIZE(Packed)); Out.str(" "); | |
| 36 | + Out.int(SIZE(Grid)); Out.str(" "); | |
| 37 | + Out.int(SIZE([]U8)); Out.ln; | |
| 38 | + c.ch := 'x'; c.attr := 7; | |
| 39 | + cells[2] := c; | |
| 40 | + Out.int(I64(cells[2].ch)); Out.str(" "); Out.int(I64(cells[2].attr)); Out.ln; | |
| 41 | + for i := 0 to 4 do | |
| 42 | + nodes[i].val := i * i; | |
| 43 | + if i < 4 then nodes[i].next := ADR(nodes[i + 1]); end; | |
| 44 | + end; | |
| 45 | + Out.int(sum(ADR(nodes[0]))); Out.ln; | |
| 46 | + for i := 0 to 2 do | |
| 47 | + for j := 0 to 3 do grid[i][j] := I32(i * 10 + j); end; | |
| 48 | + end; | |
| 49 | + Out.int(I64(grid[2][3])); Out.str(" "); Out.int(I64(grid[1][0])); Out.ln; | |
| 50 | + for i := 0 to 7 do arr[i] := i * 100; end; | |
| 51 | + p := ADR(arr[0]); | |
| 52 | + q := p + 5; | |
| 53 | + Out.int(q^); Out.str(" "); Out.int(q - p); Out.str(" "); Out.int(p[3]); Out.ln; | |
| 54 | + s := arr[2..6]; | |
| 55 | + Out.int(LEN(s)); Out.str(" "); Out.int(s[0]); Out.str(" "); Out.int(s[3]); Out.ln; | |
| 56 | + s := s[1..3]; | |
| 57 | + Out.int(LEN(s)); Out.str(" "); Out.int(s[0]); Out.ln; | |
| 58 | + s := p[1..3]; | |
| 59 | + Out.int(s[1]); Out.ln; | |
| 60 | + s := arr; | |
| 61 | + Out.int(LEN(s)); Out.ln; | |
| 62 | + pp.x := 3; pp.y := 4; | |
| 63 | + gp := pp; | |
| 64 | + pp.x := 99; | |
| 65 | + Out.int(gp.x); Out.str(" "); Out.int(gp.y); Out.ln; | |
| 66 | + m.a := 1; m.b := -2; m.c := 65535; | |
| 67 | + Out.int(m.b); Out.str(" "); Out.int(I64(m.c)); Out.ln; | |
| 68 | + bp := ^U8(ADR(m)); | |
| 69 | + Out.int(I64(ADR(m.c)) - I64(bp)); Out.ln; | |
| 70 | +end; | |
| 71 | + | |
| 72 | +end Data. |
Binary file; the patch has it.
| @@ -0,0 +1,7 @@ | ||
| 1 | +22 | |
| 2 | +20 2 | |
| 3 | +123 item | |
| 4 | +100 -1 | |
| 5 | +1338 932 | |
| 6 | +44 5 | |
| 7 | +1112 |
| @@ -0,0 +1,64 @@ | ||
| 1 | +module Edge; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +type | |
| 5 | + Inner = record vals: [4]I64; name: []U8; end; | |
| 6 | + Outer = record items: [3]Inner; n: I64; end; | |
| 7 | + | |
| 8 | +var g: Outer; calls: I64; | |
| 9 | + | |
| 10 | +(* rsp mod 16 at the call, as the callee sees it after its return | |
| 11 | + address: always 8 when the caller keeps the stack aligned *) | |
| 12 | +proc rspmod: I64; | |
| 13 | +code 48H, 89H, 0E0H, (* mov rax, rsp *) | |
| 14 | + 48H, 83H, 0E0H, 0FH, (* and rax, 15 *) | |
| 15 | + 0C3H; | |
| 16 | + | |
| 17 | +proc next: I64; | |
| 18 | +begin | |
| 19 | + calls := calls + 1; | |
| 20 | + return calls; | |
| 21 | +end; | |
| 22 | + | |
| 23 | +proc setall(var o: Outer; v: I64); | |
| 24 | +var i, j: I64; | |
| 25 | +begin | |
| 26 | + for i := 0 to 2 do | |
| 27 | + for j := 0 to 3 do o.items[i].vals[j] := v + i * 10 + j; end; | |
| 28 | + o.items[i].name := "item"; | |
| 29 | + end; | |
| 30 | + o.n := 3; | |
| 31 | +end; | |
| 32 | + | |
| 33 | +proc sumInner(xs: []Inner): I64; | |
| 34 | +var i, j, s: I64; | |
| 35 | +begin | |
| 36 | + s := 0; | |
| 37 | + for i := 0 to LEN(xs) - 1 do | |
| 38 | + for j := 0 to 3 do s := s + xs[i].vals[j]; end; | |
| 39 | + end; | |
| 40 | + return s; | |
| 41 | +end; | |
| 42 | + | |
| 43 | +proc main*; | |
| 44 | +var a: [5]I64; i, r: I64; o: Outer; pa: ^[5]I64; | |
| 45 | +begin | |
| 46 | + r := rspmod() + (1 + (2 + (3 + rspmod()))); | |
| 47 | + Out.int(r); Out.ln; | |
| 48 | + a[next()] := next() * 10; | |
| 49 | + Out.int(a[1]); Out.str(" "); Out.int(calls); Out.ln; | |
| 50 | + setall(g, 100); | |
| 51 | + Out.int(g.items[2].vals[3]); Out.str(" "); Out.str(g.items[1].name); Out.ln; | |
| 52 | + o := g; | |
| 53 | + o.items[0].vals[0] := -1; | |
| 54 | + Out.int(g.items[0].vals[0]); Out.str(" "); Out.int(o.items[0].vals[0]); Out.ln; | |
| 55 | + Out.int(sumInner(g.items)); Out.str(" "); Out.int(sumInner(g.items[1..3])); Out.ln; | |
| 56 | + pa := ADR(a); | |
| 57 | + pa^[4] := 44; | |
| 58 | + Out.int(a[4]); Out.str(" "); Out.int(LEN(pa^)); Out.ln; | |
| 59 | + i := 2; | |
| 60 | + g.items[i].vals[i + 1] := g.items[i - 1].vals[i] + 1000; | |
| 61 | + Out.int(g.items[2].vals[3]); Out.ln; | |
| 62 | +end; | |
| 63 | + | |
| 64 | +end Edge. |
Binary file; the patch has it.
| @@ -0,0 +1,13 @@ | ||
| 1 | +parity ok | |
| 2 | +21 | |
| 3 | +10 11 21 | |
| 4 | +22 | |
| 5 | +7 12 | |
| 6 | +same | |
| 7 | +nil | |
| 8 | +3245 | |
| 9 | +2432902008176640000 | |
| 10 | +95 5 | |
| 11 | +0 | |
| 12 | +16 0 | |
| 13 | +15 |
| @@ -0,0 +1,117 @@ | ||
| 1 | +module Procs; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +type | |
| 5 | + Pair = record x, y: I64; end; | |
| 6 | + Op = proc(a, b: I64): I64; | |
| 7 | + Big = record a: [10]I64; end; | |
| 8 | + | |
| 9 | +proc even(n: I64): BOOL; forward; | |
| 10 | + | |
| 11 | +proc odd(n: I64): BOOL; | |
| 12 | +begin | |
| 13 | + if n = 0 then return false; end; | |
| 14 | + return even(n - 1); | |
| 15 | +end; | |
| 16 | + | |
| 17 | +proc even(n: I64): BOOL; | |
| 18 | +begin | |
| 19 | + if n = 0 then return true; end; | |
| 20 | + return odd(n - 1); | |
| 21 | +end; | |
| 22 | + | |
| 23 | +proc swap(var a, b: I64); | |
| 24 | +var t: I64; | |
| 25 | +begin | |
| 26 | + t := a; a := b; b := t; | |
| 27 | +end; | |
| 28 | + | |
| 29 | +proc bump(p: Pair): Pair; | |
| 30 | +begin | |
| 31 | + p.x := p.x + 1; | |
| 32 | + p.y := p.y + 1; | |
| 33 | + return p; | |
| 34 | +end; | |
| 35 | + | |
| 36 | +proc add(a, b: I64): I64; begin return a + b; end; | |
| 37 | +proc mul(a, b: I64): I64; begin return a * b; end; | |
| 38 | + | |
| 39 | +proc apply(f: Op; a, b: I64): I64; | |
| 40 | +begin | |
| 41 | + return f(a, b); | |
| 42 | +end; | |
| 43 | + | |
| 44 | +proc many(a, b, c, d, e, f, g, h: I64; x: F64; i: I64; y: F64): I64; | |
| 45 | +begin | |
| 46 | + return a + 2 * b + 3 * c + 4 * d + 5 * e + 6 * f + 7 * g + 8 * h + I64(x) * 100 + 9 * i + I64(y) * 1000; | |
| 47 | +end; | |
| 48 | + | |
| 49 | +proc fact(n: I64): I64; | |
| 50 | +begin | |
| 51 | + if n <= 1 then return 1; end; | |
| 52 | + return n * fact(n - 1); | |
| 53 | +end; | |
| 54 | + | |
| 55 | +proc fill(var b: Big; v: I64); | |
| 56 | +var i: I64; | |
| 57 | +begin | |
| 58 | + for i := 0 to 9 do b.a[i] := v + i; end; | |
| 59 | +end; | |
| 60 | + | |
| 61 | +proc total(b: Big): I64; | |
| 62 | +var i, s: I64; | |
| 63 | +begin | |
| 64 | + s := 0; | |
| 65 | + for i := 0 to 9 do s := s + b.a[i]; end; | |
| 66 | + b.a[0] := 1000; | |
| 67 | + return s; | |
| 68 | +end; | |
| 69 | + | |
| 70 | +proc noresult(n: I64): I64; | |
| 71 | +begin | |
| 72 | + if n > 100 then return 1; end; | |
| 73 | +end; | |
| 74 | + | |
| 75 | +proc mk(n: I64): Big; | |
| 76 | +var b: Big; | |
| 77 | +begin | |
| 78 | + fill(b, n); | |
| 79 | + return b; | |
| 80 | +end; | |
| 81 | + | |
| 82 | +proc mkzero(n: I64): Big; | |
| 83 | +begin | |
| 84 | +end; | |
| 85 | + | |
| 86 | +proc fsum(a, b: F64): F64; | |
| 87 | +begin | |
| 88 | + return a + b; | |
| 89 | +end; | |
| 90 | + | |
| 91 | +proc main*: I64; | |
| 92 | +var a, b: I64; p, q: Pair; f: Op; bg: Big; | |
| 93 | +begin | |
| 94 | + if even(10) and odd(7) and not even(3) then Out.str("parity ok"); Out.ln; end; | |
| 95 | + a := 1; b := 2; | |
| 96 | + swap(a, b); | |
| 97 | + Out.int(a); Out.int(b); Out.ln; | |
| 98 | + p.x := 10; p.y := 20; | |
| 99 | + q := bump(p); | |
| 100 | + Out.int(p.x); Out.str(" "); Out.int(q.x); Out.str(" "); Out.int(q.y); Out.ln; | |
| 101 | + Out.int(bump(bump(p)).y); Out.ln; | |
| 102 | + f := add; | |
| 103 | + Out.int(apply(f, 3, 4)); Out.str(" "); Out.int(apply(mul, 3, 4)); Out.ln; | |
| 104 | + if f = add then Out.str("same"); Out.ln; end; | |
| 105 | + f := nil; | |
| 106 | + if f = nil then Out.str("nil"); Out.ln; end; | |
| 107 | + Out.int(many(1, 1, 1, 1, 1, 1, 1, 1, 2.5, 1, 3.9)); Out.ln; | |
| 108 | + Out.int(fact(20)); Out.ln; | |
| 109 | + fill(bg, 5); | |
| 110 | + Out.int(total(bg)); Out.str(" "); Out.int(bg.a[0]); Out.ln; | |
| 111 | + Out.int(noresult(5)); Out.ln; | |
| 112 | + Out.int(mk(7).a[9]); Out.str(" "); Out.int(mkzero(1).a[3]); Out.ln; | |
| 113 | + Out.int(I64(fsum(1.25, 2.5) * 4.0)); Out.ln; | |
| 114 | + return 7; | |
| 115 | +end; | |
| 116 | + | |
| 117 | +end Procs. |
| @@ -0,0 +1 @@ | ||
| 1 | +7 |
Binary file; the patch has it.
| @@ -0,0 +1,7 @@ | ||
| 1 | +12 | |
| 2 | +12 | |
| 3 | +24 | |
| 4 | +1 24 | |
| 5 | +3 hi12 1 3 | |
| 6 | +zero is a circle of 0 | |
| 7 | +32 |
| @@ -0,0 +1,63 @@ | ||
| 1 | +module Shapes; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +type | |
| 5 | + Shape* = variant | |
| 6 | + circle(r: F64); | |
| 7 | + rect(w, h: F64); | |
| 8 | + label(text: []U8; size: I64); | |
| 9 | + dot; | |
| 10 | + end; | |
| 11 | + | |
| 12 | +proc area(s: Shape): I64; | |
| 13 | +var a: F64; | |
| 14 | +begin | |
| 15 | + match s of | |
| 16 | + | circle(r): a := 3.0 * r * r; | |
| 17 | + | rect(w, h): a := w * h; | |
| 18 | + | label: a := 0.0; | |
| 19 | + | dot: a := 1.0; | |
| 20 | + end; | |
| 21 | + return I64(a); | |
| 22 | +end; | |
| 23 | + | |
| 24 | +proc grow(var s: Shape); | |
| 25 | +begin | |
| 26 | + match s of | |
| 27 | + | rect(w, h): w := w * 2.0; | |
| 28 | + | else: | |
| 29 | + end; | |
| 30 | +end; | |
| 31 | + | |
| 32 | +proc main*; | |
| 33 | +var s, t: Shape; shapes: [4]Shape; i: I64; z: Shape; | |
| 34 | +begin | |
| 35 | + s := circle(2.0); | |
| 36 | + Out.int(area(s)); Out.ln; | |
| 37 | + s := rect(3.0, 4.0); | |
| 38 | + Out.int(area(s)); Out.ln; | |
| 39 | + grow(s); | |
| 40 | + Out.int(area(s)); Out.ln; | |
| 41 | + t := s; | |
| 42 | + s := dot; | |
| 43 | + Out.int(area(s)); Out.str(" "); Out.int(area(t)); Out.ln; | |
| 44 | + shapes[0] := circle(1.0); | |
| 45 | + shapes[1] := label("hi", 12); | |
| 46 | + shapes[2] := dot; | |
| 47 | + shapes[3] := rect(1.5, 2.0); | |
| 48 | + for i := 0 to 3 do | |
| 49 | + match shapes[i] of | |
| 50 | + | label(text, size): Out.str(text); Out.int(size); | |
| 51 | + | else: Out.int(area(shapes[i])); | |
| 52 | + end; | |
| 53 | + Out.str(" "); | |
| 54 | + end; | |
| 55 | + Out.ln; | |
| 56 | + match z of | |
| 57 | + | circle(r): Out.str("zero is a circle of "); Out.int(I64(r)); Out.ln; | |
| 58 | + | else: Out.str("?"); | |
| 59 | + end; | |
| 60 | + Out.int(SIZE(Shape)); Out.ln; | |
| 61 | +end; | |
| 62 | + | |
| 63 | +end Shapes. |
Binary file; the patch has it.
| @@ -0,0 +1,4 @@ | ||
| 1 | +eq ne lit arr buf 0 5 2 | |
| 2 | +6 100 0 | |
| 3 | +threeone | |
| 4 | +é☺A |
| @@ -0,0 +1,39 @@ | ||
| 1 | +module Strings; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +const greeting = "hello"; | |
| 5 | + | |
| 6 | +var buf: [8]U8; | |
| 7 | + | |
| 8 | +proc count(xs: []I64): I64; | |
| 9 | +var i, s: I64; | |
| 10 | +begin | |
| 11 | + s := 0; | |
| 12 | + for i := 0 to LEN(xs) - 1 do s := s + xs[i]; end; | |
| 13 | + return s; | |
| 14 | +end; | |
| 15 | + | |
| 16 | +proc main*; | |
| 17 | +var a: []U8; b: [5]U8; c: [3]I64; d: [][]U8; | |
| 18 | +begin | |
| 19 | + a := "hello"; | |
| 20 | + if a = greeting then Out.str("eq "); end; | |
| 21 | + if a # "help" then Out.str("ne "); end; | |
| 22 | + if "abc" = "abc" then Out.str("lit "); end; | |
| 23 | + b := "hello"; | |
| 24 | + if b = a then Out.str("arr "); end; | |
| 25 | + buf := "hi"; | |
| 26 | + if buf[0..2] = "hi" then Out.str("buf "); end; | |
| 27 | + Out.int(I64(buf[5])); Out.str(" "); | |
| 28 | + Out.int(LEN(greeting)); Out.str(" "); | |
| 29 | + Out.int(LEN(a[1..3])); Out.ln; | |
| 30 | + c := [1, 2, 3]; | |
| 31 | + Out.int(count(c)); Out.str(" "); | |
| 32 | + Out.int(count([10, 20, 30, 40])); Out.str(" "); | |
| 33 | + Out.int(count([])); Out.ln; | |
| 34 | + d := ["one", "two", "three"]; | |
| 35 | + Out.str(d[2]); Out.str(d[0]); Out.ln; | |
| 36 | + Out.str("é\u{263A}\x41\n"); | |
| 37 | +end; | |
| 38 | + | |
| 39 | +end Strings. |
Binary file; the patch has it.
| @@ -0,0 +1 @@ | ||
| 1 | +linux42 |
| @@ -0,0 +1,11 @@ | ||
| 1 | +module Target; | |
| 2 | +import Out, Plat; | |
| 3 | + | |
| 4 | +proc main*; | |
| 5 | +var i: Plat.Info; | |
| 6 | +begin | |
| 7 | + i.kind := 41; | |
| 8 | + Out.str(Plat.name); Out.int(Plat.id(i.kind)); Out.ln; | |
| 9 | +end; | |
| 10 | + | |
| 11 | +end Target. |
| @@ -0,0 +1,14 @@ | ||
| 1 | +module First; | |
| 2 | +import Out; | |
| 3 | + | |
| 4 | +var count*: I64; | |
| 5 | + | |
| 6 | +proc main*; | |
| 7 | +begin | |
| 8 | + Out.str("First.main must not run"); | |
| 9 | +end; | |
| 10 | + | |
| 11 | +begin | |
| 12 | + count := 1; | |
| 13 | + Out.str("First "); | |
| 14 | +end First. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +module Plat; | |
| 2 | +const name* = "linux"; | |
| 3 | +type Info* = record kind*: I64; extra: I64; end; | |
| 4 | +proc id*(x: I64): I64; begin return x + 1; end; | |
| 5 | +end Plat. |
| @@ -0,0 +1,5 @@ | ||
| 1 | +module Plat; | |
| 2 | +const name* = "macos"; | |
| 3 | +type Info* = record kind*: I64; other: U8; end; | |
| 4 | +proc id*(x: I64): I64; begin return SOMETHING_UNKNOWN_HERE(x); end; | |
| 5 | +end Plat. |
| @@ -0,0 +1,11 @@ | ||
| 1 | +module Second; | |
| 2 | +import Out, First; | |
| 3 | + | |
| 4 | +var count*: I64; | |
| 5 | + | |
| 6 | +begin | |
| 7 | + count := First.count + 1; | |
| 8 | + Out.str("Second "); | |
| 9 | + if count = 2 then return; end; | |
| 10 | + Out.str("not reached"); | |
| 11 | +end Second. |