diff --git a/datatypes.rkt b/datatypes.rkt index cbd9fcb..e882584 100644 --- a/datatypes.rkt +++ b/datatypes.rkt @@ -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? diff --git a/interpreter/executor.rkt b/interpreter/executor.rkt index 10cd855..53573ce 100644 --- a/interpreter/executor.rkt +++ b/interpreter/executor.rkt @@ -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) @@ -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))) ) ) ) @@ -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 @@ -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)) @@ -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] @@ -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)) \ No newline at end of file diff --git a/passes/lexer.rkt b/passes/lexer.rkt index d7866c5..234046d 100644 --- a/passes/lexer.rkt +++ b/passes/lexer.rkt @@ -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)) @@ -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)) @@ -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)) diff --git a/passes/parser.rkt b/passes/parser.rkt index 6c7ab45..768da0d 100644 --- a/passes/parser.rkt +++ b/passes/parser.rkt @@ -4,6 +4,8 @@ (require parser-tools/lex parser-tools/yacc) +(require "../utils.rkt") + (require "../datatypes.rkt") (#%require "../datatypes.rkt") @@ -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) @@ -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)) @@ -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) @@ -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)) \ No newline at end of file diff --git a/tester.rkt b/tester.rkt index dfcea7a..a5283e8 100644 --- a/tester.rkt +++ b/tester.rkt @@ -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) diff --git a/utils.rkt b/utils.rkt index a47b86d..504e1ee 100644 --- a/utils.rkt +++ b/utils.rkt @@ -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)) \ No newline at end of file