Skip to content

Commit

Permalink
Merge pull request #3 from The-Continuers/sep
Browse files Browse the repository at this point in the history
Sep
  • Loading branch information
ashkan-khd authored Jul 22, 2022
2 parents d7153f0 + b6e9ad7 commit d71e489
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 28 deletions.
1 change: 1 addition & 0 deletions datatypes.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
(func (name string?) (params func_param*?) (statements list?))
(if_stmt (cond_exp expression?) (if_sts list?) (else_sts list?))
(for_stmt (iter string?) (list_exp expression?) (sts list?))
(print_stmt (expressions expression*?))
)

(define-datatype func_param func_param?
Expand Down
42 changes: 28 additions & 14 deletions interpreter/executor.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

(require "../exceptions.rkt")

(require "../utils.rkt")

(define (extend-params-scopes -scope params param-values)
(cases eval-func-param* params
(empty-eval-func-param () -scope)
Expand Down Expand Up @@ -43,7 +45,7 @@
(cases expression* -expressions
(empty-expr () (list))
(expressions (expr rest-exprs)
(cons (value-of expr scope-index) (expression*->list-val rest-exprs scope-index))
(append (expression*->list-val rest-exprs scope-index) (list (value-of expr scope-index)))
)
)
)
Expand Down Expand Up @@ -91,18 +93,21 @@


(define (apply-for iter iter_list sts scope-index parent_stmt)
(begin (display-lines (list iter_list))
(cond
[(not (pair? iter)) (report-not-pair iter_list parent_stmt)]
[(null? iter_list) null]
[else (let ([_ (extend-scope-index scope-index iter (car iter_list))])
(let ([first_exec_result (exec-stmts sts scope-index)])
(cond
[(equal? first_exec_result (new-break)) null]
[else (apply-for iter (cdr iter_list) sts scope-index parent_stmt)])
)
)]
)))
(begin
; (display-lines (list iter_list))
(cond
[(not (pair? iter_list)) (report-not-pair iter_list parent_stmt)]
[(null? iter_list) null]
[else (begin
(extend-scope-index scope-index iter (car iter_list))
; (display-lines (list (get-scope-by-index scope-index)))
(let ([first_exec_result (exec-stmts sts scope-index)])
(cond
[(equal? first_exec_result (new-break)) null]
[else (apply-for iter (cdr iter_list) sts scope-index parent_stmt)])
)
)]
)))

(define (apply-if cond-val if-sts else-sts scope-index parent_stmt)
(cond
Expand All @@ -120,6 +125,11 @@
(extend-scope-index assign-scope-index var val))
)

(define (apply-print vals)
; (display-lines (list "printing"))
(display-lines vals)
)

(define (exec stmt scope-index)
(cases statement stmt
(assign (var expr) (apply-assign var (value-of expr scope-index) scope-index))
Expand All @@ -144,9 +154,10 @@
(for_stmt (iter list_exp sts) (apply-for
iter (value-of list_exp scope-index) sts scope-index
stmt))
(print_stmt (expressions) (apply-print (expression*->list-val expressions scope-index)))
)
)
;(trace exec value-of)

(define (exec-stmts stmts scope-index)
(cond
[(null? stmts) null]
Expand All @@ -166,7 +177,10 @@
(reset-scope)
(add-scope (init-scope))
(exec-stmts program 0)
(void)
)
)

; (trace exec-stmts exec value-of)

(provide (all-defined-out))
17 changes: 10 additions & 7 deletions passes/lexer.rkt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#lang racket
(require parser-tools/lex
(prefix-in : parser-tools/lex-sre))
(require "../utils.rkt")

(define-tokens LITERALS (ID NUMBER))

(define-empty-tokens KWS (DEF GLOBAL PASS BREAK CONTINUE RETURN NONE))
(define-empty-tokens KWS (DEF GLOBAL PASS BREAK CONTINUE RETURN NONE PRINT))
(define-empty-tokens OPS (ASSIGN LPAR RPAR PAR COLON PAR_COLON COMMA SEMICOLON))
(define-empty-tokens LOOP_KWS (FOR IN))
(define-empty-tokens BOOL_KWS (TRUE FALSE))
Expand All @@ -25,9 +26,10 @@
("continue" (token-CONTINUE))
("return" (token-RETURN))
("None" (token-NONE))
("print" (token-PRINT))
;OPS
("=" (token-ASSIGN))
("():" (token-PAR_COLON))
; ("():" (token-PAR_COLON))
("()" (token-PAR))
("(" (token-LPAR))
(")" (token-RPAR))
Expand Down Expand Up @@ -82,10 +84,11 @@
(define (lex-this prog-string)
(let ([l (open-input-string prog-string)])
(begin
(display-lines (list prog-string))
(lambda () (display-return (python-lexer l))))
))

(define (display-return l) (begin (display-lines (list l)) l ))
; (display-lines (list prog-string))
(lambda ()
; (display-return
(python-lexer l)
; )
))))

(provide (all-defined-out))
14 changes: 11 additions & 3 deletions passes/parser.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
(require parser-tools/lex
parser-tools/yacc)

(require "../utils.rkt")

(require "../datatypes.rkt")
(#%require "../datatypes.rkt")

Expand Down Expand Up @@ -31,6 +33,8 @@
((PASS) (pass))
((BREAK) (break))
((CONTINUE) (continue))
((PRINT PAR) (print_stmt (empty-expr)))
((PRINT LPAR Arguments RPAR) (print_stmt $3))
)
(Compound_stmt ((Function_def) $1)
((If_stmt) $1)
Expand All @@ -42,7 +46,7 @@
)
(Global_stmt ((GLOBAL ID) (global $2)))
(Function_def ((DEF ID LPAR Params RPAR COLON Statements) (func $2 $4 $7))
((DEF ID PAR_COLON Statements) (func $2 (empty-param) $4))
((DEF ID PAR COLON Statements) (func $2 (empty-param) $5))
)
(Params ((Param_with_default) (func_params $1 (empty-param)))
((Params COMMA Param_with_default) (func_params $3 $1))
Expand Down Expand Up @@ -94,7 +98,7 @@
)
(Atom ((ID) (ref $1))
((TRUE) (atomic_bool_exp true))
((FALSE) (atomic_num_exp false))
((FALSE) (atomic_bool_exp false))
((NONE) (atomic_null_exp))
((NUMBER) (atomic_num_exp $1))
((List) $1)
Expand All @@ -108,6 +112,10 @@
; (debug "x.txt")
))

(define (parse-scan prog-string) (python-parser (lex-this prog-string)))
(define (parse-scan prog-string)
; (display-return
(python-parser (lex-this prog-string))
; )
)

(provide (all-defined-out))
5 changes: 1 addition & 4 deletions tester.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,4 @@

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

; test-1
(display-lines (list test-1))

(interpret (string-join (file->lines "test_cases/T4 (General Test) - Students/in4.txt")))
(interpret test-1)
2 changes: 2 additions & 0 deletions utils.rkt
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@

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

(define (display-return l) (begin (display-lines (list l)) l ))

(provide (all-defined-out))

0 comments on commit d71e489

Please sign in to comment.