AAQZ 3
Table of Contents
1. AAQZ 3
1.1. Handling New Errors
program -> [parser] -> AST -> [interp] -> Value
Syntax Error: when something goes wrong in the parser.
- EX: ’{+ 1} –x–> Syntax Error
Runtime Error: when something goes wrong in the interpreter.
- EX: x -—> (IdC ’x) –x–> ERROR
- {f 2} -—> (AppC ’f (list (NumC 2))) –X–> ERROR
Our Langauge Name: AAQZ
<expr>::=<num> | {<op> <expr> <expr>} | {<id> <expr> ... } | {<id>} <op>::= +|*|-|/ <defn>::= {def <id> {(<id> ...) => <expr>}}
Now with the ’…’ more things can go wrong
- passing too many arguments or too little
- {def f {x x} => {+ x 1}}
- if we call (f 10 20) we have no clue which value it will choose.
- Parser should be able to check and catch this Parse Error
We want to find errors in the parser rather than during runtime.
1.2. Recursion With Our Function Definitions
{def my-rec {(n) => {my-rec {- n 1}}}}
{my-rec 2}
= {my-rec {- 2 1}} = {my-rec 1} = {my-rec {-1 1}} = {my-rec 0} … this goes on forever
We need conditions.
1.3. Conditionals in AAQZ 3 Language
We can add if less then or equal to 0 (ifleq0) that takes soemthing to compare and two cases if it does and if it doesnt.
<expr>::=<num> | {<op> <expr> <expr>} | {<id> <expr> ... } | {<id>} | {ifleq0? <expr> <expr> <expr>} <op>::= +|*|-|/ <defn>::= {def <id> {(<id> ...) => <expr>}}
1.4. What is a Program in AAQZ 3
A function definition with a bunch of other functions.
Any list of funcitons is a valid program
<prog>::= {<defn> …}
lets look at a PROGRAM
{{def fact {(n) => {ifleq0? n 1 {* n {fact {- n 1}}}}}}}
{def main {() => {fact 2}}}}
{fact 2}
= {ifleq0? 2 1 {* 2 {fact {- 2 1}}}} = {* 2 {fact {- 2 1}}} = {* 2 {fact 1}} = {* 2 {ifleq0? 1 1 {* 1 {fact {- 1 1}}}}} = {* 2 {* 1 {fact 0}}} … = 2
Substitution doesnt work in python because n isn’t guranteed to be 2 throughout the entirety of the function because variables are mutable.
mutation - variables changing values.
Another Program {{def f1 {(a) => {+ {f2 4} a}}}}
{def f2 {(b) => {* a b}}}
{def main {() => {f1 7}}}
should f2 be allowed to be called in f1?
- most langauges work in a top down scope and dont care in the order
Should a be allowed in the scope of f2?
- in original languages like lisp this is allowed.
- this is not the standard so lets forbid it.
{{def f1} {(a) => {f2 a}}}
{def f2} {(b) => {f1 b}}}
{def main {() => {f1 2}}}
what should happen