mirror of
https://github.com/ProgramSnail/Lama.git
synced 2025-12-09 16:28:47 +00:00
x86 for the linear language
This commit is contained in:
parent
4ff1bf9858
commit
64172f66d3
9 changed files with 57 additions and 18 deletions
2
Makefile
2
Makefile
|
|
@ -4,6 +4,7 @@ SHELL := /bin/bash
|
|||
|
||||
all:
|
||||
pushd src && make && popd
|
||||
pushd runtime && make && popd
|
||||
|
||||
install: ;
|
||||
|
||||
|
|
@ -12,5 +13,6 @@ regression:
|
|||
|
||||
clean:
|
||||
pushd src && make clean && popd
|
||||
pushd runtime && make clean && popd
|
||||
pushd regression && make clean && popd
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ RC=../src/rc.opt
|
|||
check: $(TESTS)
|
||||
|
||||
$(TESTS): %: %.expr
|
||||
$(RC) $< && cat $@.input | ./$@ > $@.log && diff $@.log orig/$@.log
|
||||
cat $@.input | $(RC) -i $< > $@.log && diff $@.log orig/$@.log
|
||||
cat $@.input | $(RC) -s $< > $@.log && diff $@.log orig/$@.log
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ RC = ../../src/rc.opt
|
|||
check: $(TESTS)
|
||||
|
||||
$(TESTS): %: %.expr
|
||||
RC_RUNTIME=../../runtime $(RC) $< && cat $@.input | ./$@ > $@.log && diff $@.log orig/$@.log
|
||||
cat $@.input | $(RC) -i $< > $@.log && diff $@.log orig/$@.log
|
||||
cat $@.input | $(RC) -s $< > $@.log && diff $@.log orig/$@.log
|
||||
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ RC = ../../src/rc.opt
|
|||
check: $(TESTS)
|
||||
|
||||
$(TESTS): %: %.expr
|
||||
RC_RUNTIME=../../runtime $(RC) $< && cat $@.input | ./$@ > $@.log && diff $@.log orig/$@.log
|
||||
cat $@.input | $(RC) -i $< > $@.log && diff $@.log orig/$@.log
|
||||
cat $@.input | $(RC) -s $< > $@.log && diff $@.log orig/$@.log
|
||||
|
||||
|
|
|
|||
6
runtime/Makefile
Normal file
6
runtime/Makefile
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
all:
|
||||
gcc -m32 -c runtime.c
|
||||
|
||||
clean:
|
||||
rm -f runtime.o *~
|
||||
|
||||
22
runtime/runtime.c
Normal file
22
runtime/runtime.c
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
/* Runtime library */
|
||||
|
||||
# include <stdio.h>
|
||||
|
||||
/* Lread is an implementation of the "read" construct */
|
||||
extern int Lread () {
|
||||
int result;
|
||||
|
||||
printf ("> ");
|
||||
fflush (stdout);
|
||||
scanf ("%d", &result);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/* Lwrite is an implementation of the "write" construct */
|
||||
extern int Lwrite (int n) {
|
||||
printf ("%d\n", n);
|
||||
fflush (stdout);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -19,23 +19,30 @@ let parse infile =
|
|||
let main =
|
||||
try
|
||||
let interpret = Sys.argv.(1) = "-i" in
|
||||
let infile = Sys.argv.(2) in
|
||||
let stack = Sys.argv.(1) = "-s" in
|
||||
let to_compile = not (interpret || stack) in
|
||||
let infile = Sys.argv.(if not to_compile then 2 else 1) in
|
||||
match parse infile with
|
||||
| `Ok prog ->
|
||||
let rec read acc =
|
||||
try
|
||||
let r = read_int () in
|
||||
Printf.printf "> ";
|
||||
read (acc @ [r])
|
||||
with End_of_file -> acc
|
||||
in
|
||||
let input = read [] in
|
||||
let output =
|
||||
if interpret
|
||||
then Language.eval prog input
|
||||
else SM.run (SM.compile prog) input
|
||||
in
|
||||
List.iter (fun i -> Printf.printf "%d\n" i) output
|
||||
if to_compile
|
||||
then
|
||||
let basename = Filename.chop_suffix infile ".expr" in
|
||||
ignore @@ X86.build prog basename
|
||||
else
|
||||
let rec read acc =
|
||||
try
|
||||
let r = read_int () in
|
||||
Printf.printf "> ";
|
||||
read (acc @ [r])
|
||||
with End_of_file -> acc
|
||||
in
|
||||
let input = read [] in
|
||||
let output =
|
||||
if interpret
|
||||
then Language.eval prog input
|
||||
else SM.run (SM.compile prog) input
|
||||
in
|
||||
List.iter (fun i -> Printf.printf "%d\n" i) output
|
||||
| `Fail er -> Printf.eprintf "Syntax error: %s\n" er
|
||||
with Invalid_argument _ ->
|
||||
Printf.printf "Usage: rc [-i] <input file.expr>\n"
|
||||
Printf.printf "Usage: rc [-i | -s] <input file.expr>\n"
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@ TOPFILE = rc
|
|||
OCAMLC = ocamlc
|
||||
OCAMLOPT = ocamlopt
|
||||
OCAMLDEP = ocamldep
|
||||
SOURCES = Language.ml SM.ml Driver.ml
|
||||
SOURCES = Language.ml SM.ml X86.ml Driver.ml
|
||||
LIBS = GT.cma unix.cma re.cma re_emacs.cma re_str.cma
|
||||
CAMLP5 = -pp "camlp5o -I `ocamlfind -query GT.syntax` -I `ocamlfind -query ostap.syntax` pa_ostap.cmo pa_gt.cmo -L `ocamlfind -query GT.syntax`"
|
||||
PXFLAGS = $(CAMLP5)
|
||||
|
|
|
|||
|
|
@ -7,7 +7,6 @@ open Language
|
|||
(* put a constant on the stack *) | CONST of int
|
||||
(* read to stack *) | READ
|
||||
(* write from stack *) | WRITE
|
||||
(* put a constant of the stack *) | CONST of int
|
||||
(* load a variable to the stack *) | LD of string
|
||||
(* store a variable from the stack *) | ST of string with show
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue