diff --git a/Makefile b/Makefile index 07d1d87..256aad2 100644 --- a/Makefile +++ b/Makefile @@ -3,9 +3,12 @@ FLEX := flex LEX := src/lexicalStructure.lex EXE := alpha CFLAGS := -YACC := bison +YACC := bison -compiler: runner +TESTS-S1 := $(wildcard tests/sprint1/test/*.alpha) +TESTS-S2 := $(wildcard tests/sprint2/test/*.alpha) + +compiler: clean runner tmp/grammar.tab.c: src/grammar.y mkdir -p tmp @@ -35,25 +38,14 @@ debug: clean compiler test: test-s1 test-s2 test-s1: - ./$(EXE) -tok ./tests/sprint1/test_comment_fix1.alpha - ./$(EXE) -tok ./tests/sprint1/test_comment_fix2.alpha - ./$(EXE) -tok ./tests/sprint1/test_comment_issues.alpha - ./$(EXE) -tok ./tests/sprint1/test_comments.alpha - ./$(EXE) -tok ./tests/sprint1/test_general_token.alpha - ./$(EXE) -tok ./tests/sprint1/test_keywords.alpha - ./$(EXE) -tok ./tests/sprint1/test_operators.alpha - ./$(EXE) -tok ./tests/sprint1/test_other_punc.alpha - ./$(EXE) -tok ./tests/sprint1/test_punc_grouping.alpha - ./$(EXE) -tok ./tests/sprint1/test_real_alpha_file1.alpha - ./$(EXE) -tok ./tests/sprint1/test_real_alpha_file2.alpha - ./$(EXE) -tok ./tests/sprint1/test_simple_int.alpha - ./$(EXE) -tok ./tests/sprint1/test_simple_literals.alpha - ./$(EXE) -tok ./tests/sprint1/test_variables.alpha + chmod +x ./check.sh + $(foreach test, $(TESTS-S1), ./$(EXE) -tok $(test);) + ./check.sh test-s2: - ./$(EXE) -tok ./tests/sprint2/alpha/test_library.alpha - ./$(EXE) -tok ./tests/sprint2/alpha/test_one_line.alpha - ./$(EXE) -tok ./tests/sprint2/alpha/test_simple.alpha + chmod +x ./check.sh + $(foreach test, $(TESTS-S2), ./$(EXE) -tok $(test);) + ./check.sh clean: rm -f *.o diff --git a/check.sh b/check.sh new file mode 100755 index 0000000..b8b138c --- /dev/null +++ b/check.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +# Diff-Check Tool # +# Checks if outputed TOK = exp # +# The Translators - Spring 2025 # + +TOK_DIR="out" + +if [[ ! -d "$TOK_DIR" ]]; then + echo "Directory $TOK_DIR does not exist." + exit 1 +fi + +echo -e "\n" +for file in "$TOK_DIR"/*; do + filename=$(basename -- "$file") + filename="${filename%.*}" + num=${filename:2:1} + exp="./tests/sprint$num/expected/$filename.expected" + + if [[ -f "$exp" ]]; then + echo -e "--------------------------------------------" + echo -e "Checking $file...\n" + diff --color=always "$file" "$exp" + echo -e "--------------------------------------------\n" + fi +done \ No newline at end of file diff --git a/src/grammar.y b/src/grammar.y index 0645a4a..db116e7 100644 --- a/src/grammar.y +++ b/src/grammar.y @@ -1,5 +1,5 @@ -/* Syntax Analyzer with Bison (3.8.1) */ -/* (referenced Bison manual for file boilerplate [3.1]) */ +/* Syntax Analyzer with Bison (3.8.1) */ +/* The Translators - Spring 2025 */ // Prologue %{ diff --git a/src/lexicalStructure.lex b/src/lexicalStructure.lex index 79ccaea..7a5f113 100644 --- a/src/lexicalStructure.lex +++ b/src/lexicalStructure.lex @@ -1,15 +1,17 @@ -/* Lexical Analyzer with Flex (1.6.0) */ +/* Lexical Analyzer with Flex (1.6.0) */ +/* The Translators - Spring 2025 */ %option noyywrap %option header-file="flex.h" + %{ #include - //#include "typedefs.h" #include "grammar.tab.h" - int line_number = 1, column_number = 1; #ifndef DEBUG #define DEBUG 0 #endif + + int line_number = 1, column_number = 1; %} STARCOM [^\*]|\*+[^\)\*]+ diff --git a/src/runner.c b/src/runner.c index 49f50f8..7ee5d58 100644 --- a/src/runner.c +++ b/src/runner.c @@ -1,37 +1,32 @@ -// #include "symbol_table.h" +/* Runner File - Compiles alpha Compiler */ +/* The Translators - Spring 2025 */ + #include "runner.h" int main(int argc, char *argv[]) { if (argc == 1) { fprintf( - stderr, - "INVALID INPUT: Include a .alpha file or use -help for more inputs \n"); + stderr, INVALID); return -1; - } else if (argc == 2) { - // can be either -help or .alpha file + } + + else if (argc == 2) { if (is_help(argv[1])) { return 0; } else if (is_alpha_file(argv[1], strlen(argv[1])) == 0) { - // run flex for now - no_flag = SET_FLAG; // no argument but valid input + no_flag = SET_FLAG; alpha_file = fopen(argv[1], "r"); - } else { - fprintf(stderr, - "INVALID INPUT: Include a .alpha file or use -help for more " - "inputs\n"); + fprintf(stderr, INVALID); return -1; } - } else { - // last input must be .alpha + } + + else { if (is_alpha_file(argv[argc - 1], strlen(argv[argc - 1])) != 0) { - fprintf(stderr, - "INVALID INPUT: Include a .alpha file at end of input or use " - "-help for more inputs \n"); + fprintf(stderr, INVALID); return -1; } else { - // now check that other args are valid (flags will not be null if flag is - // present) for (int i = 1; i < argc - 1; i++) { if (check_flag(argv[i], argv[argc - 1]) != 0) { fprintf(stderr, "INVALID FLAG(S): Use -help to view valid inputs \n"); @@ -117,6 +112,7 @@ bool is_help(char *input) { printf("%s", HELP); return true; } + return false; } diff --git a/src/runner.h b/src/runner.h index 7c0d83a..2c516bf 100644 --- a/src/runner.h +++ b/src/runner.h @@ -1,3 +1,6 @@ +/* Runner File - Compiles alpha Compiler */ +/* The Translators - Spring 2025 */ + #define ALPHA_OFFSET 6 #define TOK_LEN 3 #define ST_LEN 2 @@ -7,8 +10,9 @@ "number for each of the tokens to the .tok file\n-st output the symbol " \ "table for the program to the .st file\n-help print this message and exit " \ "the alpha compiler\n" -// use to set flags for arg types -#define SET_FLAG 1 +#define SET_FLAG 1 // Used to set flags for arg types +#define INVALID \ + "INVALID INPUT: Include a .alpha file or use -help for more inputs \n" #include #include diff --git a/tests/sprint1/expected/sp1_comment_fix1.expected b/tests/sprint1/expected/sp1_comment_fix1.expected new file mode 100644 index 0000000..07d66e1 --- /dev/null +++ b/tests/sprint1/expected/sp1_comment_fix1.expected @@ -0,0 +1 @@ + daskmskdfm \ No newline at end of file diff --git a/tests/sprint1/test_comment_fix1.alpha b/tests/sprint1/test/sp1_comment_fix1.alpha similarity index 100% rename from tests/sprint1/test_comment_fix1.alpha rename to tests/sprint1/test/sp1_comment_fix1.alpha diff --git a/tests/sprint1/test_comment_fix2.alpha b/tests/sprint1/test/sp1_comment_fix2.alpha similarity index 100% rename from tests/sprint1/test_comment_fix2.alpha rename to tests/sprint1/test/sp1_comment_fix2.alpha diff --git a/tests/sprint1/test_comment_issues.alpha b/tests/sprint1/test/sp1_comment_issues.alpha similarity index 100% rename from tests/sprint1/test_comment_issues.alpha rename to tests/sprint1/test/sp1_comment_issues.alpha diff --git a/tests/sprint1/test_comments.alpha b/tests/sprint1/test/sp1_comments.alpha similarity index 100% rename from tests/sprint1/test_comments.alpha rename to tests/sprint1/test/sp1_comments.alpha diff --git a/tests/sprint1/test_general_token.alpha b/tests/sprint1/test/sp1_general_token.alpha similarity index 100% rename from tests/sprint1/test_general_token.alpha rename to tests/sprint1/test/sp1_general_token.alpha diff --git a/tests/sprint1/test_keywords.alpha b/tests/sprint1/test/sp1_keywords.alpha similarity index 100% rename from tests/sprint1/test_keywords.alpha rename to tests/sprint1/test/sp1_keywords.alpha diff --git a/tests/sprint1/test_operators.alpha b/tests/sprint1/test/sp1_operators.alpha similarity index 100% rename from tests/sprint1/test_operators.alpha rename to tests/sprint1/test/sp1_operators.alpha diff --git a/tests/sprint1/test_other_punc.alpha b/tests/sprint1/test/sp1_other_punc.alpha similarity index 100% rename from tests/sprint1/test_other_punc.alpha rename to tests/sprint1/test/sp1_other_punc.alpha diff --git a/tests/sprint1/test_punc_grouping.alpha b/tests/sprint1/test/sp1_punc_grouping.alpha similarity index 100% rename from tests/sprint1/test_punc_grouping.alpha rename to tests/sprint1/test/sp1_punc_grouping.alpha diff --git a/tests/sprint1/test_real_alpha_file1.alpha b/tests/sprint1/test/sp1_real_alpha_file1.alpha similarity index 100% rename from tests/sprint1/test_real_alpha_file1.alpha rename to tests/sprint1/test/sp1_real_alpha_file1.alpha diff --git a/tests/sprint1/test_real_alpha_file2.alpha b/tests/sprint1/test/sp1_real_alpha_file2.alpha similarity index 100% rename from tests/sprint1/test_real_alpha_file2.alpha rename to tests/sprint1/test/sp1_real_alpha_file2.alpha diff --git a/tests/sprint1/test_simple_int.alpha b/tests/sprint1/test/sp1_simple_int.alpha similarity index 100% rename from tests/sprint1/test_simple_int.alpha rename to tests/sprint1/test/sp1_simple_int.alpha diff --git a/tests/sprint1/test_simple_literals.alpha b/tests/sprint1/test/sp1_simple_literals.alpha similarity index 100% rename from tests/sprint1/test_simple_literals.alpha rename to tests/sprint1/test/sp1_simple_literals.alpha diff --git a/tests/sprint1/test_variables.alpha b/tests/sprint1/test/sp1_variables.alpha similarity index 100% rename from tests/sprint1/test_variables.alpha rename to tests/sprint1/test/sp1_variables.alpha diff --git a/tests/sprint2/test/sp2_library.alpha b/tests/sprint2/test/sp2_library.alpha new file mode 100644 index 0000000..9bacd6c --- /dev/null +++ b/tests/sprint2/test/sp2_library.alpha @@ -0,0 +1,30 @@ +(* At compiler start-up your program should create symbol table entries for the four primitive types: + Boolean (1 byte) + character (1 byte) + integer (4 bytes) + address (8 bytes) +You should #include this file at the start of your alpha file. +Some useful types are defined below. +*) +type string: 1 -> character +type BooleanXBoolean: [Boolean: x, y] +type characterXcharacter: [character: x, y] +type integerXinteger: [integer: x, y] + +type Boolean2Boolean: Boolean -> Boolean +type integer2integer: integer -> integer +type character2integer: character -> integer +type Boolean2integer: Boolean -> integer +type string2integer: string -> integer +type integerXinteger2integer: integerXinteger -> integer +type integerXinteger2Boolean: integerXinteger -> Boolean +type characterXcharacter2Boolean: characterXcharacter -> Boolean +type BooleanXBoolean2Boolean: BooleanXBoolean -> Boolean +type integer2address: integer -> address +type address2integer: address -> integer +external function printInteger: integer2integer +external function printCharacter: character2integer +external function printBoolean: Boolean2integer +external function reserve: integer2address +external function release: address2integer +function entry: string2integer diff --git a/tests/sprint2/test/sp2_one_line.alpha b/tests/sprint2/test/sp2_one_line.alpha new file mode 100644 index 0000000..01f115e --- /dev/null +++ b/tests/sprint2/test/sp2_one_line.alpha @@ -0,0 +1 @@ +type rec: [integer: x; integer: y] type T1: integer -> integer type T2: rec -> integer function foo : T1 function bar1 : T2 function bar2 : T2 foo(x) := { return x * x; } bar1(a) := { return a.x * a.y; } bar2 as (r,s) := { return r * s; } entry(arg) := { [ integer: result ; rec: w] result := foo(5); w := reserve(w); w.x := 5; w.y := 7; result := bar1(w); result := bar2(5,7); return 0; } diff --git a/tests/sprint2/test/sp2_simple.alpha b/tests/sprint2/test/sp2_simple.alpha new file mode 100644 index 0000000..13f3c9f --- /dev/null +++ b/tests/sprint2/test/sp2_simple.alpha @@ -0,0 +1,4 @@ +#include "library.alpha" +entry(arg) := { + return 0; +}