Skip to content

Commit 4687a0c

Browse files
committed
modify 'ast' -> 'parse_tree'
1 parent 012b0c7 commit 4687a0c

File tree

7 files changed

+252
-20
lines changed

7 files changed

+252
-20
lines changed

README.md

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
[![Build Status](https://travis-ci.org/gyaneman/fugafuga-lang.svg?branch=master)](https://travis-ci.org/gyaneman/fugafuga-lang)
44

55
fugafuga-lang is a programming language for my studying language implementation.
6-
The goal is ... no ... I cannot see it...
76

87
## Requirements
98

src/fgl.ml

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* This is the top level file.
44
*)
55

6-
open Ast
6+
open Parse_tree
77
open Value
88
open Environment
99
open Interp
@@ -14,12 +14,12 @@ let exec_file fname =
1414
let ic = open_in fname in
1515
try
1616
let lexbuf = Lexing.from_channel ic in
17-
let ast = Parser.main Lexer.token lexbuf in
17+
let pt = Parser.main Lexer.token lexbuf in
1818
close_in ic;
1919
if !enable_only_ast_print then
20-
print_string (string_of_program ast)
20+
print_string (string_of_program pt)
2121
else
22-
let result = interp ast [] in
22+
let result = interp pt [] in
2323
print_value result;
2424
;
2525
flush stdout;

src/interp.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
(* interp.ml *)
22

3-
open Ast
3+
open Parse_tree
44
open Value
55
open Environment
66

src/lexer.mll

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
(* lexer.mll *)
22

33
{
4-
open Ast
4+
open Parse_tree
55
open Parser (* The token is defined in parser.mli *)
66
exception Eof
77

src/parse_tree.ml

+233
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
(* parse_tree.ml *)
2+
3+
open Type
4+
5+
(* literals *)
6+
type literal =
7+
| Null
8+
| Int of int
9+
| Bool of bool
10+
| String of string
11+
;;
12+
13+
(* unary operators *)
14+
type una_op =
15+
| Not
16+
;;
17+
18+
(* binary operators *)
19+
type bin_op =
20+
| Add
21+
| Sub
22+
| Mul
23+
| Div
24+
| Mod
25+
| Equal
26+
| NotEqual
27+
| LT
28+
| LTE
29+
| GT
30+
| GTE
31+
;;
32+
33+
type ident_kind =
34+
| Variable
35+
;;
36+
37+
module type expr = sig
38+
type t = ident_kind
39+
end
40+
41+
(* expressions *)
42+
type expression =
43+
| Binary of bin_op * expression * expression
44+
| Unary of una_op * expression
45+
| Assign of string * expression (* dst, src *)
46+
| Call of expression * expression list
47+
| Ident of string * ident_kind
48+
| Literal of literal
49+
(* statements *)
50+
and statement =
51+
| Block of statement list
52+
| For of expression * expression * expression * statement list
53+
| Break
54+
| Continue
55+
| If of expression * statement list * statement list
56+
| Expression of expression
57+
| VarDecl of string * expression
58+
(* Function(id, ret datatype, param(id, datatype) list, statements) *)
59+
| Func of string * string * ((string * string) list) * statement list
60+
| Ret of expression
61+
(* program *)
62+
and program = { program: statement list }
63+
;;
64+
65+
66+
(* for metainfo *)
67+
type pos = { fname: string; lnum: int; bol: int; cnum: int };;
68+
type meta = { start_p: pos; end_p: pos };;
69+
let string_of_pos { fname = fn; lnum = ln; bol = bo; cnum = cn } =
70+
"(fname:" ^ fn ^ ", lnum:" ^ string_of_int ln ^
71+
", bol:" ^ string_of_int bo ^ ", cnum:" ^ string_of_int cn ^ ")"
72+
;;
73+
let string_of_meta { start_p = sp; end_p = ep } =
74+
"(start_p:" ^ string_of_pos sp ^ ", end_p:" ^ string_of_pos ep ^ ")"
75+
;;
76+
77+
78+
79+
(* pretty printer for AST *)
80+
let strlit s = "\"" ^ s ^ "\"";;
81+
let prop pn valstr =
82+
strlit pn ^ ":" ^ valstr
83+
let kind s = prop "kind" (strlit s)
84+
85+
let rec string_of_program pg =
86+
"{" ^
87+
kind "Program" ^ "," ^
88+
prop "program" ("[" ^ string_of_statement_list pg.program ^ "]")
89+
^ "}"
90+
and string_of_statement statement =
91+
"{" ^
92+
match statement with
93+
| Block (stmtlist) ->
94+
kind "Block" ^ "," ^
95+
prop "statement_list" ("[" ^ string_of_statement_list stmtlist ^ "]")
96+
| For (init, cond, update, body) ->
97+
kind "For" ^ "," ^
98+
prop "init" (string_of_expression init) ^ "," ^
99+
prop "cond" (string_of_expression cond) ^ "," ^
100+
prop "update" (string_of_expression update) ^ "," ^
101+
prop "body" ("[" ^ string_of_statement_list body ^ "]")
102+
| Break ->
103+
kind "Break"
104+
| Continue ->
105+
kind "Continue"
106+
| If (exp, conseq, alter) ->
107+
kind "If" ^ "," ^
108+
prop "condition" (string_of_expression exp) ^ "," ^
109+
prop "conseq" ("[" ^ string_of_statement_list conseq ^ "]") ^ "," ^
110+
prop "alter" ("[" ^ string_of_statement_list alter ^ "]")
111+
| Expression (exp) ->
112+
kind "Expression" ^ "," ^
113+
prop "exp" (string_of_expression exp)
114+
| VarDecl (dst_id, src_exp) ->
115+
kind "VarDecl" ^ "," ^
116+
prop "destination" (strlit dst_id) ^ "," ^
117+
prop "source" (string_of_expression src_exp)
118+
| Func (id, t, params, stmts) ->
119+
kind "Func" ^ "," ^
120+
prop "id" (strlit id) ^ "," ^
121+
prop "type" (strlit t) ^ "," ^
122+
prop "params" ("[" ^ string_of_param_list params ^ "]") ^ "," ^
123+
prop "stmts" ("[" ^ string_of_statement_list stmts ^ "]")
124+
| Ret (exp) ->
125+
kind "Func" ^ "," ^
126+
prop "exp" (string_of_expression exp)
127+
;
128+
^ "}"
129+
and string_of_statement_list stmtlist =
130+
match stmtlist with
131+
| [] -> ""
132+
| stmt :: stmtlist_ ->
133+
string_of_statement stmt ^
134+
string_of_statement_list_ stmtlist_
135+
;
136+
and string_of_statement_list_ = function
137+
| [] -> ""
138+
| stmtlist -> "," ^ string_of_statement_list stmtlist
139+
;
140+
and string_of_expression_list exprlist =
141+
match exprlist with
142+
| [] -> ""
143+
| expr :: exprlist_ ->
144+
string_of_expression expr ^
145+
string_of_expression_list_ exprlist_
146+
;
147+
and string_of_expression_list_ = function
148+
| [] -> ""
149+
| exprlist -> "," ^ string_of_expression_list exprlist
150+
;
151+
and string_of_expression exp =
152+
"{" ^
153+
match exp with
154+
| Binary (op, left, right) ->
155+
kind "BinaryExp" ^ "," ^
156+
prop "operator" (strlit (string_of_binop op)) ^ "," ^
157+
prop "left" (string_of_expression left) ^ "," ^
158+
prop "right" (string_of_expression right)
159+
| Unary (op, e) ->
160+
kind "UnaryExp" ^ "," ^
161+
prop "operator" (strlit (string_of_unaop op)) ^ "," ^
162+
prop "exp" (string_of_expression e)
163+
| Assign (dst, src) ->
164+
kind "AssignExp" ^ "," ^
165+
prop "destination" (strlit dst) ^ "," ^
166+
prop "source" (string_of_expression src)
167+
| Call (f, args) ->
168+
kind "CallExp" ^ "," ^
169+
prop "f" (string_of_expression f) ^ "," ^
170+
prop "args" ("[" ^ string_of_expression_list args ^ "]")
171+
| Ident (id, _) ->
172+
kind "Ident" ^ "," ^
173+
prop "id" (strlit id) ^ "," ^
174+
prop "ident_kind" (strlit "Variable")
175+
| Literal (lit) ->
176+
kind "Literal" ^ "," ^
177+
prop "literal" (string_of_literal lit)
178+
;
179+
^ "}"
180+
and string_of_string_list = function
181+
| [] -> ""
182+
| str :: strlist ->
183+
strlit str ^ string_of_string_list_ strlist
184+
and string_of_string_list_ = function
185+
| [] -> ""
186+
| x -> "," ^ string_of_string_list x
187+
and string_of_param id t =
188+
"{" ^
189+
kind "Param" ^ "," ^
190+
prop "id" (strlit id) ^ "," ^
191+
prop "type" (strlit t)
192+
^ "}"
193+
and string_of_param_list = function
194+
| [] -> ""
195+
| (id, t) :: paramlist ->
196+
string_of_param id t ^ string_of_param_list_ paramlist
197+
and string_of_param_list_ = function
198+
| [] -> ""
199+
| x -> "," ^ string_of_param_list x
200+
and string_of_binop = function
201+
| Add -> "add"
202+
| Sub -> "sub"
203+
| Mul -> "mul"
204+
| Div -> "div"
205+
| Mod -> "mod"
206+
| Equal -> "equal"
207+
| NotEqual -> "not equal"
208+
| LT -> "less than"
209+
| LTE -> "less than equal"
210+
| GT -> "greater than"
211+
| GTE -> "greater than equal"
212+
and string_of_unaop = function
213+
| Not -> "not"
214+
and string_of_literal lit =
215+
"{" ^
216+
match lit with
217+
| Null ->
218+
kind "Null"
219+
| Int (num) ->
220+
kind "Int" ^ "," ^
221+
prop "num" (string_of_int num)
222+
| Bool (b) ->
223+
kind "Bool" ^ "," ^
224+
prop "bool" (string_of_bool b)
225+
| String (str) ->
226+
kind "String" ^ "," ^
227+
prop "str" (strlit str)
228+
;
229+
^ "}"
230+
;;
231+
232+
233+

src/parser.mly

+12-12
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,44 @@
11
%{
2-
open Ast
2+
open Parse_tree
33
open Type
44
%}
55

66

77
/* literals */
8-
%token <Ast.meta*int> INT
9-
%token <Ast.meta*string> STR
8+
%token <Parse_tree.meta*int> INT
9+
%token <Parse_tree.meta*string> STR
1010

1111
/* identifier */
12-
%token <Ast.meta*string> IDENT
12+
%token <Parse_tree.meta*string> IDENT
1313

1414
/* operators */
15-
%token <Ast.meta> PLUS MINUS MUL DIV MOD
15+
%token <Parse_tree.meta> PLUS MINUS MUL DIV MOD
1616

1717
/* assignment operators */
18-
%token <Ast.meta> ASSIGN
18+
%token <Parse_tree.meta> ASSIGN
1919

2020
/* comparison operators */
21-
%token <Ast.meta> EQ NOTEQ LT LTE GT GTE NOT
21+
%token <Parse_tree.meta> EQ NOTEQ LT LTE GT GTE NOT
2222

2323
/* parenthesis, braces, brackets */
24-
%token <Ast.meta> PARENL PARENR BRACEL BRACER BRACKETL BRACKETR
24+
%token <Parse_tree.meta> PARENL PARENR BRACEL BRACER BRACKETL BRACKETR
2525

2626
/* symbols */
27-
%token <Ast.meta> COLON SEMICOLON CAMMA
27+
%token <Parse_tree.meta> COLON SEMICOLON CAMMA
2828

2929
/* keywords */
30-
%token <Ast.meta> FUNC RET VAR FOR BREAK CONTINUE IF ELSE TRUE FALSE
30+
%token <Parse_tree.meta> FUNC RET VAR FOR BREAK CONTINUE IF ELSE TRUE FALSE
3131

3232
/* others */
33-
%token <Ast.meta> EOF
33+
%token <Parse_tree.meta> EOF
3434

3535
%left PLUS MINUS
3636
%left MUL DIV MOD
3737
%left EQ NOTEQ LT LTE GT GTE
3838
%nonassoc UMINUS NOT
3939

4040
%start main
41-
%type <Ast.program> main
41+
%type <Parse_tree.program> main
4242

4343

4444

src/value.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ type value =
66
| IntVal of int
77
| BoolVal of bool
88
| StringVal of string
9-
| FuncVal of string list * Ast.statement list
9+
| FuncVal of string list * Parse_tree.statement list
1010
;;
1111

1212
let string_of_value = function

0 commit comments

Comments
 (0)