Merge pull request #43 from UB-CSE443/Dev

Getting header info
This commit is contained in:
Moroseui
2025-04-04 21:35:24 -04:00
committed by GitHub
42 changed files with 867 additions and 460 deletions

View File

@ -37,7 +37,7 @@ debug: clean compiler
test:
chmod +x ./check.sh
chmod +x ./test.sh
$(foreach test, $(TESTS-S1), (./$(EXE) -tok $(test) -debug || true);)
$(foreach test, $(TESTS-S1), (./$(EXE) -tok -debug $(test) || true);)
./test.sh sp2
./test.sh sp3
./test.sh sp4
@ -46,7 +46,7 @@ test:
test-s1:
chmod +x ./check.sh
chmod +x ./test.sh
$(foreach test, $(TESTS-S1), (./$(EXE) -tok $(test) -debug || true);)
$(foreach test, $(TESTS-S1), (./$(EXE) -tok -debug $(test) || true);)
./check.sh sp1
test-s2:

View File

@ -11,35 +11,16 @@
// 4⃣ Rule end-markers (;, |) should always be 4 spaces in.
// 5⃣ C-blocks should always be clearly defined and follow clang formatting rules.
// 6⃣ 1-line if/for/while statements must be wrapped in curly braces.
// 7DO NOT USE TABS. EVER.
// 7Comments should always be above rules
// 8⃣ DO NOT USE TABS. EVER.
// Please ask Scarlett if you are unsure of how to format something. Thanks! 😀
%{
#include <stdio.h>
#include "../src/symbol_table.c"
#include <math.h>
extern int yylex(void);
void yyerror(const char *err);
extern char* yytext;
extern int yyleng;
extern int yychar;
extern SymbolTable * cur;
//char* cur_value;
//char* cur_type;
int token_tracker;
extern int line_number;
extern int column_number;
extern FILE * yyin;
extern TableNode* funprime;
extern TableNode* arrayprim;
extern TableNode* recprime;
extern TableNode* funtypeprime;
extern TableNode* integ;
extern TableNode* addr;
extern TableNode* chara;
extern TableNode* stri;
extern TableNode* boo;
TableNode * tn;
%}
@ -144,6 +125,7 @@ prototype:
definition:
TYPE ID COLON
{
printdebug("Currently see a record definition for %s", $2);
tn = CreateEntry(getAncestor(cur),TYPE_RECORD_TYPE, recprime, $2, CreateRecordInfo(0, cur = CreateScope(cur, 0, 0)));
if (look_up(cur, $2) == undefined) {
@ -210,6 +192,7 @@ definition:
| ID {
TableNode *node = table_lookup(getAncestor(cur), $1);
if (node == undefined) {
printdebug(" undefined nodedeclared at line %d, column %d", @1.first_line, @1.first_column);
}else if(getAdInfoType(node) != TYPE_FUNCTION_DECLARATION){
printdebug("not a valid function declaration at line %d, column %d", @1.first_line, @1.first_column);
@ -228,6 +211,7 @@ definition:
}else if(getAdInfoType(parameter) != TYPE_RECORD){
printdebug("record: %s., primitive: %s.", getType(parameter), getName(recprime));
printdebug("function defined with as, but parameter is type %s at line %d, column %d", getType(parameter),@1.first_line, @1.first_column);
}else {
for (TableNode* entry = getFirstEntry(getRecList(parameter)); entry!= NULL; entry = getNextEntry(entry)){
int type_of_param_type = getAdInfoType(entry);
@ -341,6 +325,7 @@ sblock:
dblock:
L_BRACKET
{if(getLine(cur)==0){
setLineNumber(cur, @1.first_line);
@ -349,6 +334,7 @@ dblock:
cur = CreateScope(cur,@1.first_line,@1.first_column);
}
}
declaration_list R_BRACKET;

64
src/intermediate_code.c Normal file
View File

@ -0,0 +1,64 @@
// TODO: this is here to bring your attention to the comment bellow.
// check if start is NULL if it is assign it to the start globle variable
// otherwise make it next of current and set cur to your instruction.
void emit_binary_op(char* result, char* op, char* arg1, char* arg2){
return;
}
void emit_unary_op(char* result, char* op, char* arg){
return;
}
void emit_assignment(char* target, char* source){
return;
}
void emit_as_file(FILE * out_file, Instruction * instr_arr){
return;
}
void emit_label(char* label){
return;
}
void emit_jump(char* label){
return;
}
void emit_conditional_jump(char* condition, char* label){
return;
}
void emit_function_start(char* name){
return;
}
void emit_parameter(char* param){
return;
}
void emit_function_call(char* result, char* name){
return;
}
void emit_return(char* value){
return;
}
void emit_reserve(char* result, char* type_name, int size){
return;
}
void emit_release(char* pointer){
return;
}
void emit_field_access(char* result, char* record, char* field){
return;
}
void emit_array_access(char* result, char* array, char* index, char* dimension){
return;
}
void emit_bounds_check(char* index, char* size, char* error_label){
return;
}

59
src/intermediate_code.h Normal file
View File

@ -0,0 +1,59 @@
// Track 1: Core Infrastructure & Basic Expressions
// * Create intermediate_code.h/.c defining the instruction structure:
// - Struct with fields for: opcode, result, operand1, operand2, label
// - Enum for all operation types (ADD, SUB, MUL, DIV, etc.)
// * Implement temp variable generator function that produces unique names (t1, t2, etc.)
// * Create specific code emission functions:
// - emit_binary_op(char* result, char* op, char* arg1, char* arg2)
// - emit_unary_op(char* result, char* op, char* arg)
// - emit_assignment(char* target, char* source)
// * Add Bison actions for arithmetic expressions:
// - Addition: $$ = new_temp(); emit_binary_op($$, "ADD", $1, $3);
// - Subtraction, multiplication, division, modulo
#include "symbol_table.h"
typedef enum {ADD, SUB, MUL, DIV} Op; // TODO: add all the instructions
typedef struct {
Op opcode;
TableNode * result;
TableNode * operand1;
TableNode * operand2;
int label;
int instruction;
Instruction * prev;
Instruction * next;
} Instruction;
Instruction * start = NULL;
Instruction * current = NULL;
void emit_binary_op(char* result, char* op, char* arg1, char* arg2);
void emit_unary_op(char* result, char* op, char* arg);
void emit_assignment(char* target, char* source);
// TODO: Find out what these are suposed to do. Guess is create an entry in
// the list of instructions. Guess is that its suposed to ret a struct ptr
// * Implement integer/boolean/character specific operation handling
// TODO: Find out what this means.
// * Create output function to write instructions to file with line formatting
void emit_as_file(FILE * out_file, Instruction * instr_arr);
// * Implement instruction array storage for backpatching
void emit_label(char* label);
void emit_jump(char* label);
void emit_conditional_jump(char* condition, char* label);
void emit_function_start(char* name);
void emit_parameter(char* param);
void emit_function_call(char* result, char* name);
void emit_return(char* value);
void emit_reserve(char* result, char* type_name, int size);
void emit_release(char* pointer);
void emit_field_access(char* result, char* record, char* field);
void emit_array_access(char* result, char* array, char* index, char* dimension);
void emit_bounds_check(char* index, char* size, char* error_label);

View File

@ -4,8 +4,8 @@
%option noyywrap
%option header-file="flex.h"
%option yylineno
%{
#include <stdbool.h>
#include "../tmp/grammar.tab.h"
#include "../src/symbol_table.h"
#ifndef DEBUG

View File

@ -2,25 +2,14 @@
/* The Translators - Spring 2025 */
#include "runner.h"
extern TableNode *funprime;
extern TableNode *arrayprim;
extern TableNode *integ;
extern TableNode *addr;
extern TableNode *chara;
extern TableNode *stri;
extern TableNode *boo;
extern bool DEBUG;
extern char *COLOR_YELLOW;
extern char *COLOR_BLUE;
extern char *COLOR_WHITE;
int main(int argc, char *argv[]) {
// if last argument is debug then set to true and ignore it for the rest
// of this file
if (argc > 1 && strcmp(argv[argc - 1], "-debug") == 0) {
DEBUG = true;
argc--;
}
//if (argc > 1 && strcmp(argv[argc - 1], "-debug") == 0) {
// DEBUG = true;
// argc--;
// }
if (argc == 1) {
fprintf(stderr, INVALID);
@ -72,6 +61,14 @@ int check_flag(char *arg, char *alpha) {
}
fprintf(stderr, "FLAGS REPEAT\n");
return -1;
} else if (strcmp("-tc", arg) == 0) {
if (tc_flag == NULL) {
return new_file(arg, alpha);
}
} else if (strcmp("-debug", arg) == 0) {
DEBUG = true;
return 0;
} else {
fprintf(stderr, "INVALID FLAG: Use -help for valid inputs\n");
return -1;
@ -91,10 +88,12 @@ void incr(int lnum, int cnum, int tok) {
// column_number += yyleng;
// }
}
void print_tok(int tok) {
fprintf(tok_flag, "%d %d %3d \"%s\"\n", line_number, column_number, tok,
yytext);
}
int run(FILE *alpha) {
int token;
top = cur = init(CreateScope(NULL, 1, 1));
@ -143,6 +142,16 @@ int run(FILE *alpha) {
if (st_flag != NULL) {
yyparse();
if (cur == NULL) {
printdebug("%s[FATAL] cur is null", COLOR_LIGHTRED);
}
if (top == NULL) {
printdebug("%s[FATAL] top is null", COLOR_LIGHTRED);
}
print_symbol_table(top, st_flag);
fclose(st_flag);
if (yyin != NULL) {
@ -151,9 +160,14 @@ int run(FILE *alpha) {
return 0;
}
if (tc_flag != NULL) {
//SCARLETT NEEDS TO ADD THIS FUNCTIONALITY
}
yyparse();
FILE *f = fdopen(1, "w");
print_symbol_table(getAncestor(cur), f);
print_symbol_table(top, f);
fclose(f);
if (yyin != NULL) {
@ -193,7 +207,9 @@ int new_file(char *arg, char *alpha) {
type_len = TOK_LEN;
} else if (strcmp(arg, "-st") == 0) {
type_len = ST_LEN;
} else {
} else if (strcmp(arg, "-tc") == 0){
type_len = TC_LEN;
}else {
fprintf(stderr,
"INVALID FLAG: Use -help to view valid inputs\n");
return -1;
@ -213,6 +229,9 @@ int new_file(char *arg, char *alpha) {
tok_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-st") == 0) {
st_flag = fopen(file_name, "w");
} else if (strcmp(arg, "-tc") == 0) {
tc_flag = fopen(file_name, "w");
}
return 0;
}
@ -223,4 +242,4 @@ int is_alpha_file(char *alpha, int file_len) {
return -1; // not alpha file
}
return 0; // is alpha file
}
}

View File

@ -4,16 +4,18 @@
#define ALPHA_OFFSET 6
#define TOK_LEN 3
#define ST_LEN 2
#define TC_LEN 2
#define HELP \
"HELP:\nHow to run the alpha compiler:\n./alpha [options] " \
"program\nValid " \
"options:\n-tok output the token number, token, line number, and " \
"options:\n-tok output the token number, token, line number, and " \
"column " \
"number for each of the tokens to the .tok file\n-st output the " \
"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 " \
"table for the program to the .st file\n-help print this message " \
"and exit " \
"the alpha compiler\n"
"the alpha compiler\n-debug print debugging messages in grammar rules \n"
#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"
@ -25,30 +27,57 @@
#include <sys/stat.h>
#include "../tmp/flex.h"
#include "symbol_table.h"
// #include "typedefs.h"
#include "../tmp/grammar.tab.h"
#include "symbol_table.h"
extern int line_number, column_number;
extern char *yytext;
extern FILE *yyin;
int arg;
extern bool DEBUG;
SymbolTable *top;
SymbolTable *cur;
// int main(int argc, char* argv[]);
char *is_tok(int argc, char *argv[]);
// int is_alpha_file(char *file, int file_len);
FILE *alpha_file;
FILE *tok_flag = NULL;
FILE *st_flag = NULL;
FILE *tc_flag = NULL;
bool DEBUG = false;
int no_flag = 0;
int arg;
TableNode *funprime;
TableNode *arrayprim;
TableNode *integ;
TableNode *addr;
TableNode *chara;
TableNode *stri;
TableNode *boo;
TableNode *recprime;
TableNode *funtypeprime;
TableNode *undefined;
int main(int argc, char *argv[]);
int check_flag(char *arg, char *alpha);
void incr(int lnum, int cnum, int tok);
void print_tok(int tok);
int run(FILE *alpha);
bool is_help(char *input);
int new_file(char *arg, char *alpha);
int is_alpha_file(char *alpha, int file_len);
bool is_help(char *input);
int run(FILE *alpha);
int check_flag(char *arg, char *alpha);
char *COLOR_RED = "\033[0;31m";
char *COLOR_GREEN = "\033[0;32m";
char *COLOR_ORANGE = "\033[0;33m";
char *COLOR_BLUE = "\033[0;34m";
char *COLOR_PURPLE = "\033[0;35m";
char *COLOR_CYAN = "\033[0;36m";
char *COLOR_LIGHTGRAY = "\033[0;37m";
char *COLOR_DARKGRAY = "\033[1;30m";
char *COLOR_LIGHTRED = "\033[1;31m";
char *COLOR_LIGHTGREEN = "\033[1;32m";
char *COLOR_YELLOW = "\033[1;33m";
char *COLOR_LIGHTBLUE = "\033[1;34m";
char *COLOR_LIGHTPURPLE = "\033[1;35m";
char *COLOR_LIGHTCYAN = "\033[1;36m";
char *COLOR_WHITE = "\033[1;37m";

View File

@ -3,44 +3,6 @@
#include "symbol_table.h"
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
char *typey = "type";
char *funy = "function";
TableNode *funprime;
TableNode *arrayprim;
extern SymbolTable *cur;
TableNode *integ;
TableNode *addr;
TableNode *chara;
TableNode *stri;
TableNode *boo;
TableNode *recprime;
TableNode *funtypeprime;
TableNode *undefined;
char *COLOR_RED = "\033[0;31m";
char *COLOR_GREEN = "\033[0;32m";
char *COLOR_ORANGE = "\033[0;33m";
char *COLOR_BLUE = "\033[0;34m";
char *COLOR_PURPLE = "\033[0;35m";
char *COLOR_CYAN = "\033[0;36m";
char *COLOR_LIGHTGRAY = "\033[0;37m";
char *COLOR_DARKGRAY = "\033[1;30m";
char *COLOR_LIGHTRED = "\033[1;31m";
char *COLOR_LIGHTGREEN = "\033[1;32m";
char *COLOR_YELLOW = "\033[1;33m";
char *COLOR_LIGHTBLUE = "\033[1;34m";
char *COLOR_LIGHTPURPLE = "\033[1;35m";
char *COLOR_LIGHTCYAN = "\033[1;36m";
char *COLOR_WHITE = "\033[1;37m";
bool DEBUG = false;
void printdebug_impl(char *file, int line, const char *format, ...);
void printdebug_impl(char *file, int line, const char *format, ...) {
if (DEBUG) {
printf("%s<%s> [%d]%s ", COLOR_DARKGRAY, file, line,
@ -53,88 +15,6 @@ void printdebug_impl(char *file, int line, const char *format, ...) {
}
}
#define printdebug(format, ...) \
printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__)
// AdInfo *Undefined_function_type_info;
typedef enum {
// First 4 below are primitive types that are all encapsulated in
// primitive type
// TYPE_INTEGER,
// TYPE_CHARACTER,
// TYPE_BOOLEAN,
// TYPE_ADDRESS,
// Type String is an array of char enclosed in double quotes per lexer
TYPE_STRING = 1,
// Array can be multidimensional. Information should be stored here.
// This is the type of the array
TYPE_ARRAY_TYPE = 2,
// Record is user defined types
TYPE_RECORD_TYPE = 3,
// Declaring what type a particular function is without as
TYPE_FUNCTION_DECLARATION = 4,
// Declaring what type a particular function is with as
// TYPE_AS_FUNCTION_DECLARATION,
// Declaring what type a function is (what the parameters and output
// are)
TYPE_FUNCTION_TYPE = 5,
// The Type being pointed to by the first 4 above that only stores the
// size
TYPE_PRIMITIVE = 6,
// likely NULL
TYPE_ALL_ELSE = 7,
TYPE_UNDEFINED = 8,
TYPE_RECORD = 9,
TYPE_ARRAY = 10,
TYPE_SYSTEM_DEFINED = 11 // for system defined entries like funprimetype etc.
} types;
/* put in symbol_table.h
typedef struct{
int size; if(strcmp(getType(tn),getName(integ))==0){
return
}
}primitive_info;
typedef struct{
int length;
char* location;
}string_info;
typedef struct{
int numofdimensions;
//the above value tells you how long the below array is. For example if num
of dimensions is 5, I can store 1,3,2,5,9 to define > int* sizesofdimensions;
TableNode* typeofarray;
}array_info;
typedef struct{
//similar to above we define a record to hold the number of elements and an
array of tablenodes (types) that it contains in the order specified by the user
int numofelements;
TableNode* listoftypes;
}record_info;
typedef struct{
int startlinenumber;
bool regularoras;
}function_declaration_info;
typedef struct{
TableNode* parameter;
TableNode* returntype;
}function_type_info;
typedef union {
PrimAdInfo* primitive_info;
ArrayAdInfo* array_info;
RecAdInfo* record_info;
StringAdInfo* string_info;
FunDecAdInfo* func_dec_info;
FunTypeAdInfo* func_type_info;
}AdInfo;
*/
// primitive additional info only stores the size of that type
AdInfo *CreatePrimitiveInfo(int size) {
@ -168,19 +48,10 @@ int getPrimSize(TableNode *definition) {
return definition->additionalinfo->PrimAdInfo->size;
}
// probably don't need the below structure since can create from an array
/*string_info* CreateStringInfo(int length, char* loc){
string_info* stringy = (string_info*)malloc(sizeof(string_info));
stringy.length=length;
char* location = loc;
return stringy;
}
*/
// Only information stored in array info is the number of dimensions and the
// type stored in the array per professor, the actual size of the array is
// calculated at runtime so bounds checking only needs to be done then
AdInfo *CreateArrayInfo(int dim, /*int* sizes,*/ TableNode *type) {
AdInfo *CreateArrayInfo(int dim, TableNode *type) {
if (type == NULL) {
printdebug("passed a NULL reference to "
"CreateArrayInfo. Invalid.");
@ -933,41 +804,7 @@ SymbolTable *setColumnNumber(SymbolTable *st, int column) {
st->Line_Number = column;
return st;
}
/*
//we use false for type defs and true for functions for parameter of typeOf
TableNode* Define(SymbolTable* table, bool typeOf, char* id) {
if(table ==NULL || table->Parent_Scope != NULL){
printdebug("No valid table given for header defs");
return NULL;
}
TableNode* newEntry = (TableNode*)malloc(sizeof(TableNode));
//possible issues with referencing text instead of heap
if(typeOf == 0){
newEntry->theType = typey;
}
if (typeOf == 1){
newEntry->theType = funy;
}
if(table_lookup(table,id) != NULL){
printdebug("already defined at the top level, can't define duplicate
names"); return NULL;
}
newEntry->theName = id;
if (table->entries == NULL) {
table->entries = newEntry;
return newEntry;
} else {
TableNode* oldEntry = table->entries;
table->entries = newEntry;
newEntry->next = oldEntry;
return newEntry;
}
}
*/
// only check table that is given
TableNode *table_lookup(SymbolTable *table, char *x) {
if (table == NULL) {
@ -982,6 +819,7 @@ TableNode *table_lookup(SymbolTable *table, char *x) {
}
return undefined;
}
// check current table and all parents
TableNode *look_up(SymbolTable *table, char *x) {
if (table == NULL) {
@ -998,61 +836,9 @@ TableNode *look_up(SymbolTable *table, char *x) {
x, getLine(table), getColumn(table));
return look_up(table->Parent_Scope, x);
}
/*
void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "%-17s: %-6s : %-6s : %-21s: %-28s\n", "NAME",
"SCOPE", "PARENT", "TYPE", "Extra annotation");
}
TableNode *entrie = table->entries;
fprintf(file_ptr, "-----------------:--------:--------:----------------"
"------:---------"
"--------------------\n");
int parant_scope = 0;
int current_scope = 0;
if (table->Parent_Scope != NULL) {
parant_scope = table->Parent_Scope->Line_Number * 1000 +
table->Parent_Scope->Column_Number;
current_scope =
table->Line_Number * 1000 + table->Column_Number;
} else {
current_scope = 1001;
}
if (entrie == NULL) {
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n", "",
current_scope, parant_scope, "", "Empty Scope");
}
for (; entrie != NULL; entrie = entrie->next) {
if (parant_scope == 0) {
/*have to update if (strcmp(entrie->theType->theName,
"function primitive") ||
strcmp(entrie->theType->theName,
"array")) {
}
fprintf(file_ptr,
"%-17s: %06d : : %-21s: %-28s\n",
entrie->theName, current_scope,
entrie->theType->theName, "Extra annotation");
} else {
fprintf(file_ptr, "%-17s: %06d : %06d : %-21s: %-28s\n",
entrie->theName, current_scope, parant_scope,
entrie->theType->theName, "Extra annotation");
}
}
if (table->Children_Scope != NULL) {
ListOfTable *node = table->Children_Scope;
for (; node != NULL; node = node->next) {
print_symbol_table(node->table, file_ptr);
}
}
if (table->Parent_Scope == NULL) {
fprintf(file_ptr, "-----------------:--------:--------:--------"
"--------------:-------"
"----------------------\n");
}
}*/
void print_symbol_table(SymbolTable *table, FILE *file_ptr) {
if (table == NULL) {
printdebug(
"%s[FATAL] passed in NULL table to print_symbol_table",
@ -1352,20 +1138,3 @@ TableNode *getNextEntry(TableNode *tn) {
}
return tn->next;
}
// uncomment the below main function along with the headers above for a simple
// standalone test of table and entry creation
/*
int main(){
char* String = "STRING";
char* X = "X";
SymbolTable* Second = CreateScope(NULL, 2,2);
printdebug("Line number is %d, Column number of scope is
%d",Second->Line_Number,Second->Column_Number); TableNode* First_Entry =
CreateEntry(Second,String,X);
printdebug("The type of the first entry is %s",First_Entry->theType);
return 0;
}
*/

View File

@ -1,3 +1,4 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
@ -15,26 +16,12 @@ typedef struct {
int size;
} primitive_info;
/*This structure can be subsumed into the structure below (1-d array of chars)
typedef struct{
//shouldn't need to store any values since would be compiled at runtime
//int length;
//char* location;
}string_info;
*/
typedef struct {
int numofdimensions;
// the above value tells you how long the below array is. For example if
// num of dimensions is 5, I can store 1,3,2,5,9 to define > int*
// arr; shouldn't need to store any values (like sizes of dimenions or
// the location int* sizesofdimensions; do have to store type of array
struct TableNode *typeofarray;
} array_info;
typedef struct {
// similar to above we define a record to hold the number of elements
// and an array of tablenodes (types) that it contains in the >
int numofelements;
struct SymbolTable *recordScope;
} record_info;
@ -53,20 +40,17 @@ typedef union {
primitive_info *PrimAdInfo;
array_info *ArrayAdInfo;
record_info *RecAdInfo;
// string_info* StringAdInfo;
function_declaration_info *FunDecAdInfo;
function_type_info *FunTypeAdInfo;
} AdInfo;
typedef struct ListOfTable {
struct SymbolTable *table;
// struct ListOfTable* prev;
struct ListOfTable *next;
} ListOfTable;
//Table node to store
typedef struct TableNode {
// reference to the type entry that this is
struct TableNode *theType;
int tag;
char *theName;
@ -82,30 +66,100 @@ typedef struct SymbolTable {
int Column_Number;
} SymbolTable;
typedef enum {
TYPE_STRING = 1,
TYPE_ARRAY_TYPE = 2,
TYPE_RECORD_TYPE = 3,
TYPE_FUNCTION_DECLARATION = 4,
TYPE_FUNCTION_TYPE = 5,
TYPE_PRIMITIVE = 6,
TYPE_ALL_ELSE = 7,
TYPE_UNDEFINED = 8,
TYPE_RECORD = 9,
TYPE_ARRAY = 10
} types;
AdInfo *CreatePrimitiveInfo(int size);
int getPrimSize(TableNode *definition);
AdInfo *CreateArrayInfo(int dim, TableNode *type);
int getNumArrDim(TableNode *definition);
TableNode *getArrType(TableNode *definition);
AdInfo *CreateRecordInfo(int length, SymbolTable *recordScope);
int getRecLength(TableNode *definition);
SymbolTable *getRecList(TableNode *definition);
TableNode *setRecSize(TableNode *tn, int n);
int getRecSize(SymbolTable *tn);
AdInfo *CreateFunctionDeclarationInfo(int line, bool asorregular);
int getStartLine(TableNode *definition);
TableNode *setStartLine(TableNode *tn, int start);
bool getAsKeyword(TableNode *definition);
TableNode *setAsKeyword(TableNode *tn, bool as);
AdInfo *CreateFunctionTypeInfo(TableNode *parameter, TableNode *returntype);
TableNode *getParameter(TableNode *definition);
TableNode *getReturn(TableNode *definition);
SymbolTable *CreateScope(SymbolTable *ParentScope, int Line, int Column);
SymbolTable *init(SymbolTable *start);
TableNode *populateTypeAndInfo(TableNode *tn, TableNode *type, AdInfo *info);
int getAdInfoType(TableNode *tn);
TableNode *CreateEntry(SymbolTable *table, TableNode *typeOf, char *id,
AdInfo *ad);
char *getType(TableNode *tn);
char *getName(TableNode *tn);
int getLine(SymbolTable *st);
int getColumn(SymbolTable *st);
TableNode *addName(TableNode *tn, char *str);
SymbolTable *setLineNumber(SymbolTable *st, int line);
SymbolTable *setColumnNumber(SymbolTable *st, int column);
TableNode *table_lookup(SymbolTable *table, char *x);
TableNode *look_up(SymbolTable *table, char *x);
void print_symbol_table(SymbolTable *table, FILE *file_ptr);
SymbolTable *getAncestor(SymbolTable *table);
SymbolTable *removeEntry(SymbolTable *scope, char *search);
bool typeCheck(char *firstID, char *secondID);
SymbolTable *getParent(SymbolTable *st);
ListOfTable *getChildren(SymbolTable *st);
SymbolTable *getFirstChild(ListOfTable *lt);
ListOfTable *getRestOfChildren(ListOfTable *lt);
TableNode *getFirstEntry(SymbolTable *st);
TableNode *getNextEntry(TableNode *tn);
SymbolTable *init(SymbolTable *scope);
int getPrimSize(TableNode *definition);
int getNumArrDim(TableNode *definition);
TableNode *getArrType(TableNode *definition);
int getRecLength(TableNode *definition);
SymbolTable *getRecList(TableNode *definition);
int getStartLine(TableNode *definition);
bool getAsKeyword(TableNode *definition);
TableNode *getParameter(TableNode *definition);
TableNode *getReturn(TableNode *definition);
char *getType(TableNode *tn);
char *getName(TableNode *tn);
int getLine(SymbolTable *st);
int getColumn(SymbolTable *st);
void printdebug_impl(char *file, int line, const char *format, ...);
#define printdebug(format, ...) \
printdebug_impl(__FILE__, __LINE__, format, ##__VA_ARGS__)
extern int yylex(void);
extern char *yytext;
extern int yyleng;
extern int yychar;
extern SymbolTable *cur;
extern int line_number;
extern int column_number;
extern FILE *yyin;
extern bool DEBUG;
extern TableNode *funprime;
extern TableNode *arrayprim;
extern TableNode *integ;
extern TableNode *addr;
extern TableNode *chara;
extern TableNode *stri;
extern TableNode *boo;
extern TableNode *recprime;
extern TableNode *funtypeprime;
extern TableNode *undefined;
extern char *COLOR_RED;
extern char *COLOR_GREEN;
extern char *COLOR_ORANGE;
extern char *COLOR_BLUE;
extern char *COLOR_PURPLE;
extern char *COLOR_CYAN;
extern char *COLOR_LIGHTGRAY;
extern char *COLOR_DARKGRAY;
extern char *COLOR_LIGHTRED;
extern char *COLOR_LIGHTGREEN;
extern char *COLOR_YELLOW;
extern char *COLOR_LIGHTBLUE;
extern char *COLOR_LIGHTPURPLE;
extern char *COLOR_LIGHTCYAN;
extern char *COLOR_WHITE;

20
test.sh
View File

@ -48,7 +48,7 @@ if [ $# -eq 0 ]; then
if [ -f "$file" ]; then
filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" -debug
./alpha -st -debug "$file"
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc
fi
@ -60,7 +60,7 @@ if [ $# -eq 0 ]; then
if [ -f "$file" ]; then
filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" -debug
./alpha -st -debug "$file"
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc
fi
@ -72,7 +72,7 @@ if [ $# -eq 0 ]; then
if [ -f "$file" ]; then
filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" -debug
./alpha -st -debug "$file"
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc
fi
@ -84,7 +84,7 @@ if [ $# -eq 0 ]; then
if [ -f "$file" ]; then
filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" -debug
./alpha -st -debug "$file"
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n${WHITE}"
switchfunc
fi
@ -106,7 +106,7 @@ else
if [ -f "$1" ]; then
filename=$(basename -- "$1")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$1" -debug
./alpha -st -debug "$1"
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}"
exit 1
else
@ -121,7 +121,7 @@ else
if [[ "$file" == *"$1"* ]]; then
filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" -debug
./alpha -st -debug "$file"
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc
fi
@ -132,7 +132,7 @@ else
if [[ "$file" == *"$1"* ]]; then
filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" -debug
./alpha -st -debug "$file"
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc
fi
@ -143,7 +143,7 @@ else
if [[ "$file" == *"$1"* ]]; then
filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" -debug
./alpha -st -debug "$file"
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc
fi
@ -154,7 +154,7 @@ else
if [[ "$file" == *"$1"* ]]; then
filename=$(basename -- "$file")
echo -e "- ${SWITCH}Running test: ${LIGHTBLUE}$filename ${SWITCH}-----${WHITE}"
./alpha -st "$file" -debug
./alpha -st -debug "$file"
echo -e "${SWITCH}----- End of test: ${LIGHTBLUE}$filename ${SWITCH}-${WHITE}\n"
switchfunc
fi
@ -163,4 +163,4 @@ else
echo -e "${RED}[ERROR] ${YELLOW}Invalid prefix $1!${WHITE}"
exit 1
fi
fi
fi

View File

@ -0,0 +1,31 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
bar2 : 001001 : : T2 : User Defined
bar1 : 001001 : : T2 : User Defined
foo : 001001 : : T1 : User Defined
arr : 001001 : : 1 -> integer : Type of Array
T2 : 001001 : : primitive function type : User Defined
T1 : 001001 : : primitive function type : User Defined
rec : 001001 : : record : elements-2
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
w : 026000 : 001001 : rec : User Defined
result : 026000 : 001001 : integer : User Defined
arg : 026000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
r : 021000 : 001001 : integer : User Defined
s : 021000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
a : 017000 : 001001 : rec : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
x : 013000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
y : 004000 : 001001 : integer : User Defined
x : 004000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,26 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
entry : 001001 : : string2integer : User Defined
integer2integer2integerFunc: 001001 : : integer2integer2integer : User Defined
released : 001001 : : address2integer : User Defined
reserved : 001001 : : integer2address : User Defined
printBoolean : 001001 : : Boolean2integer : User Defined
printCharacter : 001001 : : character2integer : User Defined
printInteger : 001001 : : integer2integer : User Defined
integer2integer2integer : 001001 : : primitive function type : User Defined
address2integer : 001001 : : primitive function type : User Defined
integer2address : 001001 : : primitive function type : User Defined
Boolean2Boolean2Boolean : 001001 : : primitive function type : User Defined
character2character2Boolean: 001001 : : primitive function type : User Defined
integer2integer2Boolean : 001001 : : primitive function type : User Defined
string2integer : 001001 : : primitive function type : User Defined
Boolean2integer : 001001 : : primitive function type : User Defined
character2integer : 001001 : : primitive function type : User Defined
integer2integer : 001001 : : primitive function type : User Defined
Boolean2Boolean : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,17 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
b1 : 005000 : 001001 : Boolean : User Defined
b2 : 005000 : 001001 : Boolean : User Defined
arr2 : 005000 : 001001 : address : User Defined
arr : 005000 : 001001 : address : User Defined
x : 005000 : 001001 : integer : User Defined
arg : 005000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,17 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
rec : 001001 : : record : elements-2
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
w : 007000 : 001001 : rec : User Defined
arg : 007000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
y : 004000 : 001001 : integer : User Defined
x : 004000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,14 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
rec : 001001 : : record : elements-2
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
w : 004000 : 001001 : rec : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
y : 001000 : 001001 : integer : User Defined
x : 001000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,36 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
entry : 001001 : : string2integer : User Defined
printBoolean : 001001 : : Boolean2integer : User Defined
printCharacter : 001001 : : character2integer : User Defined
printInteger : 001001 : : integer2integer : User Defined
address2integer : 001001 : : primitive function type : User Defined
integer2address : 001001 : : primitive function type : User Defined
BooleanXBoolean2Boolean : 001001 : : primitive function type : User Defined
characterXcharacter2Boolean: 001001 : : primitive function type : User Defined
integerXinteger2Boolean : 001001 : : primitive function type : User Defined
integerXinteger2integer : 001001 : : primitive function type : User Defined
string2integer : 001001 : : primitive function type : User Defined
Boolean2integer : 001001 : : primitive function type : User Defined
character2integer : 001001 : : primitive function type : User Defined
integer2integer : 001001 : : primitive function type : User Defined
Boolean2Boolean : 001001 : : primitive function type : User Defined
integerXinteger : 001001 : : record : elements-2
characterXcharacter : 001001 : : record : elements-2
BooleanXBoolean : 001001 : : record : elements-2
string : 001001 : : 1 -> character : Type of Array
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
y : 015000 : 001001 : integer : User Defined
x : 015000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
y : 014000 : 001001 : character : User Defined
x : 014000 : 001001 : character : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
y : 013000 : 001001 : Boolean : User Defined
x : 013000 : 001001 : Boolean : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,62 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
make_list : 001001 : : list : User Defined
bar2 : 001001 : : T2 : User Defined
bar1 : 001001 : : T2 : User Defined
foo : 001001 : : T1 : User Defined
list : 001001 : : primitive function type : User Defined
llnode : 001001 : : record : elements-3
T2 : 001001 : : primitive function type : User Defined
T1 : 001001 : : primitive function type : User Defined
rec : 001001 : : record : elements-2
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
li : 070000 : 001001 : llnode : User Defined
w : 070000 : 001001 : rec : User Defined
result : 070000 : 001001 : integer : User Defined
arg : 070000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
r : 054000 : 001001 : integer : User Defined
s : 054000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
x : 060009 : 054000 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
: 062028 : 060009 : : Empty Scope
-------------------------:--------:--------:--------------------------:------------------------------
: 055021 : 054000 : : Empty Scope
-------------------------:--------:--------:--------------------------:------------------------------
: 056026 : 055021 : : Empty Scope
-------------------------:--------:--------:--------------------------:------------------------------
a : 050000 : 001001 : rec : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
x : 046000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
temp : 017000 : 001001 : address : User Defined
curr : 017000 : 001001 : address : User Defined
ret : 017000 : 001001 : address : User Defined
orig_a : 017000 : 001001 : integer : User Defined
a : 017000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
: 021012 : 017000 : : Empty Scope
-------------------------:--------:--------:--------------------------:------------------------------
: 026023 : 021012 : : Empty Scope
-------------------------:--------:--------:--------------------------:------------------------------
: 035020 : 026023 : : Empty Scope
-------------------------:--------:--------:--------------------------:------------------------------
: 031034 : 026023 : : Empty Scope
-------------------------:--------:--------:--------------------------:------------------------------
: 019029 : 017000 : : Empty Scope
-------------------------:--------:--------:--------------------------:------------------------------
next : 008000 : 001001 : llnode : User Defined
val : 008000 : 001001 : integer : User Defined
prev : 008000 : 001001 : llnode : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
y : 004000 : 001001 : integer : User Defined
x : 004000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,30 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
bar2 : 001001 : : T2 : User Defined
bar1 : 001001 : : T2 : User Defined
foo : 001001 : : T1 : User Defined
T2 : 001001 : : primitive function type : User Defined
T1 : 001001 : : primitive function type : User Defined
rec : 001001 : : record : elements-2
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
w : 001000 : 001001 : rec : User Defined
result : 001000 : 001001 : integer : User Defined
arg : 001000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
r : 001000 : 001001 : integer : User Defined
s : 001000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
a : 001000 : 001001 : rec : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
x : 001000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
y : 001000 : 001001 : integer : User Defined
x : 001000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,13 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
x : 005000 : 001001 : integer : User Defined
arg : 005000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,12 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
arg : 005000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,18 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
T2 : 001001 : : primitive function type : User Defined
rec : 001001 : : record : elements-2
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
w : 008000 : 001001 : rec : User Defined
arg : 008000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------
y : 004000 : 001001 : integer : User Defined
x : 004000 : 001001 : integer : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -1,28 +1,35 @@
type main: string -> integer
function entry: main
type rec: [integer: x; integer: y]
type T1: integer -> integer
type T2: rec -> integer
type arr : 1 -> integer
type arr: 1 -> integer
function foo : T1
function bar1 : T2
function bar2 : T2
function foo: T1
function bar1: T2
function bar2: T2
foo(x) := {
return x * x;
foo (x) := {
return x * x;
}
bar1(a) := {
return a.x * a.y;
bar1 (a) := {
return a.x * a.y;
}
bar2 as (r,s) := {
return r * s;
return r * s;
}
entry(arg) := {
[ integer: result ; rec: w]
result := foo(5);
w := reserve w; (* see types.alpha reserve returns a value of type address, which can be assigned to array and record variables*)
w.x := 5;
w.y := 7;
result := bar1(w); (* pass w (a rec type value) to bar1 *)
result := bar2(5,7); (* implicitly build a rec type value, assign 5 and 7 to fields x and y, but call them r and s *)
return 0;
}
entry (arg) := {
[ integer: result ; rec: w]
result := foo(5);
w := reserve w; (* see types.alpha reserve returns a value of type address, which can be assigned to array and record variables*)
w.x := 5;
w.y := 7;
result := bar1(w); (* pass w (a rec type value) to bar1 *)
result := bar2(5,7); (* implicitly build a rec type value, assign 5 and 7 to fields x and y, but call them r and s *)
return 0;
}

View File

@ -3,15 +3,19 @@ type integer2integer: integer -> integer
type character2integer: character -> integer
type Boolean2integer: Boolean -> integer
type string2integer: string -> integer
function integerXinteger2integer: integerXinteger (*-> integer
type integerXinteger2Boolean: integerXinteger -> Boolean
type characterXcharacter2Boolean: characterXcharacter -> Boolean
type BooleanXBoolean2Boolean: BooleanXBoolean -> Boolean
type integer2integer2Boolean: integer2integer -> Boolean
type character2character2Boolean: character2character -> Boolean
type Boolean2Boolean2Boolean: Boolean2Boolean -> Boolean
type integer2address: integer -> address
type address2integer: address -> integer
type integer2integer2integer: integer2integer -> integer
external function printInteger: integer2integer
external function printCharacter: character2integer
external function printBoolean: Boolean2integer
external function reserve: integer2address
external function release: address2integer
function entry: string2integer*)
external function reserved: integer2address
external function released: address2integer
function integer2integer2integerFunc: integer2integer2integer
function entry: string2integer

View File

@ -1,12 +1,17 @@
entry(arg) := {
[integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1]
x := 3 + 2 * 8;
x := 3 - 2 / 8;
x := 3 * 2 % 8;
x := 3 * 2 % 8;
x := 3 % 2 * 8;
x := 3 + 2 - 8;
arr2 := 1 * reserve x;
type main: string -> integer
function entry: main
entry (arg) := {
[integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1]
x := 3 + 2 * 8;
x := 3 - 2 / 8;
x := 3 * 2 % 8;
x := 3 * 2 % 8;
x := 3 % 2 * 8;
x := 3 + 2 - 8;
arr2 := 1 * reserve x;
arr2 := release x;
b2 := 3 < 2;
b1 := 1 = 2;

View File

@ -1,10 +1,13 @@
type main: string -> integer
function entry: main
type rec: [integer: x; integer: y]
entry(arg) := {
entry (arg) := {
[rec: w]
w := reserve w;
w.x := 1;
w.y := 2;
w.z := 3;
return 0;
return 0;
}

View File

@ -1,8 +1,8 @@
type rec: [integer: x; integer: y]
entry(arg) := {
[rec: w]
w := reserve w;
w := release (w);
return 0;
entry (arg) := {
[rec: w]
w := reserve w;
w := release (w);
return 0;
}

View File

@ -1,11 +1,14 @@
(* 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.
(*
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)
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; Boolean: y]
type characterXcharacter: [character: x; character: y]
@ -22,7 +25,9 @@ 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
function entry: string2integer

View File

@ -1,3 +1,6 @@
type main: string -> integer
function entry: main
type rec: [integer: x; integer: y]
type T1: integer -> integer
type T2: rec -> integer
@ -9,66 +12,70 @@ function foo : T1
function bar1 : T2
function bar2 : T2
function make_list : list
make_list(a) := {
[integer:orig_a; address: ret; address: curr; address: temp]
if (a < 0 | a = 0) then {
return null;
} else {
ret := reserve llnode;
ret.prev := null;
ret.next := null;
ret.val := a;
while (0 < a) {
temp := reserve llnode;
temp.prev := null;
temp.next := null;
temp.val := val;
if (a = orig_a) then {
ret.next := temp;
temp.prev := ret;
curr := temp;
} else {
curr.next := temp;
temp.prev := curr;
curr := temp;
}
a := a - 1;
}
return ret;
make_list (a) := {
[integer:orig_a; address: ret; address: curr; address: temp]
if (a < 0 | a = 0) then {
return null;
} else {
ret := reserve llnode;
ret.prev := null;
ret.next := null;
ret.val := a;
while (0 < a) {
temp := reserve llnode;
temp.prev := null;
temp.next := null;
temp.val := val;
if (a = orig_a) then {
ret.next := temp;
temp.prev := ret;
curr := temp;
} else {
curr.next := temp;
temp.prev := curr;
curr := temp;
}
a := a - 1;
}
return ret;
}
}
foo (x) := {
return x * x;
}
foo(x) := {
return x * x;
}
bar1(a) := {
return a.x * a.y;
}
bar1 (a) := {
return a.x * a.y;
}
bar2 as (r,s) := {
if (r < s) then {
while (!(r < s)) {
r := r + 1;
}
} else {
[integer: x]
x := 0;
while (x < 10) {
r := r + s;
}
if (r < s) then {
while (!(r < s)) {
r := r + 1;
}
return r * s;
}
entry(arg) := {
[ integer: result ; rec: w; llnode: li]
li := make_list(6);
result := foo(5);
w := reserve w;
w.x := 5;
w.y := 7;
result := bar1(w);
result := bar2(5,7);
return 0;
} else {
[integer: x]
x := 0;
while (x < 10) {
r := r + s;
}
}
return r * s;
}
entry (arg) := {
[ integer: result ; rec: w; llnode: li]
li := make_list(6);
result := foo(5);
w := reserve w;
w.x := 5;
w.y := 7;
result := bar1(w);
result := bar2(5,7);
return 0;
}

View File

@ -1 +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; }
type main: string -> integer function entry: main 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; }

View File

@ -1,4 +1,8 @@
entry(arg) := {
[integer:x]
x := 3 + 2 * 8;
type main: string -> integer
function entry: main
entry (arg) := {
[integer:x]
x := 3 + 2 * 8;
return 0;
}

View File

@ -1,5 +1,7 @@
type main: string -> integer
function entry: main
entry(arg) := {
[int : x]
return 0;
return 0;
}

View File

@ -1,12 +1,16 @@
type main: string -> integer
function entry: main
type rec: [integer: x; integer: y]
type T2: rec -> integer
entry(arg) := {
[rec: w]
w := reserve w;
[rec: w]
w := reserve w;
w.x := 1;
w.y := 2;
w := release w;
return 0;
return 0;
}

View File

@ -0,0 +1,17 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
b1 : 005000 : 001001 : Boolean : User Defined
b2 : 005000 : 001001 : Boolean : User Defined
arr2 : 005000 : 001001 : address : User Defined
arr : 005000 : 001001 : address : User Defined
x : 005000 : 001001 : integer : User Defined
arg : 005000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,17 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
b1 : 005000 : 001001 : Boolean : User Defined
b2 : 005000 : 001001 : Boolean : User Defined
arr2 : 005000 : 001001 : address : User Defined
arr : 005000 : 001001 : address : User Defined
x : 005000 : 001001 : integer : User Defined
arg : 005000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,17 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
a_of_s : 001001 : : 1 -> string : Type of Array
string : 001001 : : 1 -> character : Type of Array
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
many_names : 010000 : 001001 : a_of_s : User Defined
another_name : 010000 : 001001 : string : User Defined
one_name : 010000 : 001001 : string : User Defined
arg : 010000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,18 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
a : 005000 : 001001 : character : User Defined
b1 : 005000 : 001001 : Boolean : User Defined
b2 : 005000 : 001001 : Boolean : User Defined
arr2 : 005000 : 001001 : address : User Defined
arr : 005000 : 001001 : address : User Defined
x : 005000 : 001001 : integer : User Defined
arg : 005000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -0,0 +1,17 @@
NAME : SCOPE : PARENT : TYPE : Extra annotation
-------------------------:--------:--------:--------------------------:------------------------------
entry : 001001 : : main : User Defined
main : 001001 : : primitive function type : User Defined
integer : 001001 : : Primitive : size-4 bytes
address : 001001 : : Primitive : size-8 bytes
character : 001001 : : Primitive : size-1 bytes
string : 001001 : : 1 -> character : Type of Array
Boolean : 001001 : : Primitive : size-1 bytes
-------------------------:--------:--------:--------------------------:------------------------------
b1 : 005000 : 001001 : Boolean : User Defined
b2 : 005000 : 001001 : Boolean : User Defined
arr2 : 005000 : 001001 : address : User Defined
arr : 005000 : 001001 : address : User Defined
x : 005000 : 001001 : integer : User Defined
arg : 005000 : 001001 : string : User Defined
-------------------------:--------:--------:--------------------------:------------------------------

View File

@ -1,6 +1,12 @@
entry(arg) := {
[integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1]
b2 := 3 < x;
b1 := arr = 2;
b1 := 6<7 & arr2=7;
type main: string -> integer
function entry: main
entry (arg) := {
[integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1]
b2 := 3 < x;
b1 := arr = 2;
b1 := 6<7 & arr2=7;
return 0;
}

View File

@ -1,5 +1,11 @@
entry(arg) := {
[integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1]
b2 := !(3 < 2);
b1 := !5;
type main: string -> integer
function entry: main
entry (arg) := {
[integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1]
b2 := !(3 < 2);
b1 := !5;
return 0;
}

View File

@ -1,10 +1,14 @@
type main: string -> integer
function entry: main
type string: 1 -> character
type a_of_s: 1 -> string
(* maybe some other type definitions *)
entry(arg) := {
entry (arg) := {
[ string: one_name; string: another_name; a_of_s: many_names ]
one_name := "a string literal";
another_name := reserve another_name(4); (* reserve space for an an array of character, with 4 members *)
another_name(0) := 'C';
@ -20,6 +24,7 @@ entry(arg) := {
many_names(2)(2) := 'r';
many_names(2)(3) := 't';
many_names(2)(4) := 'h';
0(2)(5) := 'o';
many_names(2)(5) := 'o';
return 0;
}

View File

@ -1,4 +1,5 @@
type main: string -> integer
type rec: [integer: x; integer: y]
function entry: main

View File

@ -1,6 +1,12 @@
entry(arg) := {
[integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1]
x := -8;
type main: string -> integer
function entry: main
entry (arg) := {
[integer:x; address: arr; address: arr2; Boolean : b2; Boolean : b1]
x := -8;
x := -b1;
b2 := -x;
return 0;
}