EZGraphs A graphs and charts generating language Vincent Dobrev Edlira Kumbarce vd2006@columbia.edu ek2248@columbia.edu COMS W4115: Programming Languages and Translators Spring 2007 Introduction • Why graphs? Graphs are used widely. They communicate information more easily and efficiently than words or tables. • Objective Provide a language that can be used to create charts and graphs, targeting those with a little prior programming experience. Features • User-Friendly Syntax Very similar to C and Java. Intuitive keywords and internal function names. More organized than Ploticus. • Easy Debugging Non-cryptic and informative error messages make it easy to use and debug. User given exact location of error with file name and line and column numbers. Features (cont’d) • Portability Based on Java, so it only depends on the presence of the Java Runtime Environment. • Data Types & Operators Supports boolean, integer, floating-point, and string data types, as well as multi-dimensional arrays. Features • (cont’d) File Inclusion Allows code reuse and modularity. More organized programs. • Control Flow Statements Supports conditionals (if-then-else) and iterative statements (loops), which in turn enable user to create recursive functions. Features (cont’d) • Pre-Defined Functions Drawing: point(), line(), polygon(), etc. Transfromation: translate(), scale(), shear() etc. Data Acquiring: data() Math: exp(), log(), pow(), sqrt(), etc. Auxiliary: strToInt(), strToFloat(), substring(), size(), etc. Output: print(), println() , save(), show() Example 1 - Recursion /* * Sierpinski Triangle. */ void main() { canvas(600, 600); scale(1, -1); translate(0, -600); triangles(50, 50, 550, 50, 300, 550, 1); show(); } void triangles(int x1, int y1, int x2, int y2, int x3, int y3, int level) { line(x1, y1, x2, y2); line(x2, y2, x3, y3); line(x3, y3, x1, y1); int int int int int int xp1 yp1 xp2 yp2 xp3 yp3 = = = = = = (x2+x1) (y2+y1) (x3+x2) (y3+y2) (x1+x3) (y1+y3) / / / / / / 2; 2; 2; 2; 2; 2; if (level < 8) { triangles(x1, y1, xp1, yp1, xp3, yp3, level+1); triangles(xp1, yp1, x2, y2, xp2, yp2, level+1); triangles(xp3, yp3, xp2, yp2, x3, y3, level+1); } } Example 2 - A Pie Chart /* * A pie chart illustrating distribution of income. */ void main() { int fields = 7; /* Colors. */ int[][] c = new int[fields][3]; c[0][0] = 255; c[0][1] = 50; c[0][2] = 0; c[1][0] = 0; c[1][1] = 255; c[1][2] = 0; ... c[6][0] = 255; c[6][1] = 255; c[6][2] = 0; /* Labels. */ string[] l = new string[fields]; l[0] = "Savings"; l[1] = "Insurance"; ... l[6] = "Housing"; /* Percentages. */ int[] p = new int[fields]; p[0] = 9; p[1] = 11; ... p[6] = 21; ... Example 2 - A Pie Chart (cont’d) canvas(400,400); background(244,244,244); string title = "Income Distribution"; font("Arial", 0, 20); int width = width(title); text(title, 200 - width/2, 40); stroke(2); line(50,50,350,50); translate(100,100); int start = 0, end = 0; for (int i = 0; i < fields; i++) { color(c[i][0],c[i][1],c[i][2]); start += end; end = p[i] * 360 / 100; fillArc(0,0,200,200,start,end); } show(); } Output: Language Implementation • The source code is parsed and executed right away with no intermediate code. • Programs reside in .ezg files, with one file containing a main() function and any number of other .ezg files containing other functions. Language Implementation (cont’d) • Three types of output: 1. Text in the console 2. An image in a window on-screen 3. An image in a file • Exception handling mechanism catches, formats, and prints out error messages for the user. Examples: 1. 2. 3. 4. Error: Error: Error: Error: draw.ezg:48:10: expecting ID, found '=‘ draw.ezg:59:6: unexpected data type being assigned to array recursion.ezg:5:5: function fact(int) expected to return int recursion.ezg:7:16: variable g not declared Architecture .ezg source files Lexer Exception Handler Output Tokens Parser EzgAST Types Walker Front End Interpreter Back End data file Interpreter Structure EzgSymbolTable EzgInterpreter EzgFunction EzgStringTokenizer data file EzgInternalFunctions Console Output Screen Output (image) EzgPainter .png (image) Type System Base type class EzgArray Type classes EzgBool EzgType EzgFloat EzgInt EzgString EzgVoid Type Conversions & Operators Type Conversions & Operators (cont’d)