mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-06 06:48:48 +00:00
Pattern matching in X86
This commit is contained in:
parent
1f1ef2ce57
commit
57588f2605
18 changed files with 179 additions and 3 deletions
4
regression/orig/test040.log
Normal file
4
regression/orig/test040.log
Normal file
|
|
@ -0,0 +1,4 @@
|
||||||
|
> 1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
2
regression/orig/test041.log
Normal file
2
regression/orig/test041.log
Normal file
|
|
@ -0,0 +1,2 @@
|
||||||
|
> 600
|
||||||
|
1800
|
||||||
10
regression/orig/test042.log
Normal file
10
regression/orig/test042.log
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
> 0
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
4
|
||||||
|
4
|
||||||
|
4
|
||||||
|
4
|
||||||
|
4
|
||||||
3
regression/orig/test043.log
Normal file
3
regression/orig/test043.log
Normal file
|
|
@ -0,0 +1,3 @@
|
||||||
|
> 0
|
||||||
|
100
|
||||||
|
300
|
||||||
6
regression/orig/test044.log
Normal file
6
regression/orig/test044.log
Normal file
|
|
@ -0,0 +1,6 @@
|
||||||
|
> 1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
100
|
||||||
|
200
|
||||||
|
300
|
||||||
23
regression/test038.expr
Normal file
23
regression/test038.expr
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
fun append (x, y) {
|
||||||
|
case x of
|
||||||
|
`nil -> return y
|
||||||
|
| `cons (h, t) -> return `cons (h, append (t, y))
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
fun printList (x) {
|
||||||
|
case x of
|
||||||
|
`nil -> skip
|
||||||
|
| `cons (h, t) -> write (h); printList (t)
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
n := read ();
|
||||||
|
|
||||||
|
x := `cons (1, `cons (2, `nil));
|
||||||
|
y := `cons (3, `cons (4, `nil));
|
||||||
|
|
||||||
|
printList (x);
|
||||||
|
printList (y);
|
||||||
|
printList (append (x, y));
|
||||||
|
printList (append (y, x))
|
||||||
31
regression/test039.expr
Normal file
31
regression/test039.expr
Normal file
|
|
@ -0,0 +1,31 @@
|
||||||
|
fun insert (t, x) {
|
||||||
|
case t of
|
||||||
|
`leaf -> return `node (x, `leaf, `leaf)
|
||||||
|
| `node (y, l, r) -> if x > y
|
||||||
|
then return `node (y, insert (l, x), r)
|
||||||
|
else return `node (y, l, insert (r, x))
|
||||||
|
fi
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
fun find (t, x) {
|
||||||
|
case t of
|
||||||
|
`leaf -> return 0
|
||||||
|
| `node (y, l, r) -> if x == y then return 1
|
||||||
|
elif x > y then return find (l, x)
|
||||||
|
else return find (r, x)
|
||||||
|
fi
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
n := read ();
|
||||||
|
|
||||||
|
t := insert (insert (insert (insert (`leaf, 5), 4), 6), 3);
|
||||||
|
|
||||||
|
write (find (t, 5));
|
||||||
|
write (find (t, 4));
|
||||||
|
write (find (t, 6));
|
||||||
|
write (find (t, 3));
|
||||||
|
write (find (t, 2));
|
||||||
|
write (find (t, 1))
|
||||||
|
|
||||||
15
regression/test040.expr
Normal file
15
regression/test040.expr
Normal file
|
|
@ -0,0 +1,15 @@
|
||||||
|
fun f (x) {
|
||||||
|
case x of
|
||||||
|
`a -> write (1)
|
||||||
|
| `b -> write (2)
|
||||||
|
| `c -> write (3)
|
||||||
|
| _ -> write (4)
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
x := read ();
|
||||||
|
|
||||||
|
f (`a);
|
||||||
|
f (`b);
|
||||||
|
f (`c);
|
||||||
|
f (`d)
|
||||||
1
regression/test040.input
Normal file
1
regression/test040.input
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
0
|
||||||
11
regression/test041.expr
Normal file
11
regression/test041.expr
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
fun f (a) {
|
||||||
|
case a of
|
||||||
|
`a (x, y, z) -> write (x + y + z)
|
||||||
|
| `b (x, y, z) -> write (x + y + z)
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
x := read ();
|
||||||
|
|
||||||
|
f (`a (100, 200, 300));
|
||||||
|
f (`b (500, 600, 700))
|
||||||
1
regression/test041.input
Normal file
1
regression/test041.input
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
0
|
||||||
17
regression/test042.expr
Normal file
17
regression/test042.expr
Normal file
|
|
@ -0,0 +1,17 @@
|
||||||
|
fun f (x) {
|
||||||
|
case x of
|
||||||
|
`nil -> write (0)
|
||||||
|
| `cons (_, `nil) -> write (1)
|
||||||
|
| `cons (_, `cons (_, `nil)) -> write (2)
|
||||||
|
| `cons (_, `cons (_, `cons (_, `nil))) -> write (3)
|
||||||
|
| _ -> write (4)
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
x := read ();
|
||||||
|
y := `nil;
|
||||||
|
|
||||||
|
for i := 0, i < 10, i := i + 1 do
|
||||||
|
f (y);
|
||||||
|
y := `cons (i, y)
|
||||||
|
od
|
||||||
1
regression/test042.input
Normal file
1
regression/test042.input
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
0
|
||||||
12
regression/test043.expr
Normal file
12
regression/test043.expr
Normal file
|
|
@ -0,0 +1,12 @@
|
||||||
|
fun sum (x) {
|
||||||
|
case x of
|
||||||
|
`nil -> return 0
|
||||||
|
| `cons (x, tl) -> return x + sum (tl)
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
x := read ();
|
||||||
|
|
||||||
|
write (sum (`nil));
|
||||||
|
write (sum (`cons (100, `nil)));
|
||||||
|
write (sum (`cons (100, `cons (200, `nil))))
|
||||||
1
regression/test043.input
Normal file
1
regression/test043.input
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
0
|
||||||
37
regression/test044.expr
Normal file
37
regression/test044.expr
Normal file
|
|
@ -0,0 +1,37 @@
|
||||||
|
fun zip (x) {
|
||||||
|
case x of `pair (x, y) ->
|
||||||
|
case x of
|
||||||
|
`nil -> return `nil
|
||||||
|
| `cons (x, xs) -> case y of
|
||||||
|
`nil -> return `nil
|
||||||
|
| `cons (y, ys) -> return `cons (`pair (x, y), zip (`pair (xs, ys)))
|
||||||
|
esac
|
||||||
|
esac
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
fun unzip (x) {
|
||||||
|
case x of
|
||||||
|
`nil -> return `pair (`nil, `nil)
|
||||||
|
| `cons (`pair (x, y), tl) ->
|
||||||
|
case unzip (tl) of
|
||||||
|
`pair (xs, ys) -> return `pair (`cons (x, xs), `cons (y, ys))
|
||||||
|
esac
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
fun printList (l) {
|
||||||
|
case l of
|
||||||
|
`nil -> skip
|
||||||
|
| `cons (x, xs) -> write (x); printList (xs)
|
||||||
|
esac
|
||||||
|
}
|
||||||
|
|
||||||
|
z := read ();
|
||||||
|
|
||||||
|
x := `cons (1, `cons (2, `cons (3, `nil)));
|
||||||
|
y := `cons (100, `cons (200, `cons (300, `nil)));
|
||||||
|
|
||||||
|
case unzip (zip (`pair (x, y))) of
|
||||||
|
`pair (x, y) -> printList (x); printList (y)
|
||||||
|
esac
|
||||||
1
regression/test044.input
Normal file
1
regression/test044.input
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
0
|
||||||
|
|
@ -94,7 +94,7 @@ open SM
|
||||||
of x86 instructions
|
of x86 instructions
|
||||||
*)
|
*)
|
||||||
let compile env code =
|
let compile env code =
|
||||||
SM.print_prg code;
|
(*SM.print_prg code;*)
|
||||||
flush stdout;
|
flush stdout;
|
||||||
let suffix = function
|
let suffix = function
|
||||||
| "<" -> "l"
|
| "<" -> "l"
|
||||||
|
|
@ -351,10 +351,10 @@ class env =
|
||||||
method drop_barrier = {< barrier = false >}
|
method drop_barrier = {< barrier = false >}
|
||||||
|
|
||||||
(* associates a stack to a label *)
|
(* associates a stack to a label *)
|
||||||
method set_stack l = Printf.printf "Setting stack for %s\n" l; {< stackmap = M.add l stack stackmap >}
|
method set_stack l = (*Printf.printf "Setting stack for %s\n" l;*) {< stackmap = M.add l stack stackmap >}
|
||||||
|
|
||||||
(* retrieves a stack for a label *)
|
(* retrieves a stack for a label *)
|
||||||
method retrieve_stack l = Printf.printf "Retrieving stack for %s\n" l;
|
method retrieve_stack l = (*Printf.printf "Retrieving stack for %s\n" l;*)
|
||||||
try {< stack = M.find l stackmap >} with Not_found -> self
|
try {< stack = M.find l stackmap >} with Not_found -> self
|
||||||
|
|
||||||
(* gets a name for a global variable *)
|
(* gets a name for a global variable *)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue