ezgraphs a graphs and charts generating language coms w4115: programming languages and translators...

16
EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrev [email protected] Edlira Kumbarce [email protected]

Post on 21-Dec-2015

216 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

EZGraphs A graphs and charts generating language

COMS W4115: Programming Languages and Translators

Spring 2007

Vincent Dobrev [email protected] Edlira Kumbarce [email protected]

Page 2: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

IntroductionIntroduction

• Why graphs?Why graphs?Graphs are used widely. They communicate Graphs are used widely. They communicate information more easily and efficiently than information more easily and efficiently than words or tables.words or tables.

• ObjectiveObjectiveProvide a language that can be used to create Provide a language that can be used to create charts and graphs, targeting those with a little charts and graphs, targeting those with a little prior programming experience.prior programming experience.

Page 3: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

FeaturesFeatures

• User-Friendly SyntaxUser-Friendly SyntaxVery similar to C and Java. Intuitive keywords and Very similar to C and Java. Intuitive keywords and internal function names. More organized than internal function names. More organized than Ploticus.Ploticus.

• Easy DebuggingEasy DebuggingNon-cryptic and informative error messages make Non-cryptic and informative error messages make it easy to use and debug. User given exact it easy to use and debug. User given exact location of error with file name and line and location of error with file name and line and column numbers.column numbers.

Page 4: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

Features Features (cont’d)(cont’d)

• PortabilityPortabilityBased on Java, so it only depends on the Based on Java, so it only depends on the presence of the Java Runtime Environment.presence of the Java Runtime Environment.

• Data Types & OperatorsData Types & OperatorsSupports boolean, integer, floating-point, and Supports boolean, integer, floating-point, and string data types, as well as multi-dimensional string data types, as well as multi-dimensional arrays.arrays.

Page 5: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

FeaturesFeatures (cont’d)(cont’d)

• File InclusionFile InclusionAllows code reuse and modularity. More Allows code reuse and modularity. More organized programs.organized programs.

• Control Flow StatementsControl Flow StatementsSupports conditionals (if-then-else) and iterative Supports conditionals (if-then-else) and iterative statements (loops), which in turn enable user to statements (loops), which in turn enable user to create recursive functions.create recursive functions.

Page 6: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

FeaturesFeatures (cont’d)(cont’d)

• Pre-Defined FunctionsPre-Defined Functions

Drawing: Drawing: point(), line(), polygon(), etcpoint(), line(), polygon(), etc..

Transfromation: Transfromation: translate(), scale(), shear() etc.translate(), scale(), shear() etc.

Data Acquiring: Data Acquiring: data()data()

Math: Math: exp(), log(), pow(), sqrt(), etc.exp(), log(), pow(), sqrt(), etc.

Auxiliary: Auxiliary: strToInt(), strToFloat(), substring(), size(), etc.strToInt(), strToFloat(), substring(), size(), etc.

Output: Output: print(), println() , save(), show()print(), println() , save(), show()

Page 7: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

Example 1 - Example 1 - RecursionRecursion/* * 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 xp1 = (x2+x1) / 2; int yp1 = (y2+y1) / 2; int xp2 = (x3+x2) / 2; int yp2 = (y3+y2) / 2; int xp3 = (x1+x3) / 2; int yp3 = (y1+y3) / 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); }}

Page 8: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

Example 2 - A Pie Chart 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;

...

Page 9: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

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();}

Example 2 - A Pie Chart Example 2 - A Pie Chart (cont’d)(cont’d)

Output:

Page 10: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

Language ImplementationLanguage Implementation

• The source code is parsed and executed right The source code is parsed and executed right away with no intermediate code.away with no intermediate code.

• Programs reside in .ezg files, with one file Programs reside in .ezg files, with one file containing a main() function and any number of containing a main() function and any number of other .ezg files containing other functions.other .ezg files containing other functions.

Page 11: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

Language Implementation Language Implementation (cont’d)(cont’d)

• Three types of output:Three types of output:1.1. Text in the consoleText in the console2.2. An image in a window on-screenAn image in a window on-screen3.3. An image in a fileAn image in a file

• Exception handling mechanism catches, formats, Exception handling mechanism catches, formats, and prints out error messages for the user.and prints out error messages for the user.Examples:Examples:

1.1. Error: draw.ezg:48:10: expecting ID, found '=‘Error: draw.ezg:48:10: expecting ID, found '=‘2.2. Error: draw.ezg:59:6: unexpected data type being assigned to arrayError: draw.ezg:59:6: unexpected data type being assigned to array3.3. Error: recursion.ezg:5:5: function fact(int) expected to return intError: recursion.ezg:5:5: function fact(int) expected to return int4.4. Error: recursion.ezg:7:16: variable g not declaredError: recursion.ezg:7:16: variable g not declared

Page 12: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

ArchitectureArchitecture

Lexer

Parser

Walker

.ezg source

files

Exception Handler

Types

Interpreter

Output

datafile

Tokens

EzgAST

Front End Back End

Page 13: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

Interpreter StructureInterpreter Structure

EzgInterpreter

EzgSymbolTable

EzgFunction

EzgStringTokenizer

EzgInternalFunctions

Console Output

datafile

EzgPainterScreen Output (image)

.png(image)

Page 14: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

Type SystemType System

EzgType

EzgVoidEzgStringEzgIntEzgArray EzgBool EzgFloat

Base type class

Type classes

Page 15: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

Type Conversions & Type Conversions & OperatorsOperators

Page 16: EZGraphs A graphs and charts generating language COMS W4115: Programming Languages and Translators Spring 2007 Vincent Dobrevvd2006@columbia.edu Edlira

Type Conversions & Type Conversions & Operators Operators (cont’d)(cont’d)