Skip to content

anyahelene/cocodo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Language Implementation with Rascal – A Tutorial

  • Anya Helene Bagge, Department of Informatics, University of Bergen

History

  • First edition: Tutorial at CoCoDo'17
  • Second edition: Tutorial at UFCG in Campina Grande, Brazil, 2017
  • Third edition: Tutorial at CoCoDo'18

Setup

Resources

Things you should (hope) to learn

  • Get a brief overview of what Rascal is and what it can be used for

  • Basics of syntax definition – be aware of generalised parsing and ambiguities

  • Basics of working with concrete syntax

  • Strings and string interpolation

  • Visiting data structures

  • Basics of making an IDE

Simple experiments

Open the CoCoDo project. You'll find various source code in the 'src' folder.

If you don't have a Rascal console open in Eclipse already, right click on the project and select Rascal console. Make sure the title of the console tab includes "project: cocodo".

Set up language plugin

This will setup up syntax highlighting for the languages we're creating.

rascal>import Plugin;
ok
rascal>main();
ok

Simpl Language

Import the language definition:

import simpl::Simpl;

Remember the semicolon at the end, otherwise you'll get a confusing >>> prompt!

If you want to play around with parsing, you should also import the ParseTree module:

import ParseTree;
example = parseSimpl(|project://cocodo/src/simpl/example.simpl|)

Things to do

  • The minus operator is not implemented. Add it to the evaluator and typechecker.
  • Simpl is half way between a language based on expressions, and a language that takes a list of definitions, and then a list of expressions. This should be fixed in the evaluator and the typechecker (it's done already for the Simpr language)
  • The Plugin file defines the Eclipse plugin, have a look at it, and
  • The language is missing a less-than (or other comparison) operator. Add it. Note that the less-than symbol is reserved in Rascal, so you need to escape it: \<.
  • There is syntax for if, but the evaluator (and typechecker) doesn't support it. Add if.
  • Priorities and associativity is wrong: make them left associative and at the same priority

More excitement

In the file src/simpl/Simplifier there's a simple constant folding transformation.

  • Add support for other operators
  • Try it on nested expressions (it won't work!). You need a bottom-up traversal (bottom-up visit).
  • Add support for variables – you should make a recursive simplify function, make it take an environment (e.g., map[ID, int]) as argument and call itself recursively when it encounters a let.

TAC Language

We also have a language for three-address code.

import tac::ThreeAddressCode;
example = parseTACProgram(|project://cocodo/src/tac/example.tac|)

The parseProgram function is shorthand for this:

import ParseTree;
programTop = parse(#start[TacProgram], |project://cocodo/src/tac/example.tac|);
program = programTop.top;

Running the virtual machine

import tac::VirtualMachine;
executeProgram(example, "main", [])

Solutions

  • If evaluator: public Value eval((Expr)if then else end, Env env) = eval( (Int(0) := eval(cond, env)) ? e2 : e1, env );

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published