Skip to content

Commit d71e489

Browse files
authored
Merge pull request #3 from The-Continuers/sep
Sep
2 parents d7153f0 + b6e9ad7 commit d71e489

File tree

6 files changed

+53
-28
lines changed

6 files changed

+53
-28
lines changed

datatypes.rkt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
(func (name string?) (params func_param*?) (statements list?))
1313
(if_stmt (cond_exp expression?) (if_sts list?) (else_sts list?))
1414
(for_stmt (iter string?) (list_exp expression?) (sts list?))
15+
(print_stmt (expressions expression*?))
1516
)
1617

1718
(define-datatype func_param func_param?

interpreter/executor.rkt

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
(require "../exceptions.rkt")
1717

18+
(require "../utils.rkt")
19+
1820
(define (extend-params-scopes -scope params param-values)
1921
(cases eval-func-param* params
2022
(empty-eval-func-param () -scope)
@@ -43,7 +45,7 @@
4345
(cases expression* -expressions
4446
(empty-expr () (list))
4547
(expressions (expr rest-exprs)
46-
(cons (value-of expr scope-index) (expression*->list-val rest-exprs scope-index))
48+
(append (expression*->list-val rest-exprs scope-index) (list (value-of expr scope-index)))
4749
)
4850
)
4951
)
@@ -91,18 +93,21 @@
9193

9294

9395
(define (apply-for iter iter_list sts scope-index parent_stmt)
94-
(begin (display-lines (list iter_list))
95-
(cond
96-
[(not (pair? iter)) (report-not-pair iter_list parent_stmt)]
97-
[(null? iter_list) null]
98-
[else (let ([_ (extend-scope-index scope-index iter (car iter_list))])
99-
(let ([first_exec_result (exec-stmts sts scope-index)])
100-
(cond
101-
[(equal? first_exec_result (new-break)) null]
102-
[else (apply-for iter (cdr iter_list) sts scope-index parent_stmt)])
103-
)
104-
)]
105-
)))
96+
(begin
97+
; (display-lines (list iter_list))
98+
(cond
99+
[(not (pair? iter_list)) (report-not-pair iter_list parent_stmt)]
100+
[(null? iter_list) null]
101+
[else (begin
102+
(extend-scope-index scope-index iter (car iter_list))
103+
; (display-lines (list (get-scope-by-index scope-index)))
104+
(let ([first_exec_result (exec-stmts sts scope-index)])
105+
(cond
106+
[(equal? first_exec_result (new-break)) null]
107+
[else (apply-for iter (cdr iter_list) sts scope-index parent_stmt)])
108+
)
109+
)]
110+
)))
106111

107112
(define (apply-if cond-val if-sts else-sts scope-index parent_stmt)
108113
(cond
@@ -120,6 +125,11 @@
120125
(extend-scope-index assign-scope-index var val))
121126
)
122127

128+
(define (apply-print vals)
129+
; (display-lines (list "printing"))
130+
(display-lines vals)
131+
)
132+
123133
(define (exec stmt scope-index)
124134
(cases statement stmt
125135
(assign (var expr) (apply-assign var (value-of expr scope-index) scope-index))
@@ -144,9 +154,10 @@
144154
(for_stmt (iter list_exp sts) (apply-for
145155
iter (value-of list_exp scope-index) sts scope-index
146156
stmt))
157+
(print_stmt (expressions) (apply-print (expression*->list-val expressions scope-index)))
147158
)
148159
)
149-
;(trace exec value-of)
160+
150161
(define (exec-stmts stmts scope-index)
151162
(cond
152163
[(null? stmts) null]
@@ -166,7 +177,10 @@
166177
(reset-scope)
167178
(add-scope (init-scope))
168179
(exec-stmts program 0)
180+
(void)
169181
)
170182
)
171183

184+
; (trace exec-stmts exec value-of)
185+
172186
(provide (all-defined-out))

passes/lexer.rkt

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
#lang racket
22
(require parser-tools/lex
33
(prefix-in : parser-tools/lex-sre))
4+
(require "../utils.rkt")
45

56
(define-tokens LITERALS (ID NUMBER))
67

7-
(define-empty-tokens KWS (DEF GLOBAL PASS BREAK CONTINUE RETURN NONE))
8+
(define-empty-tokens KWS (DEF GLOBAL PASS BREAK CONTINUE RETURN NONE PRINT))
89
(define-empty-tokens OPS (ASSIGN LPAR RPAR PAR COLON PAR_COLON COMMA SEMICOLON))
910
(define-empty-tokens LOOP_KWS (FOR IN))
1011
(define-empty-tokens BOOL_KWS (TRUE FALSE))
@@ -25,9 +26,10 @@
2526
("continue" (token-CONTINUE))
2627
("return" (token-RETURN))
2728
("None" (token-NONE))
29+
("print" (token-PRINT))
2830
;OPS
2931
("=" (token-ASSIGN))
30-
("():" (token-PAR_COLON))
32+
; ("():" (token-PAR_COLON))
3133
("()" (token-PAR))
3234
("(" (token-LPAR))
3335
(")" (token-RPAR))
@@ -82,10 +84,11 @@
8284
(define (lex-this prog-string)
8385
(let ([l (open-input-string prog-string)])
8486
(begin
85-
(display-lines (list prog-string))
86-
(lambda () (display-return (python-lexer l))))
87-
))
88-
89-
(define (display-return l) (begin (display-lines (list l)) l ))
87+
; (display-lines (list prog-string))
88+
(lambda ()
89+
; (display-return
90+
(python-lexer l)
91+
; )
92+
))))
9093

9194
(provide (all-defined-out))

passes/parser.rkt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
(require parser-tools/lex
55
parser-tools/yacc)
66

7+
(require "../utils.rkt")
8+
79
(require "../datatypes.rkt")
810
(#%require "../datatypes.rkt")
911

@@ -31,6 +33,8 @@
3133
((PASS) (pass))
3234
((BREAK) (break))
3335
((CONTINUE) (continue))
36+
((PRINT PAR) (print_stmt (empty-expr)))
37+
((PRINT LPAR Arguments RPAR) (print_stmt $3))
3438
)
3539
(Compound_stmt ((Function_def) $1)
3640
((If_stmt) $1)
@@ -42,7 +46,7 @@
4246
)
4347
(Global_stmt ((GLOBAL ID) (global $2)))
4448
(Function_def ((DEF ID LPAR Params RPAR COLON Statements) (func $2 $4 $7))
45-
((DEF ID PAR_COLON Statements) (func $2 (empty-param) $4))
49+
((DEF ID PAR COLON Statements) (func $2 (empty-param) $5))
4650
)
4751
(Params ((Param_with_default) (func_params $1 (empty-param)))
4852
((Params COMMA Param_with_default) (func_params $3 $1))
@@ -94,7 +98,7 @@
9498
)
9599
(Atom ((ID) (ref $1))
96100
((TRUE) (atomic_bool_exp true))
97-
((FALSE) (atomic_num_exp false))
101+
((FALSE) (atomic_bool_exp false))
98102
((NONE) (atomic_null_exp))
99103
((NUMBER) (atomic_num_exp $1))
100104
((List) $1)
@@ -108,6 +112,10 @@
108112
; (debug "x.txt")
109113
))
110114

111-
(define (parse-scan prog-string) (python-parser (lex-this prog-string)))
115+
(define (parse-scan prog-string)
116+
; (display-return
117+
(python-parser (lex-this prog-string))
118+
; )
119+
)
112120

113121
(provide (all-defined-out))

tester.rkt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,4 @@
44

55
(define test-1 (string-join (file->lines "test_cases/T4 (General Test) - Students/in4.txt")))
66

7-
; test-1
8-
(display-lines (list test-1))
9-
10-
(interpret (string-join (file->lines "test_cases/T4 (General Test) - Students/in4.txt")))
7+
(interpret test-1)

utils.rkt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,6 @@
44

55
(define (contains l x) (if (member x l) #t #f))
66

7+
(define (display-return l) (begin (display-lines (list l)) l ))
8+
79
(provide (all-defined-out))

0 commit comments

Comments
 (0)