Buildtins, arrays, string (no X86 yet), tests

This commit is contained in:
Dmitry Boulytchev 2018-04-27 01:27:10 +03:00
parent 25eff5c282
commit 39508a0195
10 changed files with 87 additions and 25 deletions

View file

@ -0,0 +1,8 @@
> 10
20
30
40
0
1
2
3

View file

@ -0,0 +1,16 @@
> 97
98
99
100
101
102
103
104
97
97
97
97
97
97
97
97

View file

@ -14,6 +14,4 @@ for i:=0, i<x.length, i:=i+1 do
x[i] := x[i]+2
od;
printString (x);
printString (strcat ("abc", "def"))
printString (x)

21
regression/test035.expr Normal file
View file

@ -0,0 +1,21 @@
fun printArray (x) local elem {
if x.length == 0 then return fi;
for i:=0, i<x.length, i:=i+1 do
write (x[i])
od
}
n := read ();
x := [10, 20, 30, 40];
printArray (x);
for i:=0, i<x.length, i:=i+1 do
x[i] := i
od;
printArray (x)

1
regression/test035.input Normal file
View file

@ -0,0 +1 @@
0

21
regression/test036.expr Normal file
View file

@ -0,0 +1,21 @@
fun printAS (x) local i, j {
for i := 0, i<x.length, i:=i+1 do
for j := 0, j<x[i].length, j:=j+1 do
write (x[i][j])
od
od
}
y := read ();
x := ["abcd", "efgh"];
printAS (x);
for i := 0, i<x.length, i:=i+1 do
for j := 0, j<x[i].length, j:=j+1 do
x[i][j] := 'a'
od
od;
printAS (x)

1
regression/test036.input Normal file
View file

@ -0,0 +1 @@
0

View file

@ -80,15 +80,12 @@ module Builtin =
Some (match b with
| Value.String s -> Value.of_int @@ Char.code s.[i]
| Value.Array a -> List.nth a i
)
)
| "$length" -> (st, i, o, Some (Value.of_int (match List.hd args with Value.Array a -> List.length a | Value.String s -> String.length s)))
| "$array" -> (st, i, o, Some (Value.of_array args))
| "strcat" -> let [x; y] = args in
(st, i, o, Some (Value.of_string @@ Value.to_string x ^ Value.to_string y))
| "isArray" -> let [a] = args in
(st, i, o, Some (Value.of_int @@ match a with Array _ -> 1 | _ -> 0))
| "isArray" -> let [a] = args in (st, i, o, Some (Value.of_int @@ match a with Value.Array _ -> 1 | _ -> 0))
| "isString" -> let [a] = args in (st, i, o, Some (Value.of_int @@ match a with Value.String _ -> 1 | _ -> 0))
end

View file

@ -18,7 +18,6 @@ open Language
(* returns from a function *) | RET of bool with show
(* The type for the stack machine program *)
type prg = insn list
let print_prg p = List.iter (fun i -> Printf.printf "%s\n" (show(insn) i)) p
@ -60,7 +59,7 @@ let rec eval env ((cstack, stack, ((st, i, o) as c)) as conf) = function
then eval env ((prg', st)::cstack, stack, c) (env#labeled f)
else eval env (env#builtin conf f n p) prg'
| BEGIN (_, args, locals) -> let vs, stack' = split (List.length args) stack in
let state = List.combine args vs in
let state = List.combine args @@ List.rev vs in
eval env (cstack, stack', (List.fold_left (fun s (x, v) -> State.update x v s) (State.enter st (args @ locals)) state, i, o)) prg'
| END | RET _ -> (match cstack with
| (prg', st')::cstack' -> eval env (cstack', stack, (State.leave st st', i, o)) prg'
@ -91,7 +90,7 @@ let run p i =
method builtin (cstack, stack, (st, i, o)) f n p =
let f = match f.[0] with 'L' -> String.sub f 1 (String.length f - 1) | _ -> f in
let args, stack' = split n stack in
let (st, i, o, r) = Language.Builtin.eval (st, i, o, None) args f in
let (st, i, o, r) = Language.Builtin.eval (st, i, o, None) (List.rev args) f in
let stack'' = if p then stack' else let Some r = r in r::stack' in
Printf.printf "Builtin: %s\n";
(cstack, stack'', (st, i, o))
@ -112,7 +111,7 @@ let run p i =
let compile (defs, p) =
let label s = "L" ^ s in
let rec call f args p =
let args_code = List.concat @@ List.map expr (List.rev args) in
let args_code = List.concat @@ List.map expr args in
args_code @ [CALL (label f, List.length args, p)]
and expr = function
| Expr.Var x -> [LD x]
@ -121,7 +120,7 @@ let compile (defs, p) =
| Expr.Binop (op, x, y) -> expr x @ expr y @ [BINOP op]
| Expr.Call (f, args) -> call f args false
| Expr.Array xs -> List.flatten (List.map expr xs) @ [CALL ("$array", List.length xs, false)]
| Expr.Elem (a, i) -> expr i @ expr a @ [CALL ("$elem", 2, false)]
| Expr.Elem (a, i) -> expr a @ expr i @ [CALL ("$elem", 2, false)]
| Expr.Length e -> expr e @ [CALL ("$length", 1, false)]
in
let rec compile_stmt l env = function

View file

@ -220,7 +220,7 @@ let compile env code =
push_args env ((Push x)::acc) (n-1)
in
let env, pushs = push_args env [] n in
env, pushr @ pushs @ [Call f; Binop ("+", L (n*4), esp)] @ (List.rev popr)
env, pushr @ (List.rev pushs) @ [Call f; Binop ("+", L (n*4), esp)] @ (List.rev popr)
in
(if p then env, code else let y, env = env#allocate in env, code @ [Mov (eax, y)])
in