From a3e07807e87821a3ed22e532f1f7545f1fa15b80 Mon Sep 17 00:00:00 2001 From: Greg Gauthier Date: Sun, 6 Apr 2025 21:30:48 +0100 Subject: [PATCH] initial commit --- .gitignore | 3 +++ src/adder.a68 | 4 ++++ src/aplusb.a68 | 1 + src/aplusb2.a68 | 5 +++++ src/ass1.a68 | 5 +++++ src/ass2.a68 | 5 +++++ src/ass3.a68 | 6 ++++++ src/ass4.a68 | 5 +++++ src/ass5.a68 | 6 ++++++ src/ass6.a68 | 6 ++++++ src/assignments.a68 | 18 ++++++++++++++++++ src/big_fact.a68 | 20 ++++++++++++++++++++ src/coerce-test.a68 | 5 +++++ src/copyback.a68 | 11 +++++++++++ src/customplusab.a68 | 19 +++++++++++++++++++ src/fact.a68 | 7 +++++++ src/fact_input.a68 | 20 ++++++++++++++++++++ src/hello.a68 | 5 +++++ src/input.txt | 1 + src/more_slicing.a68 | 6 ++++++ src/ops.a68 | 2 ++ src/output.txt | 1 + src/prec_chk.a68 | 5 +++++ src/realplusab.a68 | 17 +++++++++++++++++ src/rows.a68 | 16 ++++++++++++++++ src/sizing.a68 | 16 ++++++++++++++++ src/slices.a68 | 20 ++++++++++++++++++++ src/test1.a68 | 7 +++++++ src/types.a68 | 17 +++++++++++++++++ src/types2.a68 | 23 +++++++++++++++++++++++ src/types3.a68 | 26 ++++++++++++++++++++++++++ 31 files changed, 308 insertions(+) create mode 100644 .gitignore create mode 100644 src/adder.a68 create mode 100644 src/aplusb.a68 create mode 100644 src/aplusb2.a68 create mode 100644 src/ass1.a68 create mode 100644 src/ass2.a68 create mode 100644 src/ass3.a68 create mode 100644 src/ass4.a68 create mode 100644 src/ass5.a68 create mode 100644 src/ass6.a68 create mode 100644 src/assignments.a68 create mode 100644 src/big_fact.a68 create mode 100644 src/coerce-test.a68 create mode 100644 src/copyback.a68 create mode 100644 src/customplusab.a68 create mode 100644 src/fact.a68 create mode 100644 src/fact_input.a68 create mode 100644 src/hello.a68 create mode 100644 src/input.txt create mode 100644 src/more_slicing.a68 create mode 100644 src/ops.a68 create mode 100644 src/output.txt create mode 100644 src/prec_chk.a68 create mode 100644 src/realplusab.a68 create mode 100644 src/rows.a68 create mode 100644 src/sizing.a68 create mode 100644 src/slices.a68 create mode 100644 src/test1.a68 create mode 100644 src/types.a68 create mode 100644 src/types2.a68 create mode 100644 src/types3.a68 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0893017 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +bin/ +*.l +*.seed diff --git a/src/adder.a68 b/src/adder.a68 new file mode 100644 index 0000000..0a55d39 --- /dev/null +++ b/src/adder.a68 @@ -0,0 +1,4 @@ +BEGIN INT m = read int; + INT n = read int; + print (("sum is", m + n)) +END diff --git a/src/aplusb.a68 b/src/aplusb.a68 new file mode 100644 index 0000000..87836c1 --- /dev/null +++ b/src/aplusb.a68 @@ -0,0 +1 @@ +print((read int + read int)) diff --git a/src/aplusb2.a68 b/src/aplusb2.a68 new file mode 100644 index 0000000..c7c0948 --- /dev/null +++ b/src/aplusb2.a68 @@ -0,0 +1,5 @@ + +open(stand in, "input.txt", stand in channel); +open(stand out, "output.txt", stand out channel); +print((read int + read int)) + diff --git a/src/ass1.a68 b/src/ass1.a68 new file mode 100644 index 0000000..3524b3a --- /dev/null +++ b/src/ass1.a68 @@ -0,0 +1,5 @@ +BEGIN + INT a = 1; + a := a + 1; + print(a) +END \ No newline at end of file diff --git a/src/ass2.a68 b/src/ass2.a68 new file mode 100644 index 0000000..51395d1 --- /dev/null +++ b/src/ass2.a68 @@ -0,0 +1,5 @@ +BEGIN + INT a = 1; + a := 2; + print(a) +END \ No newline at end of file diff --git a/src/ass3.a68 b/src/ass3.a68 new file mode 100644 index 0000000..8f27fc8 --- /dev/null +++ b/src/ass3.a68 @@ -0,0 +1,6 @@ +BEGIN + INT a; + a := 1; + a := a + 1; + print(a) +END \ No newline at end of file diff --git a/src/ass4.a68 b/src/ass4.a68 new file mode 100644 index 0000000..6400121 --- /dev/null +++ b/src/ass4.a68 @@ -0,0 +1,5 @@ +BEGIN + REF INT a = LOC INT := 1; + a := a + 1; + print(a) +END \ No newline at end of file diff --git a/src/ass5.a68 b/src/ass5.a68 new file mode 100644 index 0000000..480c6ad --- /dev/null +++ b/src/ass5.a68 @@ -0,0 +1,6 @@ +BEGIN + REF INT a = LOC INT; # Allocate but don’t initialize # + a := 1; # Assign initial value # + a := a + 1; # Increment # + print(a) +END \ No newline at end of file diff --git a/src/ass6.a68 b/src/ass6.a68 new file mode 100644 index 0000000..5f88114 --- /dev/null +++ b/src/ass6.a68 @@ -0,0 +1,6 @@ +BEGIN + INT a := 1; + a := a + 1; + a +:= 1; + print(a) +END \ No newline at end of file diff --git a/src/assignments.a68 b/src/assignments.a68 new file mode 100644 index 0000000..bc00789 --- /dev/null +++ b/src/assignments.a68 @@ -0,0 +1,18 @@ +BEGIN + INT a; # Declare without init # + INT b; + INT c; + + a := 1; # Assign separately # + b := 1; + c := a + b; + + print(c); + + a := a + 1; # Standard assignment # + INT poly = a * (a + 1); + poly := poly +1; + + print(a); + print(poly) +END \ No newline at end of file diff --git a/src/big_fact.a68 b/src/big_fact.a68 new file mode 100644 index 0000000..e80461c --- /dev/null +++ b/src/big_fact.a68 @@ -0,0 +1,20 @@ +BEGIN + PR precision 3000 PR + OP *:= = (REF LONG LONG INT x, LONG LONG INT y) VOID: x := x * y; + + PROC factorial = (LONG LONG INT n) LONG LONG INT: + BEGIN + LONG LONG INT result := 1; + LONG LONG INT i := 1; + WHILE i <= n DO + result *:= i; + i +:= 1 + OD; + result + END; + + print("Enter a number: "); + LONG LONG INT a; + read((a)); + print(("Factorial of ", a, " is ", factorial(a))) +END \ No newline at end of file diff --git a/src/coerce-test.a68 b/src/coerce-test.a68 new file mode 100644 index 0000000..726929c --- /dev/null +++ b/src/coerce-test.a68 @@ -0,0 +1,5 @@ +BEGIN + LONG INT a; + read((a)); + print(a) # Just echo the input # +END \ No newline at end of file diff --git a/src/copyback.a68 b/src/copyback.a68 new file mode 100644 index 0000000..40a5d83 --- /dev/null +++ b/src/copyback.a68 @@ -0,0 +1,11 @@ +BEGIN + INT n = read int; + FLEX [n] INT u; + + [UPB u + 1] INT v; + v[: UPB u] := u; + u := v; + print((n, newline)); + print((v, newline)); + print((u, newline)) +END \ No newline at end of file diff --git a/src/customplusab.a68 b/src/customplusab.a68 new file mode 100644 index 0000000..fe215b8 --- /dev/null +++ b/src/customplusab.a68 @@ -0,0 +1,19 @@ +BEGIN + OP +:= = (REF INT x, INT y) VOID: x := x + y; # Define +:= # + + INT a; # No init # + INT b; + INT c; + + a := 1; + b := 1; + c := a + b; + + print(c); + + a +:= 1; # Use custom operator # + INT poly = a * (a + 1); + + print(a); + print(poly) +END \ No newline at end of file diff --git a/src/fact.a68 b/src/fact.a68 new file mode 100644 index 0000000..6373451 --- /dev/null +++ b/src/fact.a68 @@ -0,0 +1,7 @@ +BEGIN + PROC factorial = (INT n) INT: + IF n <= 1 THEN 1 ELSE n * factorial(n - 1) FI; + + INT a := 5; + print(("Factorial of ", a, " is ", factorial(a))) # Factorial of +5 is +120 # +END \ No newline at end of file diff --git a/src/fact_input.a68 b/src/fact_input.a68 new file mode 100644 index 0000000..969837e --- /dev/null +++ b/src/fact_input.a68 @@ -0,0 +1,20 @@ +BEGIN + PR precision 256 PR + OP *:= = (REF LONG LONG INT x, LONG LONG INT y) VOID: x := x * y; + + PROC factorial = (LONG LONG INT n) LONG LONG INT: + BEGIN + LONG LONG INT result := 1; + LONG LONG INT i := 1; + WHILE i <= n DO + result *:= i; + i +:= 1 + OD; + result + END; + + print("Enter a number: "); + LONG LONG INT a; + read((a)); + print(("Factorial of ", a, " is ", factorial(a))) +END \ No newline at end of file diff --git a/src/hello.a68 b/src/hello.a68 new file mode 100644 index 0000000..6b054cd --- /dev/null +++ b/src/hello.a68 @@ -0,0 +1,5 @@ +BEGIN + INT a := 1; # Mutable variable # + a +:= 1; # Built-in increment # + print(a) # Output 2 # +END \ No newline at end of file diff --git a/src/input.txt b/src/input.txt new file mode 100644 index 0000000..ebcee1a --- /dev/null +++ b/src/input.txt @@ -0,0 +1 @@ +3 4 diff --git a/src/more_slicing.a68 b/src/more_slicing.a68 new file mode 100644 index 0000000..59c593f --- /dev/null +++ b/src/more_slicing.a68 @@ -0,0 +1,6 @@ +BEGIN + STRING s := "abcdefghijklmnopqrstuvwxyz"; + print((s, newline)); + s[2 : 7] := s[9 : 14]; + print((s, newline)) +END \ No newline at end of file diff --git a/src/ops.a68 b/src/ops.a68 new file mode 100644 index 0000000..7da599a --- /dev/null +++ b/src/ops.a68 @@ -0,0 +1,2 @@ +OP *:= = (REF INT x, INT y) VOID: x := x * y; # Multiply-and-assign # +PROC add = (INT x, INT y) INT: x + y; # Simple procedure # \ No newline at end of file diff --git a/src/output.txt b/src/output.txt new file mode 100644 index 0000000..43d671f --- /dev/null +++ b/src/output.txt @@ -0,0 +1 @@ + +7 \ No newline at end of file diff --git a/src/prec_chk.a68 b/src/prec_chk.a68 new file mode 100644 index 0000000..c849ea9 --- /dev/null +++ b/src/prec_chk.a68 @@ -0,0 +1,5 @@ +BEGIN + PR precision 256 PR + LONG LONG INT a := LONG LONG 10 ^ 200; # Widen 10 to LONG LONG INT # + print(a) +END \ No newline at end of file diff --git a/src/realplusab.a68 b/src/realplusab.a68 new file mode 100644 index 0000000..488a950 --- /dev/null +++ b/src/realplusab.a68 @@ -0,0 +1,17 @@ +BEGIN + INT a; # No init # + INT b; + INT c; + + a := 1; + b := 1; + c := a + b; + + print(c); + + a PLUSAB 1; # Test prelude operator # + INT poly = a * (a + 1); + + print(a); + print(poly) +END \ No newline at end of file diff --git a/src/rows.a68 b/src/rows.a68 new file mode 100644 index 0000000..df24c6e --- /dev/null +++ b/src/rows.a68 @@ -0,0 +1,16 @@ +BEGIN + OP ELEM = (INT needle, [] INT haystack) BOOL: + BEGIN + BOOL found := FALSE; + FOR i FROM LWB haystack TO UPB haystack DO + IF haystack[i] = needle THEN + found := TRUE + FI + OD; + found + END; + + [] INT first primes = (1, 3, 5, 7, 11); + print (first primes[1]); + print (1 ELEM first primes) # prints two times +1 # +END \ No newline at end of file diff --git a/src/sizing.a68 b/src/sizing.a68 new file mode 100644 index 0000000..f63c6d5 --- /dev/null +++ b/src/sizing.a68 @@ -0,0 +1,16 @@ +BEGIN + INT n = read int; # Read size n from console input (e.g., 3) # + FLEX [n] INT u; # Declare flexible row u with n elements (uninitialized) # + FOR i FROM 1 TO n DO + u[i] := read int # Initialize u’s n elements with console input # + OD; + [n + 1] INT v; # Declare fixed row v with n+1 elements (uninitialized) # + FOR i FROM 1 TO n + 1 DO + v[i] := 0 # Initialize all v elements to 0 # + OD; + v[: UPB u] := u; # Copy u’s n elements into v’s first n positions # + u := v; # Assign v to u, resizing u to n+1 and copying values # + print((n, newline)); # Print original size n # + print((u, newline)); # Print u (now n+1 elements) # + print((v, newline)) # Print v (n+1 elements) # +END \ No newline at end of file diff --git a/src/slices.a68 b/src/slices.a68 new file mode 100644 index 0000000..d0fa548 --- /dev/null +++ b/src/slices.a68 @@ -0,0 +1,20 @@ +BEGIN + [, ] INT r = + ((1, 2, 3, 4), + (5, 6, 7, 8), + (9, 10, 11, 12), + (13, 14, 15, 16)); + + put(stand out, (r[2, 2], newline)); + put(stand out, (r[3, ], " ", newline)); # Space between elements # + put(stand out, (r[, 2 UPB r], " ", newline)); + put(stand out, (r[3, 2], newline)); + put(stand out, (r[2, ], " ", newline)); + put(stand out, (r[, 3], " ", newline)); + + [] CHAR word = 3 * "ab"; + print((word,newline)); + + [1 : 5] INT first_primes := (1, 3, 5, 7, 11); + print(first_primes[3]) +END diff --git a/src/test1.a68 b/src/test1.a68 new file mode 100644 index 0000000..8b700fc --- /dev/null +++ b/src/test1.a68 @@ -0,0 +1,7 @@ +#include "ops.a68" +BEGIN + INT a := 1; + a +:= 1; # Built-in # + a *:= 2; # Custom from ops.a68 # + print(a) # 4 (1 + 1 = 2, 2 * 2 = 4) # +END \ No newline at end of file diff --git a/src/types.a68 b/src/types.a68 new file mode 100644 index 0000000..5a4b426 --- /dev/null +++ b/src/types.a68 @@ -0,0 +1,17 @@ +BEGIN + +INT a = 1; # standard integer # +LONG b = 1234567890987654321; # long integer # +LONG LONG c = 1234567890987654321234567890987654321; # long long int # +INT x = 5, y = 10; # works # +INT one = 1, start = one; # will not work because order is arbitrary with comma # +INT one = 1; INT start = one; # will work because order is linear with semicolon # +REAL n = 2.795; # standard real # +LONG REAL m = 18.2020202020202999222922922222991; # long real # +DOUBLE pi times 2 = 6.283185307179586476925286767; # also long real # + +INT q = READ INT; # read input to integer blam # +REAL k = READ REAL; # read input to real kapow # +REAL frumkin = 18.625;INT fleegle = ROUND(frumpkin) # real to int conversion is done by rounding # + +END diff --git a/src/types2.a68 b/src/types2.a68 new file mode 100644 index 0000000..7b65a83 --- /dev/null +++ b/src/types2.a68 @@ -0,0 +1,23 @@ +BEGIN + INT a = 1; # standard integer # + LONG INT b = 1234567890987654321; # long integer (corrected LONG syntax) # + LONG LONG INT c = 1234567890987654321234567890987654321; # long long int (corrected syntax) # + INT x = 5, y = 10; # works # + INT one = 1; INT start = one; # works with semicolons # + REAL n = 2.795; # standard real # + LONG REAL m = 18.2020202020202999222922922222991; # long real # + DOUBLE pi_times_2 = 6.283185307179586476925286767; # also long real (no spaces in identifier) # + + INT q; # declare q first # + REAL k; # declare k first # + read((q, k)); # read input into q and k # + + REAL frumkin = 18.625; + INT fleegle = ROUND(frumkin); # real to int conversion by rounding # + + print(("a = ", a, " b = ", b, newline)); + print(("c = ", c, " x = ", x, " y = ", y, newline)); + print(("one = ", one, " start = ", start, newline)); + print(("n = ", n, " m = ", m, newline)); + print(("q = ", q, " k = ", k, newline)) # optional: output to verify # +END diff --git a/src/types3.a68 b/src/types3.a68 new file mode 100644 index 0000000..a94ef62 --- /dev/null +++ b/src/types3.a68 @@ -0,0 +1,26 @@ +BEGIN + INT a = 1; # standard integer # + LONG INT b = 1234567890987654321; # long integer # + LONG LONG INT c = 1234567890987654321234567890987654321; # long long int # + INT x = 5, y = 10; # works # + INT one = 1; INT strt = one; # works with semicolons # + REAL n = 2.795; # standard real # + LONG REAL m = 18.2020202020202999222922922222991; # long real # + DOUBLE pi_times_2 = 6.283185307179586476925286767; # also long real # + + INT q; # declare q first # + REAL k; # declare k first # + read((q, k)); # read input into q and k # + + REAL frumkin = 18.625; + INT fleegle = ROUND(frumkin); # real to int conversion by rounding # + + print(("a = ", a, " b = ", b, newline)); + print(("c = ", c, newline)); + print(("x = ", x, " y = ", y, newline)); + print(("one = ", one, " start = ", strt, newline)); + print(("n = ", n, " m = ", m, newline)); + print(("pi_times_2 = ", pi_times_2, newline)); + print(("q = ", q, " k = ", k, newline)); + print(("frumkin = ", frumkin, " fleegle = ", fleegle, newline)) +END