CS计算机代考程序代写 Java interpreter Lecture 4. DefineLang – Global Variables

Lecture 4. DefineLang – Global Variables

Lecture 4. DefineLang – Global Variables

February 1, 2021

Local and Global Variables

I Local variable: available based on the scope of the let expression
I Global variable: available during the entire iteration with the

interpreter
I (define Sun 0)
I (define a 97)

I DefineLang: new feature added to VarLang, called define declaration

I Syntax: keyword define, name, initial value

I Example:
(define i 1)
(define ii 2)
(* i ii)

Examples

DefineLang Demo

Grammar

Extending AST (syntax): Read Phase

1. ST node: DefineDecl

2. Modify program to store DefineDecl

3. Modify visitor interface to support DefineDecl

4. Modify formatter regrading print DefineDecl

Extending Semantics: Eval

I Varlang: evaluate a program starting in an empty environment;
DefineLang: when a program starts running, the declared global
variables are defined; the program can have free variables

I Unitval: A UnitVal is like a void type in Java. It allows programming
language definitions and implementations to uniformly treat
programs and expressions as evaluating to ’a value’ – e.g., a define
declaration

I each definition changes the global initEnv to add a new binding from
name to value.

Review and Further Reading

Definelang: support globals

I Syntax: definition declaration (AST node, visitor interface)

I Semantics: modify environment for each run; unitval;

Further reading:

I Rajan: CH 4, 7, 8