aboutsummaryrefslogtreecommitdiff
path: root/l
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2017-09-02 15:48:01 +0200
committertomsmeding <tom.smeding@gmail.com>2017-09-02 15:51:08 +0200
commitc3c19f3220485af12c3a5561ad1754bf546e7429 (patch)
tree80e601853f7259722c4ec8fa3dc2704cd3f125f9 /l
parent45e9991a1f83974c3459037f4791d865f5b342f1 (diff)
Move *.lang files to a directory
Diffstat (limited to 'l')
-rw-r--r--l/arrays.lang7
-rw-r--r--l/bf.lang104
-rw-r--r--l/chaincond.lang14
-rw-r--r--l/chartest.lang19
-rw-r--r--l/compare.lang10
-rw-r--r--l/dupifs.lang8
-rw-r--r--l/fibo.lang31
-rw-r--r--l/opttest.lang8
-rw-r--r--l/primes.bf194
-rw-r--r--l/putint.lang4
-rw-r--r--l/putstr.lang19
-rw-r--r--l/strlen.lang11
-rw-r--r--l/struct.lang34
-rw-r--r--l/test.lang20
14 files changed, 483 insertions, 0 deletions
diff --git a/l/arrays.lang b/l/arrays.lang
new file mode 100644
index 0000000..8cab05f
--- /dev/null
+++ b/l/arrays.lang
@@ -0,0 +1,7 @@
+func int main() {
+ int[] arr := new int[5];
+ arr[0] = 97;
+ arr[1] = 98;
+ putc(char(arr[0]));
+ return 0;
+}
diff --git a/l/bf.lang b/l/bf.lang
new file mode 100644
index 0000000..0ce6389
--- /dev/null
+++ b/l/bf.lang
@@ -0,0 +1,104 @@
+type byte := char;
+
+func putstr(char[] str) {
+ int i := 0;
+ while (str[i] != '\0') {
+ putc(str[i]);
+ i = i + 1;
+ }
+}
+
+func int[] makejumpmap(char[] src, int srclen) {
+ int[] jm := new int[srclen];
+ int[] stack := new int[srclen];
+ int stkp := 0;
+ int i := 0;
+ while (i < srclen) {
+ // putint(i); putc(' '); putint(stkp); putc(' '); putc(src[i]); putc('\n');
+ if (src[i] == '[') {
+ stack[stkp] = i;
+ stkp = stkp + 1;
+ }
+ if (src[i] == ']') {
+ stkp = stkp - 1;
+ int j := stack[stkp];
+ jm[i] = j;
+ jm[j] = i;
+ }
+ i = i + 1;
+ }
+ if (stkp != 0) {
+ putstr("Unmatched opening brackets in BF source\n");
+ exit(1);
+ }
+ /*i = 0;
+ while (i < srclen) {
+ putint(jm[i]);
+ putc(' ');
+ i = i + 1;
+ }
+ putc('\n');*/
+ return jm;
+}
+
+func interpret(char[] src, int srclen) {
+ int[] jm := makejumpmap(src, srclen);
+ byte[] mem := new char[4088];
+ int ip := 0;
+ int memp := 0;
+ while (src[ip] != '\0') {
+ // putint(ip); putc('\n');
+ char c := src[ip];
+ if (c == '+') {mem[memp] = mem[memp] + byte(1);}
+ if (c == '-') {mem[memp] = mem[memp] - byte(1);}
+ if (c == '>') {memp = memp + 1;}
+ if (c == '<') {memp = memp - 1;}
+ if (c == '.') {putc(mem[memp]);}
+ if (c == ',') {mem[memp] = byte(getc());}
+ if (c == '[') {
+ if (mem[memp] == '\0') {
+ ip = jm[ip];
+ }
+ }
+ if (c == ']') {
+ if (mem[memp] != '\0') {
+ ip = jm[ip];
+ }
+ }
+ ip = ip + 1;
+ }
+
+ // debugger;
+
+ putc('\n');
+ memp = 0;
+ while (memp < 10) {
+ putint(int(mem[memp]) & 0xff); putc(' ');
+ memp = memp + 1;
+ }
+ putc('\n');
+}
+
+func int main() {
+ int bufsize := 12280;
+ char[] source := new char[12280];
+
+ int sourcelen := 0;
+ while (1) {
+ int c := getc();
+ // putc(char(c));
+ if (c < 0) {
+ break;
+ } else {
+ source[sourcelen] = char(c);
+ sourcelen = sourcelen + 1;
+ if (sourcelen >= bufsize - 1) {
+ break;
+ }
+ }
+ }
+ source[sourcelen] = '\0';
+
+ interpret(source, sourcelen);
+ return 0;
+}
diff --git a/l/chaincond.lang b/l/chaincond.lang
new file mode 100644
index 0000000..542cae9
--- /dev/null
+++ b/l/chaincond.lang
@@ -0,0 +1,14 @@
+func int main() {
+ int a := 0;
+ if (a == 0) {
+ a = 1;
+ } else {
+ a = 2;
+ }
+ if (a > 0) {
+ a = 10;
+ } else {
+ a = 20;
+ }
+ return a;
+} \ No newline at end of file
diff --git a/l/chartest.lang b/l/chartest.lang
new file mode 100644
index 0000000..208bcab
--- /dev/null
+++ b/l/chartest.lang
@@ -0,0 +1,19 @@
+int glob := 42;
+
+func f(char x) {
+ putc(x);
+}
+
+func char g() {
+ return '\n';
+}
+
+func int main() {
+ char c := 'a';
+ f(c);
+ putc('\n');
+ char d := g();
+ c = d + d + d + d + d + d + d + d + 'a';
+ putc(c);
+ return 0;
+}
diff --git a/l/compare.lang b/l/compare.lang
new file mode 100644
index 0000000..1de9a70
--- /dev/null
+++ b/l/compare.lang
@@ -0,0 +1,10 @@
+func int main() {
+ int i := 0;
+ while (i != 10) {
+ if (i == 5) {
+ i = i + 2;
+ }
+ i = i + 1;
+ }
+ return 0;
+}
diff --git a/l/dupifs.lang b/l/dupifs.lang
new file mode 100644
index 0000000..91e7ca4
--- /dev/null
+++ b/l/dupifs.lang
@@ -0,0 +1,8 @@
+func int main() {
+ int c := getc();
+ int i := 0;
+ if (c == 97) {i = 1;}
+ if (c == 98) {i = 2;}
+ putint(i);
+ return 0;
+}
diff --git a/l/fibo.lang b/l/fibo.lang
new file mode 100644
index 0000000..e1a7a4a
--- /dev/null
+++ b/l/fibo.lang
@@ -0,0 +1,31 @@
+func printnum(int n) {
+ if (n == 0) {putc('0'); return;}
+ if (n < 0) {putc('-'); n = -n;}
+ while (n > 0) {
+ putc('0' + char(n % 10));
+ n = n / 10;
+ }
+}
+
+func int fibo(int n) {
+ int a := 0;
+ int b := 1;
+ int i := 0;
+ while (i < n) {
+ int c := a + b;
+ a = b;
+ b = c;
+ i = i + 1;
+ }
+ return b;
+}
+
+func int main() {
+ int i := 0;
+ while (i <= 20) {
+ printnum(fibo(i));
+ putc('\n');
+ i = i + 1;
+ }
+ return 0;
+}
diff --git a/l/opttest.lang b/l/opttest.lang
new file mode 100644
index 0000000..9f87e78
--- /dev/null
+++ b/l/opttest.lang
@@ -0,0 +1,8 @@
+func int main() {
+ int a := 1;
+ int b := 2;
+ int x := a + b;
+ b = 100;
+ putint(x); putc('\n');
+ return 0;
+}
diff --git a/l/primes.bf b/l/primes.bf
new file mode 100644
index 0000000..f492705
--- /dev/null
+++ b/l/primes.bf
@@ -0,0 +1,194 @@
+compute prime numbers
+to use type the max number then push Alt 1 0
+===================================================================
+======================== OUTPUT STRING ============================
+===================================================================
+>++++++++[<++++++++>-]<++++++++++++++++.[-]
+>++++++++++[<++++++++++>-]<++++++++++++++.[-]
+>++++++++++[<++++++++++>-]<+++++.[-]
+>++++++++++[<++++++++++>-]<+++++++++.[-]
+>++++++++++[<++++++++++>-]<+.[-]
+>++++++++++[<++++++++++>-]<+++++++++++++++.[-]
+>+++++[<+++++>-]<+++++++.[-]
+>++++++++++[<++++++++++>-]<+++++++++++++++++.[-]
+>++++++++++[<++++++++++>-]<++++++++++++.[-]
+>+++++[<+++++>-]<+++++++.[-]
+>++++++++++[<++++++++++>-]<++++++++++++++++.[-]
+>++++++++++[<++++++++++>-]<+++++++++++.[-]
+>+++++++[<+++++++>-]<+++++++++.[-]
+>+++++[<+++++>-]<+++++++.[-]
+===================================================================
+======================== INPUT NUMBER ============================
+===================================================================
+[[[ COMMENT
+ + cont=1
+ [
+ - cont=0
+ >,
+ ======SUB10======
+ ----------
+
+ [ not 10
+ <+> cont=1
+ =====SUB38======
+ ----------
+ ----------
+ ----------
+ --------
+ >
+ =====MUL10=======
+ [>+>+<<-]>>[<<+>>-]< dup
+ >>>+++++++++
+ [
+ <<<
+ [>+>+<<-]>>[<<+>>-]< dup
+ [<<+>>-]
+ >>-
+ ]
+ <<<[-]<
+ ======RMOVE1======
+ <
+ [>+<-]
+ ]
+ <
+ ]
+ >>[<<+>>-]<<
+]]]
+++++++++++.[-] OUTPUT A NEWLINE
+- THE INPUT NUMBER (WE DONT HAVE STDIN YET)
+===================================================================
+======================= PROCESS NUMBER ===========================
+===================================================================
+==== ==== ==== ====
+numd numu teid teiu
+==== ==== ==== ====
+>+<-
+[
+ >+
+ ======DUP======
+ [>+>+<<-]>>[<<+>>-]<
+ >+<--
+ >>>>>>>>+<<<<<<<< isprime=1
+ [
+ >+
+ <-
+ =====DUP3=====
+ <[>>>+>+<<<<-]>>>>[<<<<+>>>>-]<<<
+ =====DUP2=====
+ >[>>+>+<<<-]>>>[<<<+>>>-]<<< <
+ >>>
+ ====DIVIDES=======
+ [>+>+<<-]>>[<<+>>-]< DUP i=div
+
+ <<
+ [
+ >>>>>+ bool=1
+ <<<
+ [>+>+<<-]>>[<<+>>-]< DUP
+ [>>[-]<<-] IF i THEN bool=0
+ >>
+ [ IF i=0
+ <<<<
+ [>+>+<<-]>>[<<+>>-]< i=div
+ >>>
+ - bool=0
+ ]
+ <<<
+ - DEC i
+ <<
+ -
+ ]
+
+ +>>[<<[-]>>-]<<
+ >[-]< CLR div
+ =====END DIVIDES====
+ [>>>>>>[-]<<<<<<-] if divides then isprime=0
+ <<
+ >>[-]>[-]<<<
+ ]
+ >>>>>>>>
+ [
+ -
+ <<<<<<<[-]<<
+ [>>+>+<<<-]>>>[<<<+>>>-]<<<
+ >>
+ ===================================================================
+ ======================== OUTPUT NUMBER ===========================
+ ===================================================================
+ [>+<-]>
+
+ [
+ ======DUP======
+ [>+>+<<-]>>[<<+>>-]<
+
+
+ ======MOD10====
+ >+++++++++<
+ [
+ >>>+<< bool= 1
+ [>+>[-]<<-] bool= ten==0
+ >[<+>-] ten = tmp
+ >[<<++++++++++>>-] if ten=0 ten=10
+ <<- dec ten
+ <- dec num
+ ]
+ +++++++++ num=9
+ >[<->-]< dec num by ten
+
+ =======RROT======
+ [>+<-]
+ < [>+<-]
+ < [>+<-]
+ >>>[<<<+>>>-]
+ <
+
+ =======DIV10========
+ >+++++++++<
+ [
+ >>>+<< bool= 1
+ [>+>[-]<<-] bool= ten==0
+ >[<+>-] ten = tmp
+ >[<<++++++++++>>>+<-] if ten=0 ten=10 inc div
+ <<- dec ten
+ <- dec num
+ ]
+ >>>>[<<<<+>>>>-]<<<< copy div to num
+ >[-]< clear ten
+
+ =======INC1=========
+ <+>
+ ]
+
+ <
+ [
+ =======MOVER=========
+ [>+<-]
+
+ =======ADD48========
+ +++++++[<+++++++>-]<->
+
+ =======PUTC=======
+ <.[-]>
+
+ ======MOVEL2========
+ >[<<+>>-]<
+
+ <-
+ ]
+
+ >++++[<++++++++>-]<.[-]
+
+ ===================================================================
+ =========================== END FOR ===============================
+ ===================================================================
+ >>>>>>>
+ ]
+ <<<<<<<<
+ >[-]<
+ [-]
+ <<-
+]
+
+======LF========
+
+++++++++++.[-]
diff --git a/l/putint.lang b/l/putint.lang
new file mode 100644
index 0000000..198b4ee
--- /dev/null
+++ b/l/putint.lang
@@ -0,0 +1,4 @@
+func int main() {
+ putint(-12345678); putc('\n');
+ return 0;
+}
diff --git a/l/putstr.lang b/l/putstr.lang
new file mode 100644
index 0000000..f132bda
--- /dev/null
+++ b/l/putstr.lang
@@ -0,0 +1,19 @@
+func putstr(char[] str) {
+ int i := 0;
+ while (str[i] != '\0') {
+ putc(str[i]);
+ i = i + 1;
+ }
+}
+
+func int main() {
+ char[] str := new char[100];
+ str[0] = 'k';
+ str[1] = 'a';
+ str[2] = str[1];
+ str[3] = 's';
+ str[4] = '\n';
+ str[5] = '\0';
+ putstr(str);
+ return 0;
+}
diff --git a/l/strlen.lang b/l/strlen.lang
new file mode 100644
index 0000000..27c23a3
--- /dev/null
+++ b/l/strlen.lang
@@ -0,0 +1,11 @@
+func int strlen(char[] str) {
+ int i := 0;
+ char c := str[i];
+ while (c != '\0') {
+ i = i + 1;
+ c = str[i];
+ }
+ return i;
+}
+
+func int main() {return 0;}
diff --git a/l/struct.lang b/l/struct.lang
new file mode 100644
index 0000000..00b3442
--- /dev/null
+++ b/l/struct.lang
@@ -0,0 +1,34 @@
+type S := struct {
+ int x;
+ char y;
+};
+
+S global := {x = 1, y = '!'};
+
+func f(int iets1, S s, int iets2) {
+ putint(s.x); putc(s.y); putc('\n');
+ putint(iets1); putc(' '); putint(iets2); putc('\n');
+}
+
+func int main() {
+ /*global.x = 3 * global.x + int(global.y);
+ putint(global.x + 1); putc(global.y); putc('\n');
+ int a := getc();
+ int b := getc();
+ getc(); // newline
+ S ding := {x = 2*a, y = 'a'};
+ // return ding.x;
+ f(123, ding, 456);
+ S ding2 := ding;
+ ding2.x = ding2.x;
+ // ding2.y = ding2.y;
+ f(234, ding2, 567);
+ return int(ding.y) + a + b;*/
+
+ int a := getc();
+ getc(); // newline
+ S ding := {x = 2*a, y = 'a'};
+ S ding2 := ding;
+ f(123, ding2, 456);
+ return 0;
+}
diff --git a/l/test.lang b/l/test.lang
new file mode 100644
index 0000000..f03b3e3
--- /dev/null
+++ b/l/test.lang
@@ -0,0 +1,20 @@
+int glob := 100;
+
+func f() {
+ glob = glob + 10;
+}
+
+func g(int x , int y) {
+ glob = glob + x + 2 * y;
+}
+
+func int main() {
+ int i := 0; /* block /* comments */ nest! */
+ // int[4] arr := 2;
+ i = i + 1;
+ f();
+ if (i > 0 && i < 10) {
+ int i := 20;
+ g(i , 2 * i);
+ }
+}