Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
grnt426 committed Mar 11, 2012
2 parents 0a7cb46 + 2852dc3 commit aafaca0
Show file tree
Hide file tree
Showing 15 changed files with 480 additions and 84 deletions.
28 changes: 21 additions & 7 deletions Hartz Instruction Set.txt
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,18 @@ Instruction Set:
- ADD <0101>
- Add
- add $source, $source2, $dest
- SW <0110>
- SW <01100>
- Store word to data ring
- sw $source
- LW <0111>
- SI <01101>
- Store immediate onto data ring
- si literal/constant
- LW <01110>
- Load word from data ring
- lw $dest
- LI <01111>
- Load Immediate into destination register
- li $dest
- BEZ <1000>
- Branch if equal to zero
- beq $source
Expand All @@ -61,12 +67,9 @@ Instruction Set:
- rotates by the specified value in the register
- rot (bit 6 = 0)
- implicitly rotates once
- LROT <10011(0/1)>
- LROT <10011>
- Rotates Data Ring by amount in next value on text ring
- lrot $multiplier_reg (bit 7 is register, bit 6 = 1)
- applies multiplier, then loads and performs rotate
- lrot (bit 7 ignored, bit 6 = 0)
- apply no multiplier
- lrot literal/constant
- LJMP <1010>
- Specifies that the next instruction is to be made a longer jump
- ljmp !multiplier
Expand All @@ -79,6 +82,17 @@ Instruction Set:
- NOP <11111>
- No Operation
- does nothing
- LFSJ <111101>
- Load From, Substract, and Jump
- Loads the current value pointed at on the data ring,
subtracts from next value on text ring, stores to jump register,
and jumps.
- Used to return from a function call.
- STJ <1111110>
- Store To, and Jump
- Stores to the data ring the next value on the text ring, then stores
value after that in the jump register and jumps.
- Used to make a function call.

Program Execution:
- Load instruction from text ring
Expand Down
100 changes: 94 additions & 6 deletions idents.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,49 @@ void process_label_def(char *tok, struct program *prog){
prog->line_count);
#endif

struct symbol *s;

if(tok[0] == LABEL_SYM){
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "Label definition is empty!\n");
prog->error_code = SYM_ERR;
prog->error_code = EMPTY_DEF;
}
else{
char *iden = (char *) malloc(sizeof(tok));
strcpy(iden, tok);
iden[strlen(iden)-1] = '\0';
if(find_symbol(iden, prog->tbl)){
print_compiler_error(prog, RED_C);
fprintf(stderr, "Doubly defined label!\n");
if( (s = find_symbol(iden, prog->tbl)) ){

// Looks like we found the function that was previously
// declared
if(s->type == FUNC_TYPE){

// Gah! The fucntion was already claimed elsewhere!
if(s->pos != -1){
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "Function was already defined on line "
"%d!\n", s->pos);
prog->error_code = FUNC_DOUBLE;
}
else{
// giving the function a location
s->pos = prog->term_count;
}
}
else{
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "Doubly defined label!\n");
prog->error_code = DOUBLE_DEF;
}
}

// Just a plain label
else{
add_symbol(iden, prog->line_count, prog->tbl, prog->term_count);
add_symbol(iden, prog->line_count, prog->tbl, prog->term_count,
LABEL_TYPE);
}
}
}
Expand All @@ -60,24 +88,31 @@ short check_const_def(char *tok, struct program *prog){
void process_const_def(char *tok, struct program *prog){
if(!tok[1]){
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "Defined constant is empty!\n");
prog->error_code = EMPTY_DEF;
}
else{
char *iden = (char *) malloc(sizeof(tok)-1);
strcpy(iden, tok+1);
if(find_symbol(iden, prog->const_tbl)){
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "Doubly defined constant!\n");
prog->error_code = DOUBLE_DEF;
}
else{
tok = strtok(0, STR_TOK_SEP);
if(!tok){
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "Newly defined constant has no value!\n");
prog->error_code = NO_DEF_VAL;
}
else{
// TODO: CHECK VALUE OF ATOI() call
add_symbol(iden, atoi(tok), prog->const_tbl, prog->line_count);
add_symbol(iden, stonum(tok), prog->const_tbl, prog->line_count,
CONST_TYPE);
}
}
}
Expand Down Expand Up @@ -149,6 +184,7 @@ void print_compiler_error(struct program *prog, const char *color){
*/
void print_unexpected_ident(char *ident, struct program *prog){
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "\tUnexpected Identifier '%s'.\n", ident);
prog->error_code = GARBAGE;
}
Expand All @@ -159,6 +195,7 @@ void print_unexpected_ident(char *ident, struct program *prog){
*/
void print_expected_ident(char *ident, char *expected, struct program *prog){
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "\tExpected '%s' but found '%s'.\n", expected, ident);
prog->error_code = GARBAGE;
}
Expand All @@ -167,3 +204,54 @@ void print_asterisk(const char *color, FILE *out){
fprintf(out, "%s * " RST_C, color);
}

short check_func_def(char *tok, struct program *prog){
return tok[0] == FUNC_DEF;
}

void process_func_def(char *tok, struct program *prog){

// Check for empty definitions
if(strlen(tok) == 1){
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "\tFunction Definition is empty!\n");
prog->error_code = EMPTY_DEF;
}
else{
char *iden = (char *) malloc(sizeof(tok) + 1);
strcpy(iden, tok + 1);
iden[strlen(iden)] = '\0';
if(find_symbol(iden, prog->tbl)){
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "\tFunction already defined!\n");
prog->error_code = DOUBLE_DEF;
}
else{
add_symbol(iden, -1, prog->tbl, -1, FUNC_TYPE);
}
}
}

void print_literal_too_large(char *iden, struct program *prog){
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "\tThe literal '%s' is too large to represent!\n", iden);
prog->error_code = LIT_TOO_BIG;
}

void print_expected_literal(char *iden, struct program *prog){
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "\tExpected a literal, not '%s'!\n", iden);
prog->error_code = UNEXPECTED;
}

void print_expected_const(char *iden, struct program *prog){
print_compiler_error(prog, RED_C);
print_asterisk(RED_C, stderr);
fprintf(stderr, "\tExpected a constant definiton, not '%s'!\n", iden);
prog->error_code = UNEXPECTED;
}


18 changes: 18 additions & 0 deletions idents.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,21 @@
#define LABEL_SYM ':'
#define CONSTANT_SYM '#'
#define LITERAL_SYM '!'
#define FUNC_DEF '.'

// Soft Contraints
#define MAX_LINE_LEN 32

// Error Reporting
#define DOUBLE_DEF 30
#define NO_DEF_VAL 31
#define EMPTY_DEF 32
#define FUNC_DOUBLE 33
#define CALL_NON_F 34
#define RET_NON_F 35
#define LIT_TOO_BIG 36
#define UNEXPECTED 37

struct program;

// Label Processing Functions
Expand All @@ -33,11 +44,18 @@ void process_comment(struct program *prog);
// Error Reporting
void print_compiler_error(struct program *prog, const char *color);
void print_asterisk(const char *color, FILE *out);
void print_expected_const(char *iden, struct program *prog);
void print_expected_literal(char *iden, struct program *prog);
void print_literal_too_large(char *iden, struct program *prog);

// Identifier Error Reporting
void print_unexpected_ident(char *ident, struct program *prog);
void print_expected_ident(char *ident, char *expected, struct program * prog);

// Function Definition Processing
short check_func_def(char *tok, struct program *prog);
void process_func_def(char *tok, struct program *prog);

// Miscellaneous
void blind_consume();

Expand Down
40 changes: 37 additions & 3 deletions symbols.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
#include "idents.h"
#include "strlib.h"

void add_symbol(char *iden, int val, struct symbol_table *tbl, int pos){
void add_symbol(char *iden, int val, struct symbol_table *tbl, int pos,
short type){
if(!tbl)
return;

Expand All @@ -24,6 +25,7 @@ void add_symbol(char *iden, int val, struct symbol_table *tbl, int pos){
sym->next = 0;
sym->pos = pos;
sym->used = 0;
sym->type = type;


if(tbl->r){
Expand All @@ -49,6 +51,19 @@ struct symbol *find_symbol(char *iden, struct symbol_table *tbl){
return sym;
}

struct symbol *find_symbol_at(int pos, struct symbol_table *tbl){
if(pos < 0)
return 0;
if(!tbl)
return 0;

struct symbol *sym = tbl->r;
while(sym && sym->pos != pos){
sym = sym->next;
}
return sym;
}

void print_symbol(struct symbol *sym, int c){

if(c > -1)
Expand All @@ -63,7 +78,9 @@ void print_symbol(struct symbol *sym, int c){
printf( "Next:\t%p\n"
"Iden:\t%s\n"
"Val:\t%d\n"
"Used:\t%d\n", sym->next, sym->iden, sym->val, sym->used);
"Type:\t%d\n"
"Used:\t%d\n", sym->next, sym->iden, sym->val, sym->type,
sym->used);
}
}

Expand All @@ -89,7 +106,7 @@ void print_symbol_not_found(const char *bad_sym, struct program *prog){
fprintf(stderr, "%s:\n", prog->input);
print_asterisk(RED_C, stderr);
fprintf(stderr, "\tUnknown Symbol '%s'.\n", bad_sym);
prog->error_code = -1; // TODO: create actual error_code
prog->error_code = BAD_SYM; // TODO: create actual error_code

}

Expand All @@ -102,3 +119,20 @@ void print_symbol_not_used(const struct symbol *sym, const char *sym_type,
fprintf(stderr, "\tWarning: %s '%s' is not used.\n", sym_type, sym->iden);
}

void print_non_func_call(const struct symbol *sym, struct program *prog,
int err_line){
print_asterisk(RED_C, stderr);
fprintf(stderr, "%s, %d:\n", prog->input, err_line);
print_asterisk(RED_C, stderr);
fprintf(stderr, "\tUnable to call a non-function, '%s'!\n", sym->iden);
prog->error_code = CALL_NON_F;
}

void print_return_from_non_func(int abs_pos, struct program *prog){
print_asterisk(RED_C, stderr);
fprintf(stderr, "%s, %d:\n", prog->input, abs_pos);
print_asterisk(RED_C, stderr);
fprintf(stderr, "\tUnable to return without a function declared first!\n");
prog->error_code = RET_NON_F;
}

16 changes: 14 additions & 2 deletions symbols.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
#ifndef SYMBOLS_H_
#define SYMBOLS_H

#define BAD_SYM
// Error Codes
#define BAD_SYM 40

// Type values
#define CONST_TYPE 0
#define LABEL_TYPE 1
#define FUNC_TYPE 2

struct Term;

Expand All @@ -18,6 +24,7 @@ typedef struct symbol{
int val;
int pos;
short used;
short type;
}symbol;

struct program{
Expand All @@ -36,10 +43,12 @@ struct program{
};

// Symbol manipulation
void add_symbol(char *iden, int val, struct symbol_table *tbl, int pos);
void add_symbol(char *iden, int val, struct symbol_table *tbl, int pos,
short type);

// Symbol searching
struct symbol *find_symbol(char *iden, struct symbol_table *tbl);
struct symbol *find_symbol_at(int pos, struct symbol_table *tbl);

// Printing of symbols
void print_symbol(struct symbol *sym, int c);
Expand All @@ -49,5 +58,8 @@ void print_symbols(struct symbol_table *tbl);
void print_symbol_not_found(const char *bad_sym, struct program *prog);
void print_symbol_not_used(const struct symbol *sym, const char *sym_type,
const struct program *prog);
void print_non_func_call(const struct symbol *sym, struct program *prog,
int err_line);
void print_return_from_non_func(int abs_pos, struct program *prog);

#endif
Loading

0 comments on commit aafaca0

Please sign in to comment.