cmsc 245 wrap up this class is about understanding how
play

CMSC 245 Wrap-up This class is about understanding how programs - PowerPoint PPT Presentation

CMSC 245 Wrap-up This class is about understanding how programs work To do this, were going to have to learn how a computer works Learned a ton in the class Regexp Lexical vs. Dynamic Scoping Closures Parsing Objects Racket Heaps


  1. CMSC 245 Wrap-up

  2. This class is about understanding how programs work

  3. To do this, we’re going to have to learn how a computer works

  4. Learned a ton in the class

  5. Regexp Lexical vs. Dynamic Scoping Closures Parsing Objects Racket Heaps Functions Stacks Method dispatch C++ Classes Garbage collection JS Calling conventions Assembly

  6. To apologize for making you write so much I wrote 732 lines of C++ yesterday

  7. • Today we’re going to design an interpreter • Our source language will be a subset of Scheme • Numbers, variables, if, lambdas, let, begin, set! • We’ll write our own lexer, grammar, and parser • Starting from what you already wrote in labs • Our interpreter will use data structures from the course • And will include garbage collection under the hood

  8. Raw Text Lexer Regex Parser CFG AST C++ (Sub)classes Interpreter Methods on AST

  9. Raw Text Lexer scanner.l ~20 lines of code Parser parser.cc —150 lines of code AST interpreter.h —220 lines of code Interpreter interpreter.cc —160 lines of code

  10. Lesson Sometimes the most best way to do something is to find someone who’s already done it for you…

  11. Symbol Table Garbage Collector

  12. HAMT Boehm GC

  13. HAMT H ash A rray- M apped T rie Think of this is as a hash table that is “quick” to copy Boehm GC High-performance GC for C We’ll use this to make it so our interpreter is automatically garbage collected

  14. We’ll have our hash table use the GC under the hood HAMT Boehm GC So when we put things into HAMT, they are automatically GC’d

  15. The grammar… (In EBNF, allows E+) START -> E $ E -> number E -> identifier E -> ( OP E+ ) E -> ( begin E+ ) E -> ( lambda (ID+) E ) E -> ( set! x E ) E -> ( E+ ) OP --> +|-|*|=

  16. The grammar… (In EBNF, allows E+) START -> E $ Note! Not LL(1)! E -> number E -> identifier E -> ( OP E+ ) E -> ( begin E+ ) E -> ( lambda (ID+) E ) E -> ( set! x E ) E -> ( E+ ) OP --> +|-|*|=

  17. We’ll dig into this AST in a few mins Idea: Represent using subclasses AstNode SetNode FunctionCallNode Variable name Expression Function to call ConstantNode Arguments LambdaNode Number VariableNode IfThenElseNode Arguments Lambda body Variable name Guard Then Else

  18. The lexer… [ \t] { continue; } [\n] { tokenCount++; return NEWLINE; } ";".* { continue; } "(" { tokenCount++; return LPAREN; } ")" { tokenCount++; return RPAREN; } "+" { tokenCount++; return PLUS; } "-" { tokenCount++; return MINUS; } "*" { tokenCount++; return TIMES; } "lambda" { tokenCount++; return LAMBDA; } "let" { tokenCount++; return LET; } "<EOF>" { tokenCount++; return END_OF_INPUT; } -?{digit}+ { tokenCount++; return INT; } {identifier} { tokenCount++; return IDENTIFIER; } . { scannerError(); continue; }

  19. The Parser I started from code we gave you in Lab 5… But I cheated because it’s not LL(1) See parser.cc (5 minute tour)

  20. The Symbol Table Is a dictionary that takes strings to addresses in the heap Means most things are stored on heap Necessitates GC (we’ll discuss next)

  21. The Symbol Table Wrapper for strings Representation of pointers typedef hamt<HashedString, Address> environment; HAMT is a dictionary Two methods: Get: Takes a dictionary and key, gives us address Which we then look up in heap Insert: Takes a dictionary, key, and value Returns a new dictionary

  22. The Heap Stores two possible things: Plain old numbers Closures You could add other things (strings, etc..) To find x , we look up address in symbol table, then use that address to look up through the heap

  23. Symbol tables Wrapper around std::string typedef hamt<HashedString, Address> environment;

  24. Symbol tables Wrapper around std::string typedef hamt<HashedString, Address> environment; Closures struct Closure { AstNode *function; environment *environment; };

  25. Symbol tables Wrapper around std::string typedef hamt<HashedString, Address> environment; Closures struct Closure { AstNode *function; environment *environment; Values }; typedef variant<int, Closure> value; Variant is new in C++17 Container that allows me to store anything from any set of types get<int>(x) // gets the integer value assuming // x is an integer

  26. Symbol tables Wrapper around std::string typedef hamt<HashedString, Address> environment; Closures struct Closure { AstNode *function; environment *environment; Values }; typedef variant<int, Closure> value; Heap hamt<Address, value> *heap = new hamt<Address, value>();

  27. Address *putValueInHeap(value v) { heapSize++; Address* addr = new ((Address*)GC_MALLOC(sizeof(Address))) Address({heapSize}); value * val = new ((value*)GC_MALLOC(sizeof(value))) value(v)); heap = const_cast<hamt<Address, value> *> (heap->insert(addr,val)); return addr; } value getValueFromHeap(Address a) { return *heap->get(&a); }

  28. Address *putValueInHeap(value v) { heapSize++; Address* addr = new ((Address*)GC_MALLOC(sizeof(Address))) Address({heapSize}); value * val = new ((value*)GC_MALLOC(sizeof(value))) value(v)); heap = const_cast<hamt<Address, value> *> (heap->insert(addr,val)); return addr; Tracks the object with GC } value getValueFromHeap(Address a) { return *heap->get(&a); }

  29. Every AstNode implementation has a method execute : symbol table —> value There is a “top level” symbol table where global variables go (top of interpreter.cc )

  30. int main() { while (true) { REPL cout << "> "; AstNode *AST = parseE();; executeToplevelAst(AST); } }; void executeToplevelAst(AstNode *node) { value result = node->execute(globalEnvironment); if (holds_alternative<int>(result)) { cout << get<int>(result) << endl; } else { get<Closure>(result).function->render(); cout << endl; } }

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend