Skip to content

Commit

Permalink
[www] publish Annotated.java to complete the java lab2 stub
Browse files Browse the repository at this point in the history
  • Loading branch information
andreasabel committed Nov 22, 2024
1 parent 3d82935 commit 893ca04
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Material: plt = course book, dragon = Dragon book. Slides follow closely the plt
| Tue 19/11 | 13-15 | Type checking | [slides](plt-book/ipl-book/slides/4-slides-ipl-book.pdf), plt 4, dragon 5,6, [script](notes/type-checking.html), [prime.c](notes/prime.c), [prime-stms.c](notes/prime-stms.c), [division.c](notes/division.c), [division-annotated.c](notes/division-annotated.c) |
| Thu 21/11 | 13-15 | Interpreting | [slides](plt-book/ipl-book/slides/5-slides-ipl-book.pdf), plt 5, [script](notes/interpreter.html) |
| Tue 26/11 | 13-14 | Hands-on with Lab 2 (Haskell) | [script](notes/monads.html) |
| Tue 26/11 | 14-15 | Hands-on with Lab 2 (Java) | [script](notes/java.html) |
| Tue 26/11 | 14-15 | Hands-on with Lab 2 (Java) | [script](notes/java.html), [Annotated.java](notes/Annotated.java) |
| Thu 28/11 | 13-15 **SB-H5** | Code generation | [slides](plt-book/ipl-book/slides/6-slides-ipl-book.pdf), plt 6, dragon 6,7, [notes](notes/compilation.html), [prime.c](notes/prime.c), [prime.j](notes/prime.j) |
| Tue 03/12 | 13-14 | Hands-on with Lab 3 (Haskell) | |
| Tue 03/12 | 14-15 | Hands-on with Lab 3 (Java) | |
Expand Down
92 changes: 92 additions & 0 deletions notes/Annotated.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Programming Language Technology (Chalmers DAT151 / GU DIT231)
// (C) 2022-24 Andreas Abel
// All rights reserved.

import java.util.List;

// The annotated version of abstract syntax of language CMM.

interface Annotated {

// The following is yet a representation of the CMM syntax
// _before_ type checking.
//
// TODO: Change this to type-annotated syntax trees.
//
// In essence this amounts to:
//
// * Adding "Type" fields to some of the constructors.
// * Adding a "type()" method to the "Exp" interface to give
// uniform constant-time access to the type of the expression.
// * Possibly adding an expression constructor to convert from 'int' to 'double'.
//
// However, you might want to also restructure expressions (and statements etc.)
// to better fit the need of the interpreter.
// (E.g., 'EMul' and 'EAdd' could be fused to 'EArithOp' etc.)

// Programs

record Program(List<Def> listdef_) {}

// Function definitions

record Def(Type type_, String id_, List<Arg> listarg_, List<Stm> liststm_) {}

// Function parameters

record Arg(Type type_, String id_) {}

// Statements

sealed interface Stm permits
SDecls, SInit, SBlock,
SExp, SReturn, SWhile, SIfElse {}

record SDecls (Type type_, List<String> listid_) implements Stm {}
record SInit (Type type_, String id_, Exp exp_) implements Stm {}
record SBlock (List<Stm> liststm_) implements Stm {}
record SExp (Exp exp_) implements Stm {}
record SReturn (Exp exp_) implements Stm {}
record SWhile (Exp exp_, Stm stm_) implements Stm {}
record SIfElse (Exp exp_, Stm stm_1, Stm stm_2) implements Stm {}

// Expressions

sealed interface Exp permits
EBool, EInt, EDouble, EId, EApp, EPost, EPre,
EMul, EAdd, ECmp, EAnd, EOr, EAss {}

record EBool (Boolean boolean_) implements Exp {}
record EInt (Integer integer_) implements Exp {}
record EDouble (Double double_) implements Exp {}

record EId (String id_) implements Exp {}
record EApp (String id_, List<Exp> listexp_) implements Exp {}

record EPost (String id_, IncDecOp incdecop_) implements Exp {}
record EPre (IncDecOp incdecop_, String id_) implements Exp {}

record EMul (Exp exp_1, MulOp mulop_, Exp exp_2) implements Exp {}
record EAdd (Exp exp_1, AddOp addop_, Exp exp_2) implements Exp {}
record ECmp (Exp exp_1, CmpOp cmpop_, Exp exp_2) implements Exp {}

record EAnd (Exp exp_1, Exp exp_2) implements Exp {}
record EOr (Exp exp_1, Exp exp_2) implements Exp {}

record EAss (String id_, Exp exp_) implements Exp {}

// Types and operators can be concisely represented as enums.
// However, you could also (partially) reuse their definition in cmm.Absyn.

// Operators

enum IncDecOp { OInc, ODec };
enum MulOp { OTimes, ODiv };
enum AddOp { OPlus, OMinus };
enum CmpOp { OLt, OGt, OLtEq, OGtEq, OEq, ONEq };

// Types

enum Type { Type_bool, Type_int, Type_double, Type_void };

}

0 comments on commit 893ca04

Please sign in to comment.