120 likes | 277 Vues
ASCL – Another Simple Calculator Language. Ryan Blace Spring 2007. Outline. Review of requirements Quick introduction to ASCL Features and examples Design and implementation details References that were used in development Conclusion. Review of Requirements. Floating point calculations
E N D
ASCL – Another Simple Calculator Language Ryan Blace Spring 2007
Outline • Review of requirements • Quick introduction to ASCL • Features and examples • Design and implementation details • References that were used in development • Conclusion
Review of Requirements • Floating point calculations • Mathematical ad boolean expressions • Number storage and recall • Input and output of numbers and strings
Quick Intro to ASCL Global variable declarations int $globalInt; string $globalString; Function declarations void #foo() { int $localInt = 5; $globalInt = $localInt; } Entry point entry() { string $text = “hi”; #foo(); if($globalInt == 5) { print($text); } }
Datatypes : boolean, integer, double, string Variables Global and local Local overriding allowed Scoping by function and block Simple methods Support recursion Each method has its own local variables No parameters or return vals If-then and If-then-else While() {} loops Input and output functions Operators Equality/inequality/comparison Boolean and, or, xor, not Positive and negative unary qualifiers All common arithmetic operators Trigonometric functions Square root, exponents Logarithms Factorial ASCL Features
Design and Implementation • Language parser that builds an abstract syntax tree • Tree parser that implements the language and executes the program • Abstract Value class • Extend Value to make IntValue, DoubleValue, StringValue, BooleanValue • Operators are functions on the class • Polymorphism handles routing method calls for specific data types • Methods, flow control, and looping all enabled using tree node indexes and seek() function on the TreeNodeStream object in the ANTLR tree grammar
Design and Implementation • Global variables stored in hash map • Function id’s and code block tree node indexes stored in hash map • Manage a stack of “Function Contexts” • Each time we enter a new function, push • Each time we exit a function, pop • Function context contains a stack of “Block Contexts” • Same as before • Variable references are resolved by traversing the block context stack of the current function context from top to bottom. If none found, check the global variable hash.
Helpful References • Definitive ANTLR Reference • If you need to work with ANTLR again, buy the online book. It was only $30 or so... Totally worth it. • www.antlr.org • Contains a lot of useful info if you dig deep enough • Antlr-interest email list • www.antlr.org:8080/pipermail/antlr-interest • Tons of good discussions on the new features of ANTLR 3.0
Conclusions… ASCL is a very simple and quick to learn language It takes very little programming to do very complex things It provides a programmer with everything needed to build a calculator like the MS application (except the UI stuff) Future work… Methods with parameters and return types Function overloading Structs and maybe classes For loops Exception handling (yikes!) Conclusion and Future Work