From 0e856ca15d8aa1499edc33c7302d4d97be404290 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Sun, 8 Nov 2026 09:49:14 +0300 Subject: publish --- src/double.mil | 18 ++++++++++++++++++ src/double.ms | 33 +++++++++++++++++++++++++++++++++ src/fac-tail.mil | 19 +++++++++++++++++++ src/fac.mil | 13 +++++++++++++ src/fib-tail.mil | 20 ++++++++++++++++++++ src/fib.mil | 13 +++++++++++++ src/gcd.mil | 18 ++++++++++++++++++ 7 files changed, 134 insertions(+) create mode 100644 src/double.mil create mode 100644 src/double.ms create mode 100644 src/fac-tail.mil create mode 100644 src/fac.mil create mode 100644 src/fib-tail.mil create mode 100644 src/fib.mil create mode 100644 src/gcd.mil (limited to 'src') diff --git a/src/double.mil b/src/double.mil new file mode 100644 index 0000000..aea5387 --- /dev/null +++ b/src/double.mil @@ -0,0 +1,18 @@ +/* double.mil --- read double write -*- MiLan -*- */ +/* function declarations */ +FUNC double (1) RETURNS; + +/* program */ +BEGIN + LOCALS n; + n := READ; + n := double (n); + WRITE (n) +END + +/* function definitions */ +FUNC double (n) RETURNS +BEGIN + RETURN (n + n) +END +/* double.mil ends here */ diff --git a/src/double.ms b/src/double.ms new file mode 100644 index 0000000..2aa3145 --- /dev/null +++ b/src/double.ms @@ -0,0 +1,33 @@ +; double.ms --- input double print -*- MiLan-VM -*- +; Double integer with a function call. +; Args and result are in callers stack frame, thus the +; caller makes space for result and frees args after the call. + +; BEGIN +; LOCALS n; +0: SHIFT 1 ; make space for local variable `n' +; n := READ; +1: INPUT ; read number to double +2: STOREB 0 ; store read number to the first local variable `n' +; n := double (n); +3: LOADB 0 ; load read number from the first local variable `n' +4: NOP ; SHIFT 1, if there are no args and call returns +5: CALL 10 ; call double +6: STOREB 0 ; store returned value to first local variable `n' +; WRITE (n); +7: LOADB 0 ; load doubled number from the first local variable `n' +8: PRINT ; print double number +; END +9: STOP ; stop + +; FUNC double (n) RETURNS +; BEGIN +10: NOP ; SHIFT N, where N is number of local variables +; RETURN (n + n); +11: LOADB -3 ; load first, and only, argument +12: LOADB -3 ; load first, and only, argument +13: ADD ; add it +14: STOREB -3 ; write the result into (implicit) first arg +15: RETURN 0 ; return, poping N - RETURNS args +; END +; double.ms ends here diff --git a/src/fac-tail.mil b/src/fac-tail.mil new file mode 100644 index 0000000..a97705d --- /dev/null +++ b/src/fac-tail.mil @@ -0,0 +1,19 @@ +/* fac-tail.mil --- compute factorial of a number using tail recursion -*- MiLan -*- */ +FUNC fac (1) RETURNS; +FUNC fac (2) RETURNS; + +BEGIN + WRITE (fac (READ)) +END + +FUNC fac (n) RETURNS +BEGIN + RETURN fac (n, 1) +END + +FUNC fac (n acc) RETURNS +BEGIN + IF n = 0 THEN RETURN acc FI; + RETURN fac (n - 1, n*acc) +END +/* fac-tail.mil end here */ diff --git a/src/fac.mil b/src/fac.mil new file mode 100644 index 0000000..3e19aa8 --- /dev/null +++ b/src/fac.mil @@ -0,0 +1,13 @@ +/* fac.mil --- compute factorial of a number -*- MiLan -*- */ +FUNC fac (1) RETURNS; + +BEGIN + WRITE (fac (READ)) +END + +FUNC fac (n) RETURNS +BEGIN + IF n = 0 THEN RETURN 1 FI; + RETURN (n*fac (n - 1)) +END +/* fac.mil end here */ diff --git a/src/fib-tail.mil b/src/fib-tail.mil new file mode 100644 index 0000000..13655fd --- /dev/null +++ b/src/fib-tail.mil @@ -0,0 +1,20 @@ +/* fib-tail.mil --- compute fibonacci number using tail recursion -*- MiLan -*- */ +FUNC fib (1) RETURNS; +FUNC fib (3) RETURNS; + +BEGIN + WRITE (fib (READ)) +END + +FUNC fib (n) RETURNS +BEGIN + RETURN fib (0, 1, n) +END + +FUNC fib (a b n) RETURNS +BEGIN + IF n = 0 THEN RETURN a FI; + IF n = 1 THEN RETURN b FI; + RETURN fib (b, a + b, n - 1) +END +/* fib-tail.mil end here*/ diff --git a/src/fib.mil b/src/fib.mil new file mode 100644 index 0000000..6b14ca0 --- /dev/null +++ b/src/fib.mil @@ -0,0 +1,13 @@ +/* fib.mil --- compute fibonacci number -*- MiLan -*- */ +FUNC fib (1) RETURNS; + +BEGIN + WRITE (fib (READ)) +END + +FUNC fib (n) RETURNS +BEGIN + IF n < 2 THEN RETURN (n) FI; + RETURN (fib (n - 1) + fib (n - 2)) +END +/* fib.mil ends here */ diff --git a/src/gcd.mil b/src/gcd.mil new file mode 100644 index 0000000..baefbee --- /dev/null +++ b/src/gcd.mil @@ -0,0 +1,18 @@ +/* gcd.mil -- compute greatest common divisor -*- MiLan -*- */ +FUNC gcd (0); + +BEGIN + a := READ; + b := READ; + gcd (); + WRITE (c) +END + +FUNC gcd () +BEGIN + WHILE a != b DO + IF a < b THEN b := b - a + ELSE a := a - b FI OD; + c := a +END +/* gcd.mil ends here */ -- cgit v1.3.1