CS302: Paradigms of Programming Variations on the Scheme Interpreter Manas Thakur Feb-June 2020
A feat accomplished • What we have seen: • The kernel of an Eval/Apply Interpreter based on the Environment Model for a (good enough) subset of Scheme • Add the implementation of environment data structures: • We get a working interpreter! 2
Graduate to Post-Graduate • We can use the knowledge gained to experiment with programming language technology in several ways: 1. Extend the language with new syntactic constructs • What’s the disadvantage of using macros for this? 2. Explore various design choices in the space of programming languages 3. Design new programming languages themselves! • That even introduce novel paradigms of programming!! 3
1. Extending languages with new syntactic sugar 4
Varargs in Scheme • Desire: Eliminate the need to “fix” the number of parameters that a procedure takes (+ x x) • Have we seen such procedures? (+ x y z) • Hint: We saw some in the first class itself! (* (+ 2 3) (+ 5 7 8) z) • Need changes at three levels: • Syntax • Semantics • Implementation 5
Choosing syntax for a new construct • Considerations: • Do we need a keyword or a symbol? • ?, :, if • How much would we be constraining the programmers as a result of our choice? • Underscores if (c1) S1 else if (c2) S2 • Can we introduce it unambiguously? else S3 • Problems with ambiguity? • Hyphens, if-else chaining, … 6
Syntax and Semantics for Varargs (define foo (lambda (x . y) (+ x (sum-list y)))) (define (sum-list (lambda (l) What happens (foldr + l 0))) without the ‘.’? > (foo 2 5 7 9) • x takes the first argument • y takes the rest of the arguments • y has to be a list. 7
Changes in the interpreter • Standard way of pairing up actual argument values with formal parameter variables: (define (pair-up (lambda (vars vals) (cond ((eq? vars ‘()) (cond ((eq? vals ‘()) ‘()) (else (error “TMA”)))) ((eq? vals ‘()) (error “TFA”)) (else (cons (cons (car vars) (car vals)) (pair-up (cdr vars) (cdr vals))))))) 8
Changes in the interpreter (Cont.) • Pairing up actual argument values with formal parameter variables with varargs: (define (pair-up (lambda (vars vals) (cond ((eq? vars ‘()) (cond ((eq? vals ‘()) ‘()) (else (error “TMA”)))) ((symbol? vars) (cons (cons vars vals) ‘())) ((eq? vals ‘()) (error “TFA”)) (else (cons (cons (car vars) (car vals)) (pair-up (cdr vars) (cdr vals))))))) 9
Recommend
More recommend