LMU ☀️ CMSI 585
PROGRAMMING LANGUAGE FOUNDATIONS
HOMEWORK #2 Due: 2026-08-04

Learning Objectives

With this assignment you will demonstrate:

Readings and Videos

Please:

Instructions

Submit, to BrightSpace, a neatly typeset answer to Problems 1 and 2, and a link to a GitHub repo for Problem 3. Please make a best effort for typesetting. Handwritten answers are okay if you are short on time, but pay extra attention to neatness if you go that route.

Problems to Submit

Each of the problems are for an extension to the Bella language in the course notes. The new language is called Bella 2, and like Bella, is strongly-typed and dynamically typed. Here is the abstract syntax:

$ \begin{array}{l} n\!: \mathsf{Numeral} \\ i\!: \mathsf{Identifier} \\ e\!: \mathsf{Expression} = n \;|\; i \;|\; \mathtt{true} \;|\; \mathtt{false} \;|\; \mathit{uop} \; e \;|\; e_1 \; \mathit{bop} \; e_2 \;|\; i \; e^* \;|\; e \; \mathtt{?} \; e_1 \; \mathtt{:} \; e_2 \;|\; \mathtt{[} \; e^* \; \mathtt{]} \;|\; e \mathtt{[} e \mathtt{]} \\ s\!: \mathsf{Statement} = \mathtt{let}\;i = e \;|\; \mathtt{func}\;i\;i^*=e \;|\; i = e \;|\; \mathtt{print}\;e \;|\; \mathtt{while}\;e\;b \\ b\!: \mathsf{Block} = \mathtt{block}\; s^* \\ p\!: \mathsf{Program} = \mathtt{program}\; b \end{array} $

The unary operators are - and !. The binary operators are +, -, *, /, %, **, <, <=, ==, !=, >=, >, &&, and ||.

The standard library is as we saw in class.

Here are the problems to submit. Please use Claude, Codex, Gemini or your favorite agent for assistance. You may let the agent do the entire assignment for you; however, you still need to read and understand everything. Use the assist as a learning opportunity.

This assignment is too large to do entirely by hand.

  1. Give an operational semantics for Bella 2. (It is possible that this is already done in the course notes. There is benefit in packaging it all up neatly as part of an assignment packet, though.)
  2. Give a denotational semantics for Bella 2.
  3. Write a complete interpreter for Bella 2. The language in which you implement the interpreter is not important. TypeScript, any ML dialect, Clojure, Rust, Swift, Kotlin, Python, Java, Go, or any other language is fine. What is important is that you include a test suite with 100% coverage. I will also ask that you use modern forms of the language you choose. For example, if you choose Java, please use records and sealed classes and any other modern Java feature, such as switch expressions, that make sense.
  4. The input to the interpret function can be a structured program representation (an abstract syntax “tree” object). As this is not a compiler writing class, there is no need to write a parser. If you wish to write a parser, I won’t stop you, but I will not be awarding any extra credit for doing so.

    If you would like to use TypeScript, I have a skeleton of code you can use to get started:

    type BuiltInFunction = (...args: Value[]) => Value;
    type UserFunction = [Identifier[], Expression];
    export type Value = number | boolean | Value[] | BuiltInFunction | UserFunction;
    
    type Memory = Map<string, Value>;
    type Output = Value[];
    type State = [Memory, Output];
    
    // Custom type guards
    
    function isUserFunction(v: Value): v is UserFunction {
      return Array.isArray(v) && Array.isArray(v[0]) && v[0].length === 2;
    }
    
    function isBuiltInFunction(v: Value): v is BuiltInFunction {
      return typeof v === "function";
    }
    
    function isArray(x: Value): x is Value[] {
      return Array.isArray(x);
    }
    
    // Expressions
    
    export interface Expression {
      interpret(m: Memory): Value;
    }
    
    export class Numeral implements Expression {
      constructor(public value: number) {}
      interpret(_: Memory): Value {
        // TODO
      }
    }
    
    export class BooleanLiteral implements Expression {
      constructor(public value: boolean) {}
      interpret(_: Memory): Value {
        // TODO
      }
    }
    
    export class Identifier implements Expression {
      constructor(public name: string) {}
      interpret(m: Memory): Value {
        // TODO
      }
    }
    
    export class UnaryExpression implements Expression {
      constructor(public operator: string, public expression: Expression) {}
      interpret(m: Memory): Value {
        // TODO
      }
    }
    
    export class BinaryExpression implements Expression {
      constructor(
        public operator: string,
        public left: Expression,
        public right: Expression
      ) {}
      interpret(m: Memory): Value {
        // TODO
      }
    }
    
    export class Call implements Expression {
      constructor(public callee: Identifier, public args: Expression[]) {}
      interpret(m: Memory): Value {
        const functionValue = m.get(this.callee.name);
        const argValues = this.args.map((arg) => arg.interpret(m));
        if (functionValue === undefined) {
          throw new Error("Identifier was undeclared");
        } else if (isUserFunction(functionValue)) {
          const [parameters, expression] = functionValue;
          if (parameters.length !== this.args.length) {
            throw new Error("Wrong number of arguments");
          }
          const locals = parameters.map((p, i) => [p.name, argValues[i]] as const);
          return expression.interpret(new Map([...m, ...locals]));
        } else if (isBuiltInFunction(functionValue)) {
          return functionValue(...argValues);
        } else {
          throw new Error("Not a function");
        }
      }
    }
    
    export class ConditionalExpression implements Expression {
      constructor(
        public test: Expression,
        public consequent: Expression,
        public alternate: Expression
      ) {}
      interpret(m: Memory): Value {
        // TODO
    }
    
    export class ArrayLiteral implements Expression {
      constructor(public elements: Expression[]) {}
      interpret(m: Memory): Value {
        // TODO
      }
    }
    
    export class SubscriptExpression implements Expression {
      constructor(public array: Expression, public subscript: Expression) {}
      interpret(m: Memory): Value {
        // TODO
      }
    }
    
    // Statements
    
    export interface Statement {
      interpret([m, o]: State): State;
    }
    
    export class VariableDeclaration implements Statement {
      constructor(public id: Identifier, public expression: Expression) {}
      interpret([m, o]: State): State {
        // TODO
      }
    }
    
    export class FunctionDeclaration implements Statement {
      constructor(
        public id: Identifier,
        public parameters: Identifier[],
        public expression: Expression
      ) {}
      interpret([m, o]: State): State {
        // TODO
      }
    }
    
    export class Assignment implements Statement {
      constructor(public id: Identifier, public expression: Expression) {}
      interpret([m, o]: State): State {
        // TODO
      }
    }
    
    export class PrintStatement implements Statement {
      constructor(public expression: Expression) {}
      interpret([m, o]: State): State {
        return [m, [...o, this.expression.interpret(m)]];
      }
    }
    
    export class WhileStatement implements Statement {
      constructor(public expression: Expression, public block: Block) {}
      interpret([m, o]: State): State {
        // TODO
      }
    }
    
    // Block
    
    export class Block {
      constructor(public statements: Statement[]) {}
      interpret([m, o]: State): State {
        let state: State = [m, o];
        for (let statement of this.statements) {
          state = statement.interpret(state);
        }
        return state;
      }
    }
    
    // Program
    
    export class Program {
      constructor(public block: Block) {}
      interpret(): Output {
        const initialMemory: Memory = new Map<string, Value>([
          ["pi", Math.PI as Value],
          ["sqrt", Math.sqrt as Value],
          ["sin", Math.sin as Value],
          ["cos", Math.cos as Value],
          ["ln", Math.log as Value],
          ["exp", Math.exp as Value],
          ["hypot", Math.hypot as Value],
        ]);
        const [_, o] = this.block.interpret([initialMemory, []]);
        return o;
      }
    }
    
    export function interpret(p: Program) {
      return p.interpret();
    }
    

    An invocation of the interpreter might look like this:

    const sample: Program = new Program(
      new Block([new PrintStatement(new Numeral(5))])
    )
    
    interpret(sample)