Скачать презентацию The GG Programming Language We give it to Скачать презентацию The GG Programming Language We give it to

148502e6ed605d4362fb8edb463f7c7a.ppt

  • Количество слайдов: 35

The GG Programming Language “We give it to you. ” The GG Programming Language “We give it to you. ”

The Authors u Kierstan Bell • Documentation and Front-end u Elizabeth Mutter • Front-end The Authors u Kierstan Bell • Documentation and Front-end u Elizabeth Mutter • Front-end u Jake Porway • Testing and Front-end u Jonah Tower • Back-end

Language Overview Jonah Language Overview Jonah

Why GG? Why GG?

…it makes things easy! Sockets u I/O u Threads u …it makes things easy! Sockets u I/O u Threads u

Sockets a Simple Client #include #include #include <stdio. h> <stdlib. h> <string. h> <unistd. Sockets a Simple Client #include #include #include /* sklar: need to define socklen_t for cunix; not needed for linux sklar: cunix; */ /* typedef int socklen_t; */ socklen_t; #define TERM 'n' /*----------------------------------sysabort() system error occured; reports error and exits program occured; ---------------------------------*/ void sysabort ( char *m ) { perror( m ); perror( exit( 1 ); } /* end of sysabort() */ sysabort() /*----------------------------------client_sock() creates a socket and then attempts to make a connection to that socket ---------------------------------*/ int client_sock ( int port ) { int ss, optval = 1; ss, struct sockaddr_in sin; /* (1) create an endpoint for communication */ if (( ss = socket( PF_INET, SOCK_STREAM, 0 )) == -1) { sysabort( "client/socket" ); sysabort( } /* (1 a) set socket options */ if ( setsockopt( ss, SOL_SOCKET, SO_REUSEADDR, /* basically allows socket to bind */ /*(const char *)&optval, sizeof(optval)) == -1 *)&optval, sizeof(optval)) ) {*/ &optval, sizeof(optval)) == -1 ) { &optval, sizeof(optval)) sysabort( "client/setsockopt" ); sysabort( "client/setsockopt" } memset( &sin, 0, sizeof( sin )); memset( sizeof( sin_family = AF_INET; sin_addr. s_addr = htonl( INADDR_LOOPBACK ); htonl( sin_port = port;

This is Really Simple /* (2) make a connection to the server socket */ This is Really Simple /* (2) make a connection to the server socket */ if ( connect( ss, (struct sockaddr *)&sin, (socklen_t)sizeof(sin) ) == -1 ) *)&sin, (socklen_t)sizeof(sin) { printf("foobarn"); sysabort( "client/connect" ); sysabort( } /* return the socket descriptor */ return( ss ); } /* end of client_sock() */ client_sock() /*----------------------------------echo_client() ---------------------------------*/ void echo_client ( int port ) { int sock, olen, nwritten, nread, more; olen, nwritten, nread, unsigned char i; char ibuf[64], obuf[64], *p; /* create client socket on port. . . */ printf( "client: port=%dn", port ); printf( port=%dn", port sock = client_sock( port ); client_sock( /* loop, reading input from client and sending it to server */ more = 1; while ( more ) { printf( "enter message to send (q to quit): " ); printf( p = obuf; p = fgets( p, 64, stdin ); fgets( p[strlen(p)-1] = ''; if ( p[0] == 'q' ) { more = 0; p[0] = TERM; p[1] = ''; } else { printf( "client: writing message [%s]n", p ); printf( [%s]n", p i = strlen( p ); strlen( } if (( nwritten = write( sock, &i, sizeof( i ) )) == -1 ) { sizeof( sysabort( "client/write" ); sysabort( } if (( nwritten = write( sock, p, strlen(p) )) == -1 ) { strlen(p) sysabort( "client/write" ); sysabort( } if ( more ) { if (( nread = read( sock, &i, sizeof( i ))) < 0 ) { sizeof( sysabort( "client/read" ); sysabort( } fprintf( stdout, "client: i=%dn", i ); fprintf( stdout, "client: i=%dn", i fflush( stdout ); fflush( if (( nread = read( sock, ibuf, i )) < 0 ) { ibuf, sysabort( "client/read" ); sysabort( } ibuf[nread] = ''; ibuf[nread] if ( ibuf[0] == TERM ) { more = 0; } else { fprintf( stdout, "client: read message [%s]n", ibuf ); fprintf( stdout, "client: [%s]n", ibuf fflush( stdout ); fflush( } } } /* end while more */ /* close socket connection */ close( sock );

…Still going } /* end of echo_client() */ echo_client() /*----------------------------------main() ---------------------------------*/ int main( int …Still going } /* end of echo_client() */ echo_client() /*----------------------------------main() ---------------------------------*/ int main( int argc, char *argv[] ) { argc, *argv[] int port; if ( argc < 2 ) { printf( "usage: client. x n" ); printf( exit( 1 ); } sscanf( argv[1], "%d", &port ); sscanf( echo_client( port ); echo_client( return( 0 ); } /* end of main() */

And now in GG void main(string arg 1, string arg 2){ sockconnect(arg 1, stoi(arg And now in GG void main(string arg 1, string arg 2){ sockconnect(arg 1, stoi(arg 2)); while(1){ send(getline()); print(recv()); } }

File I/O (in Java) File f = new File(“foobar. txt”); File I/O (in Java) File f = new File(“foobar. txt”);

File I/O (in Java) File f = new File(“foobar. txt”); File. Reader fr = File I/O (in Java) File f = new File(“foobar. txt”); File. Reader fr = new File. Reader(f);

File I/O (in Java) File f = new File(“foobar. txt”); File. Reader fr = File I/O (in Java) File f = new File(“foobar. txt”); File. Reader fr = new File. Reader(f); Buffered. Reader in = new Buffered. Reader(fr);

File I/O (in Java) File f = new File(“foobar. txt”); File. Reader fr = File I/O (in Java) File f = new File(“foobar. txt”); File. Reader fr = new File. Reader(f); Buffered. Reader in = new Buffered. Reader(fr); String line = in. read. Line();

…but wait! try{ File f = new File(“foobar. txt”); File. Reader fr = new …but wait! try{ File f = new File(“foobar. txt”); File. Reader fr = new File. Reader(f); Buffered. Reader in = new Buffered. Reader(fr); String line = in. read. Line(); } Catch(IOException e){ System. err. println(“Oh no!”); }

Look at our Cool Version file f = “foobar. txt”; string line = fgetline(f); Look at our Cool Version file f = “foobar. txt”; string line = fgetline(f);

And, GG lets you thread things WOW! And, GG lets you thread things WOW!

Syntax and Semantics Kierstan Syntax and Semantics Kierstan

This is Like C, Baby! u u u Functional language NOT Object Oriented C-like This is Like C, Baby! u u u Functional language NOT Object Oriented C-like syntax int x; void main() { int foo(int i) int z; { x = 10; int y; y = i + 1; return y; } z = foo(x); }

Data Types u boolean b = true; boolean b = false; u char a Data Types u boolean b = true; boolean b = false; u char a = ‘a’; u file my. File = “my. File. txt” u int i = 0; u string hi = “hi”;

Control Flow u while statement while(<boolean>){ statements; } u if if(<boolean>){ statements; } Control Flow u while statement while(){ statements; } u if if(){ statements; }

Built-in Functions u u u u u string fgetline(file f) void fprint(file f, string Built-in Functions u u u u u string fgetline(file f) void fprint(file f, string option, string line) int getint() string getline() string getlocalhost() int get. Time() void print(string line) string recv(int port) void send(file, int port) void send(string line, int port) void socketclose(int port) int socketcreate(int port) int socketconnect(string host, int port) int stoi(string s)

Threaded Functions threaded void func. A(){ print(“a”); } threaded void func. B(){ print(“b”); } Threaded Functions threaded void func. A(){ print(“a”); } threaded void func. B(){ print(“b”); }

The Compiler Elizabeth The Compiler Elizabeth

Lexer/Parser Both written in ANTLR u Lexer u • Parses a GG file into Lexer/Parser Both written in ANTLR u Lexer u • Parses a GG file into Tokens u Parser • Takes the Lexer’s tokens as input • Checks for syntax errors • Creates a tree

Building the Tree For example GG assignment statement: a = my. Func(); Building the Tree For example GG assignment statement: a = my. Func();

Walking the Tree u u Semantic check AND code generation done in one pass Walking the Tree u u Semantic check AND code generation done in one pass Uses ANTLR’s tree walker • Walks the dummy nodes • … but java code does most of the work u Semantic: • Hashtables keep track of : u u u Global variables Local variables Function declarations Keywords Code: • Prints java code to. java files

Code Generation: Java Files u [name-of-file]. java • Runnable java file u u [name-of-file]Main. Code Generation: Java Files u [name-of-file]. java • Runnable java file u u [name-of-file]Main. Method. java • Wrapper class that contains the translated GG code [name-of-function]Threaded. Function. java • Created for each threaded function

Testing and Lessons Learned Jake Testing and Lessons Learned Jake

Standard Regression Testing u No surprises here… • Small modules tested against base cases Standard Regression Testing u No surprises here… • Small modules tested against base cases using script Syntactic tests run with Lexer/Parser u Semantic tests run with Tree Walker u Generated code checks run by hand u

Syntactic Error Checking • Checks syntax in Lexer/Parser • Basic syntactic errors introduced into Syntactic Error Checking • Checks syntax in Lexer/Parser • Basic syntactic errors introduced into correct reference cases • Resulting trees are compared Base Case void main() { int a = 3; (FUNC_DECL void main (BODY (= a 3))) } Error void main() { int a = 3 } “Expected SEMI found CCURLY”

Semantic Error Checking • Only need to check select semantic errors, since most egregious Semantic Error Checking • Only need to check select semantic errors, since most egregious errors are syntactic • Keep log of test results, check for failure void main() { int a = “Not a string”; } Prints to log file: test 1 -typecheck-incorrect. gg Result: “Expected type int but got ‘Not a string’”

Code Correctness • Not done with automated checking • No mortal should have to Code Correctness • Not done with automated checking • No mortal should have to hand-generate the Java base case for a client/server program • Instead, functionality is checked thoroughly, not code

Lessons Learned! Roles are great, but don’t be afraid to “diversify” u Meetings, meetings, Lessons Learned! Roles are great, but don’t be afraid to “diversify” u Meetings, meetings, and more meetings! There’s comfort in consistency u Clairvoyance – Who knew ANTLR would be so hard u Ctrl-1 -0 -0 – Who put this in Emacs? u

THE END THE END