012820 16:24:35
1: Id: absyn.mli,v 1.4 20200128 16:24:3508
2:
3:
4: Abstract syntax definitions for SB.
5:
6:
7: type linenr
8: type ident
9: type label
10: type number
11: type oper
12:
13: type memref
14:
int
string
string
float
string
Arrayref of ident expr
Variable of ident
15:
16: and expr
17:
18:
19:
20:
21: type printable Printexpr of expr
Number of number
Memref of memref
Unary of oper expr
Binary of oper expr expr
22:
23:
24: type stmt
25:
26:
27:
28:
29:
30:
31: type progline linenr label option stmt option
32:
33: type program progline list
34:
String of string
Dim of ident expr
Let of memref expr
Goto of label
If of expr label
Print of printable list
Input of memref list
11
cse112wmAssignmentsasg2ocamlinterpcodebinary absyn.mli
012820 16:24:35
11
1: Id: etc.mli,v 1.1 20190124 15:47:3808
2:
3:
4: Main program and system access.
5:
6:
7: val warn : string list unit
8:
9: val die : string list unit
10:
11: val syntaxerror : Lexing.position string list unit
12:
13: val usageexit : string list unit
14:
15: val readnumber : unit float
16:
cse112wmAssignmentsasg2ocamlinterpcodebinary etc.mli
012820 16:24:35
13:
14:
15:
16:
17:
18:
19: let warn message eprintlist execname :: : :: message
20:
21: let die message warn message; quit
22:
23: let syntaxerror position message
exitstatusref : 1;
flushall ;
List.iter prerrstring message;
prerrnewline ;
flushall
24:
25:
26:
27:
28: let usageexit message
29: eprintlist Usage: :: execname :: :: message; quit
30:
warn position.Lexing.posfname :: :
:: stringofint position.Lexing.poslnum :: :
:: message
31: let buffer : string list ref ref
32:
33: let rec readnumber match !buffer with
34:
35:
36:
37:
38:
39:
40:
head::tail buffer : tail;
try floatofstring head
with Failure nan
let line inputline stdin
in buffer : Str.split Str.regexp t line;
readnumber
11
1: Id: etc.ml,v 1.3 20200122 16:07:2408
2:
3: let execname Filename.basename Sys.argv.0
4:
5: let exitstatusref : int ref ref 0
6:
7: let quit
8: if !Sys.interactive
9: then Printf.printf quit : exit dn! !exitstatusref
10: else exit !exitstatusref
11:
12: let eprintlist message
cse112wmAssignmentsasg2ocamlinterpcodebinary etc.ml
012820 16:24:35
11
1: Generated: Tue Jan 28 16:24:35 PST 2020
2: type variabletablet string, float Hashtbl.t
3: type arraytablet string, float array Hashtbl.t
4: type unaryfntablet string, float float Hashtbl.t
5: type binaryfntablet string, float float float Hashtbl.t
6: type labeltablet string, Absyn.program Hashtbl.t
7: val variabletable : variabletablet
8: val arraytable : arraytablet
9: val unaryfntable : unaryfntablet
10: val binaryfntable : binaryfntablet
11: val labeltable : labeltablet
12: val initlabeltable : Absyn.program unit
13: val dumplabeltable : unit unit
cse112wmAssignmentsasg2ocamlinterpcodebinary tables.mli
012820 16:24:35
12
1: Id: tables.ml,v 1.6 20200127 18:45:2808
2:
3: type variabletablet string, float Hashtbl.t
4: type arraytablet string, float array Hashtbl.t
5: type unaryfntablet string, float float Hashtbl.t
6: type binaryfntablet string, float float float Hashtbl.t
7: type labeltablet string, Absyn.program Hashtbl.t
8:
9: let variabletable : variabletablet Hashtbl.create 16
10: let List.iter fun label, value
11:
12:
13:
14:
15:
16:
17: let arraytable : arraytablet Hashtbl.create 16
18:
19: let unaryfntable : unaryfntablet Hashtbl.create 16
20: let List.iter fun label, value
21: Hashtbl.add unaryfntable label value
22: , .;
23: , .;
24: abs , absfloat;
25: acos , acos;
26: asin , asin;
27: atan , atan;
28: ceil , ceil;
29: cos , cos;
30: exp , exp;
31: floor, floor;
32: log , log;
33: log10, log10;
34: log2 , fun x log x . log 2.0;
35: round, Float.round;
36: sin , sin;
37: sqrt , sqrt;
38: tan , tan
39:
40: let binaryfntable : binaryfntablet Hashtbl.create 16
41: let List.iter fun label, value
42: Hashtbl.add binaryfntable label value
43: , .;
44: , .;
45: , .;
46: , .;
47: , modfloat;
48: ,
49:
Hashtbl.add variabletable label value
e , exp 1.0;
eof, 0.0;
pi , acos .1.0;
nan, nan
cse112wmAssignmentsasg2ocamlinterpcodebinary tables.ml
012820 16:24:35
22
50:
51: let labeltable : labeltablet Hashtbl.create 16
52:
53: let rec initlabeltable program
54: let rec init program match program with
55:
56: , Some label, ::rest
57: Hashtbl.add labeltable label program; init rest
58: ::rest init rest
59: in Hashtbl.reset labeltable; init program
60:
61: let dumplabeltable
62:
63:
64:
65:
66:
67:
68:
let dump key value match value with
line, , ::
Printf.fprintf stderr
labeltable: s line dn! key line
in Hashtbl.iter dump labeltable
cse112wmAssignmentsasg2ocamlinterpcodebinary tables.ml
012820 16:24:36
11
1: Generated: Tue Jan 28 16:24:36 PST 2020
2: val quote : string string
3: val join : string string string string list string
4: val stringofoption : a string a option string
5: val stringofctor : string string list string
6: val stringoflist : a string a list string
7: val stringofprintable : Absyn.printable string
8: val stringofmemref : Absyn.memref string
9: val stringofexpr : Absyn.expr string
10: val stringofstmt : Absyn.stmt string
11: val dumpprogline : int string option Absyn.stmt option unit
12: val dumpprogram : Absyn.program unit
cse112wmAssignmentsasg2ocamlinterpcodebinary dumper.mli
012820 16:24:36
12
1: Id: dumper.ml,v 1.17 20200127 14:28:0708
2:
3: open Printf;;
4:
5: let quote string
6: let regex Str.regexp
7: and subst
8: in Str.globalsubstitute regex subst string ;;
9:
10: let join start sep stop list
11:
12:
13:
14:
15:
16:
17:
18:
19: let stringofoption strfn item match item with
20: None None
21: Some thing Some strfn thing ;;
22:
23: let stringofctor ctor args
24: join ctor , args;;
25:
26: let stringoflist strfn list
27: join ; List.map strfn list;;
28:
29: let rec stringofprintable printable match printable with
let rec join list match list with
stop
unit unit stop
head::tail head sep join tail
in match list with
start stop
:: start join list;;
30:
31:
32:
33:
34:
35: and stringofmemref memref match memref with
Absyn.Printexpr expr
stringofctor Printexpr stringofexpr expr
Absyn.String string
stringofctor String quote string
Absyn.Arrayref ident, expr
stringofctor Arrayref quote ident; stringofexpr expr
Absyn.Variable ident stringofctor Variable quote ident
36:
37:
38:
39:
40: and stringofexpr expr match expr with
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
Absyn.Number number
stringofctor Number stringoffloat number
Absyn.Memref memref
stringofctor Memref stringofmemref memref
Absyn.Unary oper, expr
stringofctor Unary quote oper; stringofexpr expr
Absyn.Binary oper, expr1, expr2
stringofctor Binary
quote oper; stringofexpr expr1; stringofexpr expr2;;
cse112wmAssignmentsasg2ocamlinterpcodebinary dumper.ml
012820 16:24:36
22
51:
52: let stringofstmt stmt: Absyn.stmt match stmt with
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70: let dumpprogline linenr, labeloption, stmtoption
Absyn.Dim ident, expr
stringofctor Dim
quote ident , stringofexpr expr
Absyn.Let memref, expr
stringofctor Let
stringofmemref memref; stringofexpr expr
Absyn.Goto label
stringofctor Goto quote label
Absyn.If expr, label
stringofctor If stringofexpr expr; quote label
Absyn.Print printablelist
stringofctor Print
stringoflist stringofprintable printablelist
Absyn.Input memreflist
stringofctor Input
stringoflist stringofmemref memreflist;;
71:
72:
73:
74:
75: let dumpprogram program : Absyn.program
76: List.iter dumpprogline program;;
77:
Printf.fprintf stderr program: d s: sn! linenr
stringofoption quote labeloption
stringofoption stringofstmt stmtoption;;
cse112wmAssignmentsasg2ocamlinterpcodebinary dumper.ml
012820 16:24:36
11
1: Id: interp.mli,v 1.7 20200124 12:57:0608
2:
3:
4: Interpreter for Silly Basic
5:
6:
7: val wantdump : bool ref
8:
9: val interpretprogram : Absyn.program unit
10:
cse112wmAssignmentsasg2ocamlinterpcodebinary interp.mli
012820 16:24:36
12
1: Id: interp.ml,v 1.9 20200128 13:33:0008
2:
3: open Absyn
4:
5: exception Unimplemented of string
6: let noexpr reason raise Unimplemented reason
7: let nostmt reason continuation raise Unimplemented reason
8:
9: let wantdump ref false
10:
11: let rec evalexpr expr : Absyn.expr : float match expr with
12: Number number number
13: Memref memref noexpr evalexpr Memref
14: Unary oper, expr noexpr evalexpr Unary
15: Binary oper, expr1, expr2 noexpr evalexpr Binary
16:
17: let rec interpret program : Absyn.program match program with
18:
19: firstline::continuation match firstline with
20: , , None interpret continuation
21: , , Some stmt interpstmt stmt continuation
22:
23: and interpstmt stmt : Absyn.stmt continuation : Absyn.program
24: match stmt with
25: Dim ident, expr nostmt Dim ident, expr continuation
26: Let memref, expr nostmt Let memref, expr continuation
27: Goto label nostmt Goto label continuation
28: If expr, label nostmt If expr, label continuation
29: Print printlist interpprint printlist continuation
30: Input memreflist interpinput memreflist continuation
31:
32: and interpprint printlist : Absyn.printable list
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
continuation : Absyn.program
let printitem item
printstring ;
match item with
String string
let regex Str.regexp .
in printstring Str.replacefirst regex 1 string
Printexpr expr
printfloat evalexpr expr
in List.iter printitem printlist; printnewline ;
interpret continuation
cse112wmAssignmentsasg2ocamlinterpcodebinary interp.ml
012820 16:24:36
22
45:
46: and interpinput memreflist : Absyn.memref list
continuation : Absyn.program
let inputnumber memref
try let number Etc.readnumber
in printfloat number; printnewline
with Endoffile
printstring Endoffile; printnewline
47:
48:
49:
50:
51:
52:
53:
54:
55:
56: let interpretprogram program
57:
58:
59:
60:
61:
Tables.initlabeltable program;
if !wantdump then Tables.dumplabeltable ;
if !wantdump then Dumper.dumpprogram program;
interpret program
in List.iter inputnumber memreflist;
interpret continuation
cse112wmAssignmentsasg2ocamlinterpcodebinary interp.ml
012820 16:24:36
11
1: Id: main.ml,v 1.3 20200124 12:57:0608
2:
3:
4: Main program reads a file and prints to stdout.
5:
6:
7: let interpretsource filename
8:
9:
10:
11:
12:
13:
14:
15:
16:
try let sourcefile
if filename
then stdin
else openin filename in
let lexbuf Lexing.fromchannel sourcefile in
let abstractsyntax Parser.program Scanner.token lexbuf in
Interp.interpretprogram abstractsyntax
with Syserror string Etc.die string
17: let if !Sys.interactive
18: then
19: else match Array.length Sys.argv with
20: 1 interpretsource
21: 2 interpretsource Sys.argv.1
22: Etc.usageexit filename.sb
23:
cse112wmAssignmentsasg2ocamlinterpcodebinary main.ml
012820 16:24:36
stmtlist error EOL
: label action
action
label
LET memref EQUAL expr
GOTO IDENT
IF relexpr GOTO IDENT
PRINT printlist
Absyn.Let 2, 4
Absyn.Goto 2
Absyn.If 2, 4
Absyn.Print 2
Absyn.Print
Absyn.Input 2
1::3
1
39:
40:
41:
42:
43: printlist : print COMMA printlist
PRINT
INPUT inputlist
44:
45:
46: print
47:
48:
49: inputlist : memref COMMA inputlist 1::3
50: memref 1
51:
print
: expr
STRING
Absyn.Printexpr 1
Absyn.String 1
1
12
1: Id: parser.mly,v 1.3 20200128 13:31:1108
2:
3:
4:
5: let linenr symbolstartpos .Lexing.poslnum
6:
7: let syntax Etc.syntaxerror symbolstartpos syntax error
8:
9:
10:
11: token string RELOP EQUAL ADDOP MULOP POWOP
12: token string IDENT NUMBER STRING
13: token COLON COMMA LPAR RPAR LSUB RSUB EOL EOF
14: token DIM LET GOTO IF PRINT INPUT
15:
16: type Absyn.program program
17:
18: start program
19:
20:
21:
22: program : stmtlist EOF
23:
24: stmtlist : stmtlist stmt EOL
25:
26:
27:
28: stmt
29:
30:
31:
32:
33: label
34:
35: action
36:
37:
38:
List.rev 1
2::1
syntax; 1
linenr, Some 1, Some 2
linenr, None, Some 1
linenr, Some 1, None
linenr, None, None
: IDENT COLON
: DIM IDENT LSUB expr RSUB Absyn.Dim 2, 4
cse112wmAssignmentsasg2ocamlinterpcodebinary parser.mly
012820 16:24:36
22
73:
52:
53: memref
54:
55:
56: relexpr
57:
58:
59: expr
60:
61:
62: term
63:
64:
65: factor
66:
67:
68: primary
69:
70:
: IDENT
IDENT LSUB expr RSUB
: expr RELOP expr
expr EQUAL expr
: expr ADDOP term
term
: term MULOP factor
factor
: primary POWOP factor
primary
: LPAR expr RPAR
ADDOP primary
NUMBER
memref
IDENT LPAR expr RPAR
Absyn.Variable 1
Absyn.Arrayref 1, 3
Absyn.Binary 2, 1, 3
Absyn.Binary 2, 1, 3
Absyn.Binary 2, 1, 3
1
Absyn.Binary 2, 1, 3
1
Absyn.Binary 2, 1, 3
1
2
Absyn.Unary 1, 2
Absyn.Number floatofstring 1
Absyn.Memref 1
Absyn.Unary 1, 3
71: 72:
cse112wmAssignmentsasg2ocamlinterpcodebinary parser.mly
012820 16:24:36
12
1: Id: scanner.mll,v 1.2 20191126 14:09:5108
2:
3:
4:
5: let lexerror lexbuf
6: Etc.syntaxerror Lexing.lexemestartp lexbuf
7: invalid character Lexing.lexeme lexbuf
8:
9: let newline lexbuf
let incr pos
pos with Lexing.poslnum pos.Lexing.poslnum 1;
Lexing.posbol pos.Lexing.poscnum
in lexbuf.Lexing.lexstartp incr lexbuf.Lexing.lexstartp;
10:
11:
12:
13:
14:
15:
16: let lexeme Lexing.lexeme
17:
18:
19:
20: let letter
21: let digit
22: let fraction
23: let exponent
24:
25: let comment
26: let ident
27: let number
28: let string
29:
az AZ
09
digit .? digit . digit
E e ? digit
n
letter letter digit
fraction exponent?
n
lexbuf.Lexing.lexcurrp incr lexbuf.Lexing.lexcurrp
cse112wmAssignmentsasg2ocamlinterpcodebinary scanner.mll
012820 16:24:36
22
30:
31: rule token parse
32: eof
33: t
34: comment
35: n
36: :
37: ,
38:
39:
40:
41:
42:
43: !
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54: dim
55: goto
56: if
57: input
58: let
59: print
60: number
61: string
62: ident
63:
64:
Parser.EOF
token lexbuf
token lexbuf
newline lexbuf; Parser.EOL
Parser.COLON
Parser.COMMA
Parser.LPAR
Parser.RPAR
Parser.LSUB
Parser.RSUB
Parser.EQUAL lexeme lexbuf
Parser.RELOP lexeme lexbuf
Parser.RELOP lexeme lexbuf
Parser.RELOP lexeme lexbuf
Parser.RELOP lexeme lexbuf
Parser.RELOP lexeme lexbuf
Parser.ADDOP lexeme lexbuf
Parser.ADDOP lexeme lexbuf
Parser.MULOP lexeme lexbuf
Parser.MULOP lexeme lexbuf
Parser.MULOP lexeme lexbuf
Parser.POWOP lexeme lexbuf
Parser.DIM
Parser.GOTO
Parser.IF
Parser.INPUT
Parser.LET
Parser.PRINT
Parser.NUMBER lexeme lexbuf
Parser.STRING lexeme lexbuf
Parser.IDENT lexeme lexbuf
lexerror lexbuf; token lexbuf
cse112wmAssignmentsasg2ocamlinterpcodebinary scanner.mll
012820 16:24:36
1: Id: Makefile,v 1.16 20200128 16:12:5008
2:
3:
4: General useful macros
5:
6:
7: MKFILE Makefile
8: MAKEFLAGS nobuiltinrules
9: DEPSFILE MKFILE.deps
10: NOINCLUDE ci clean spotless
11: NEEDINCL
12: GMAKE
13: OCAMLOPT
14:
filter NOINCLUDE, MAKECMDGOALS
MAKE noprintdirectory
ocamlopt g
15:
16: File macros
17:
18:
19: EXECBIN
20: OBJCMX
21:
22: OBJCMI
23: OBJBIN
24: MLSOURCE
25:
26: GENLEXYACC parser.mli parser.ml scanner.ml
27: GENSOURCE dumper.mli tables.mli GENLEXYACC
28: GENFILES GENSOURCE parser.output DEPSFILE
29: OTHERFILES MKFILE DEPSFILE using .ocamlinit
30: ALLSOURCES MLSOURCE parser.mly scanner.mll OTHERFILES
31: LISTING Listing.ps
32:
33:
34: General targets
35:
36:
37: all : EXECBIN
38:
39: EXECBIN : OBJCMX OBJBIN
40: OCAMLOPT str.cmxa OBJCMX o EXECBIN
41:
42: .cmi : .mli
43: OCAMLOPT c
44:
45: .o .cmx : .ml
46: OCAMLOPT c
47:
48: .ml : .mll
49: ocamllex
50:
51: .mli .ml : .mly
52: ocamlyacc v
53:
sbinterp
etc.cmx parser.cmx scanner.cmx tables.cmx
dumper.cmx interp.cmx main.cmx
OBJCMX:.cmx.cmi absyn.cmi
OBJCMX:.cmx.o
absyn.mli etc.mli etc.ml tables.mli tables.ml
dumper.mli dumper.ml interp.mli interp.ml main.ml
12
cse112wmAssignmentsasg2ocamlinterpcodebinary Makefile
012820 16:24:36
22
54:
55: MAKEMLI echo Generated: date ; OCAMLOPT i
56:
57: tables.mli : tables.ml absyn.cmi
58: call MAKEMLI
59:
60: dumper.mli : dumper.ml absyn.cmi
61: call MAKEMLI
62:
63:
64: Misc targets
65:
66:
67: clean :
68: rm OBJCMI OBJCMX OBJBIN GENSOURCE
69:
70: spotless : clean
71: rm EXECBIN GENFILES LISTING LISTING:.ps.pdf
72:
73: ci : ALLSOURCES
74: checksource ALLSOURCES
75: cid ALLSOURCES
76:
77: deps : MLSOURCE GENSOURCE
78: echo Generated: date DEPSFILE
79: ocamldep MLSOURCE GENSOURCE DEPSFILE
80:
81: DEPSFILE : tables.mli
82: touch DEPSFILE
83: GMAKE deps
84:
85: lis : ALLSOURCES
86: mkpspdf LISTING ALLSOURCES
87:
88: again :
89: GMAKE spotless
90: GMAKE deps
91: GMAKE ci
92: GMAKE all
93: GMAKE lis
94:
95: ifeq NEEDINCL
96: include DEPSFILE
97: endif
98:
cse112wmAssignmentsasg2ocamlinterpcodebinary Makefile
012820 16:24:35
12
1: Generated: Tue Jan 28 16:24:35 PST 2020
2: absyn.cmi :
3: dumper.cmo :
4: absyn.cmi
5: dumper.cmi
6: dumper.cmx :
7: absyn.cmi
8: dumper.cmi
9: dumper.cmi :
10: absyn.cmi
11: dumper.cmi :
12: absyn.cmi
13: etc.cmo :
14: etc.cmi
15: etc.cmx :
16: etc.cmi
17: etc.cmi :
18: interp.cmo :
19: tables.cmi
20: etc.cmi
21: dumper.cmi
22: absyn.cmi
23: interp.cmi
24: interp.cmx :
25: tables.cmx
26: etc.cmx
27: dumper.cmx
28: absyn.cmi
29: interp.cmi
30: interp.cmi :
31: absyn.cmi
32: main.cmo :
33: scanner.cmo
34: parser.cmi
35: interp.cmi
36: etc.cmi
37: main.cmx :
38: scanner.cmx
39: parser.cmx
40: interp.cmx
41: etc.cmx
42: parser.cmo :
43: etc.cmi
44: absyn.cmi
45: parser.cmi
46: parser.cmx :
47: etc.cmx
48: absyn.cmi
49: parser.cmi
50: parser.cmi :
51: absyn.cmi
52: scanner.cmo :
53: parser.cmi
54: etc.cmi
55: scanner.cmx :
56: parser.cmx
57: etc.cmx
58: tables.cmo :
cse112wmAssignmentsasg2ocamlinterpcodebinary Makefile.deps
012820 16:24:35
22
59: absyn.cmi
60: tables.cmi
61: tables.cmx :
62: absyn.cmi
63: tables.cmi
64: tables.cmi :
65: absyn.cmi
66: tables.cmi :
67: absyn.cmi
cse112wmAssignmentsasg2ocamlinterpcodebinary Makefile.deps
012820 16:24:36
11
1: let rcs Id: using,v 1.3 20190124 17:15:0708 ;;
2:
3: printendline rcs;;
4:
5: load str.cma;;
6:
7: moduse absyn.mli;;
8: moduse etc.ml;;
9:
10: moduse parser.ml;;
11: moduse scanner.ml;;
12:
13: moduse tables.ml;;
14: moduse dumper.ml;;
15:
16: moduse interp.ml;;
17: moduse main.ml;;
18:
19: open Interp;;
20: open Main;;
21:
22: wantdump : true;;
23:
cse112wmAssignmentsasg2ocamlinterpcodebinary using
012820 16:24:36
11
1: let rcs Id: .ocamlinit,v 1.6 20190124 18:40:2608 ;;
2:
3: printendline rcs;;
4:
5: load str.cma;;
6:
7: moduse absyn.mli;;
8: moduse etc.ml;;
9:
10: moduse parser.ml;;
11: moduse scanner.ml;;
12:
13: moduse tables.ml;;
14: moduse dumper.ml;;
15:
16: moduse interp.ml;;
17: moduse main.ml;;
18:
19: open Interp;;
20: open Main;;
21:
22: wantdump : true;;
23:
cse112wmAssignmentsasg2ocamlinterpcodebinary .ocamlinit