diff --git a/exercises/02/ps2.pdf b/exercises/02/ps2.pdf new file mode 100644 index 0000000..1d7a9e0 Binary files /dev/null and b/exercises/02/ps2.pdf differ diff --git a/exercises/02/ps2_recitation.pdf b/exercises/02/ps2_recitation.pdf new file mode 100644 index 0000000..4e3d1ff Binary files /dev/null and b/exercises/02/ps2_recitation.pdf differ diff --git a/exercises/02/ps2_skeleton.tar.gz b/exercises/02/ps2_skeleton.tar.gz new file mode 100644 index 0000000..b2e3c1e Binary files /dev/null and b/exercises/02/ps2_skeleton.tar.gz differ diff --git a/exercises/02/vslc/Makefile b/exercises/02/vslc/Makefile new file mode 100644 index 0000000..5af0ab0 --- /dev/null +++ b/exercises/02/vslc/Makefile @@ -0,0 +1,12 @@ +LEX=flex +YACC=bison +YFLAGS+=--defines=src/y.tab.h -o y.tab.c +CFLAGS+=-std=c99 -g -Isrc -Iinclude -D_POSIX_C_SOURCE=200809L -DYYSTYPE="node_t *" + +src/vslc: src/vslc.c src/parser.o src/scanner.o src/nodetypes.o src/tree.o +src/y.tab.h: src/parser.c +src/scanner.c: src/y.tab.h src/scanner.l +clean: + -rm -f src/parser.c src/scanner.c src/*.tab.* src/*.o +purge: clean + -rm -f src/vslc diff --git a/exercises/02/vslc/include/nodetypes.h b/exercises/02/vslc/include/nodetypes.h new file mode 100644 index 0000000..5d302cc --- /dev/null +++ b/exercises/02/vslc/include/nodetypes.h @@ -0,0 +1,37 @@ +#ifndef NODETYPES_H +#define NODETYPES_H +typedef enum { + PROGRAM, + GLOBAL_LIST, + GLOBAL, + STATEMENT_LIST, + PRINT_LIST, + EXPRESSION_LIST, + VARIABLE_LIST, + ARGUMENT_LIST, + PARAMETER_LIST, + DECLARATION_LIST, + FUNCTION, + STATEMENT, + BLOCK, + ASSIGNMENT_STATEMENT, + ADD_STATEMENT, + SUBTRACT_STATEMENT, + MULTIPLY_STATEMENT, + DIVIDE_STATEMENT, + RETURN_STATEMENT, + PRINT_STATEMENT, + NULL_STATEMENT, + IF_STATEMENT, + WHILE_STATEMENT, + EXPRESSION, + RELATION, + DECLARATION, + PRINT_ITEM, + IDENTIFIER_DATA, + NUMBER_DATA, + STRING_DATA +} node_index_t; + +extern char *node_string[26]; +#endif diff --git a/exercises/02/vslc/src/nodetypes.c b/exercises/02/vslc/src/nodetypes.c new file mode 100644 index 0000000..dd90315 --- /dev/null +++ b/exercises/02/vslc/src/nodetypes.c @@ -0,0 +1,34 @@ +#define STRING(x) #x +char *node_string[30] = { + STRING(PROGRAM), + STRING(GLOBAL_LIST), + STRING(GLOBAL), + STRING(STATEMENT_LIST), + STRING(PRINT_LIST), + STRING(EXPRESSION_LIST), + STRING(VARIABLE_LIST), + STRING(ARGUMENT_LIST), + STRING(PARAMETER_LIST), + STRING(DECLARATION_LIST), + STRING(FUNCTION), + STRING(STATEMENT), + STRING(BLOCK), + STRING(ASSIGNMENT_STATEMENT), + STRING(ADD_STATEMENT), + STRING(SUBTRACT_STATEMENT), + STRING(MULTIPLY_STATEMENT), + STRING(DIVIDE_STATEMENT), + STRING(RETURN_STATEMENT), + STRING(PRINT_STATEMENT), + STRING(NULL_STATEMENT), + STRING(IF_STATEMENT), + STRING(WHILE_STATEMENT), + STRING(EXPRESSION), + STRING(RELATION), + STRING(DECLARATION), + STRING(PRINT_ITEM), + STRING(IDENTIFIER_DATA), + STRING(NUMBER_DATA), + STRING(STRING_DATA) +}; +#undef STRING diff --git a/exercises/02/vslc/vsl_programs/Makefile b/exercises/02/vslc/vsl_programs/Makefile new file mode 100644 index 0000000..4565b2b --- /dev/null +++ b/exercises/02/vslc/vsl_programs/Makefile @@ -0,0 +1,12 @@ +VSLC := ../src/vslc + +PS2_EXAMPLES := $(patsubst ps2-parser/%.vsl, ps2-parser/%.ast, $(wildcard ps2-parser/*.vsl)) + +all: $(PS2_EXAMPLES) + echo $(PS2_EXAMPLES) + +%.ast: %.vsl + $(VSLC) -t < $^ > $@ + +clean: + -rm -r */*.ast \ No newline at end of file diff --git a/exercises/02/vslc/vsl_programs/ps2-parser/assignments.vsl b/exercises/02/vslc/vsl_programs/ps2-parser/assignments.vsl new file mode 100644 index 0000000..ea98aad --- /dev/null +++ b/exercises/02/vslc/vsl_programs/ps2-parser/assignments.vsl @@ -0,0 +1,14 @@ +// checking that comments are ignored + +// This program checks the assignment operators + +func main() +begin + var a + a := 3 + a += 1 + a /= 2 + a *= 32 + a -= 2 + print a +end \ No newline at end of file diff --git a/exercises/02/vslc/vsl_programs/ps2-parser/function_call.vsl b/exercises/02/vslc/vsl_programs/ps2-parser/function_call.vsl new file mode 100644 index 0000000..d5c75a5 --- /dev/null +++ b/exercises/02/vslc/vsl_programs/ps2-parser/function_call.vsl @@ -0,0 +1,8 @@ +func add(a, b) begin + return a + b +end + +func main() +begin + print add(40, 2) +end \ No newline at end of file diff --git a/exercises/02/vslc/vsl_programs/ps2-parser/helloworld.vsl b/exercises/02/vslc/vsl_programs/ps2-parser/helloworld.vsl new file mode 100644 index 0000000..49219f8 --- /dev/null +++ b/exercises/02/vslc/vsl_programs/ps2-parser/helloworld.vsl @@ -0,0 +1,3 @@ +func main() begin + print "Hello, World!" +end \ No newline at end of file diff --git a/exercises/02/vslc/vsl_programs/ps2-parser/if_else.vsl b/exercises/02/vslc/vsl_programs/ps2-parser/if_else.vsl new file mode 100644 index 0000000..7d4fbe3 --- /dev/null +++ b/exercises/02/vslc/vsl_programs/ps2-parser/if_else.vsl @@ -0,0 +1,25 @@ +func main() +begin + var a, b, c, d + c := 1 + a := 3 + b := a + c // 4 + d := a * 100 + 50 + print "a", a + print "b", b + print "c", c + print "d", d + if a = 14 then + print 1, "N", d / 5 + a, "RPR", a, "TERS " + else + print "COMP", c, "L", "ERS " + + print b, "R", a, " " + + if a < b then + if d > 42 then + print b, "W", d, "ME" + else + print "L", b, "M", c + // A dangling else, what could go wrong? +end diff --git a/exercises/02/vslc/vsl_programs/ps2-parser/variables.vsl b/exercises/02/vslc/vsl_programs/ps2-parser/variables.vsl new file mode 100644 index 0000000..abd12ec --- /dev/null +++ b/exercises/02/vslc/vsl_programs/ps2-parser/variables.vsl @@ -0,0 +1,19 @@ + +var global_var + +func my_func(param) +begin + var local_var, local_var2 + local_var := 1 +end + +var glob1, glob2 + +func main() +begin + var main_local_var + begin + var main_local_nested_var + main_local_nested_var := main_local_var + end +end \ No newline at end of file diff --git a/exercises/02/vslc/vsl_programs/ps2-parser/while.vsl b/exercises/02/vslc/vsl_programs/ps2-parser/while.vsl new file mode 100644 index 0000000..8d6b024 --- /dev/null +++ b/exercises/02/vslc/vsl_programs/ps2-parser/while.vsl @@ -0,0 +1,10 @@ +// check parsing of do-while loop + +func main() +begin + var i + i := 2 + while i < 9000 do + i := i * i + print i +end \ No newline at end of file