From: Mike D. Lowis Date: Sun, 11 Mar 2012 16:58:50 +0000 (-0400) Subject: Added define and import operators. Updated assignment parser rule with a rule for... X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=8bc875be64e282ebf701663cf2541af3e83a23c8;p=archive%2Fdlang.git Added define and import operators. Updated assignment parser rule with a rule for declarations. Also updated scheme output to reflect the changes. --- diff --git a/example.dl b/example.dl index f2d5b55..4480afa 100644 --- a/example.dl +++ b/example.dl @@ -3,7 +3,7 @@ #------------------------------------------------------------------------------ # Nums -foo = 1 +foo := 1 foo = 1.0 # Char @@ -51,12 +51,12 @@ foo = ({|a,b| a + b })(1,2) # Macro Definition and Usage #------------------------------------------------------------------------------ -% if [ +@ if [ (E B B) : exec_if($1, $2, $3), (E B) : exec_if($1, $2) ] -if (1==1) +if (1 < 2) { print(1 + 1) }{ @@ -72,11 +72,11 @@ if (1 == 1) # Delayed Evaluation #------------------------------------------------------------------------------ -% delay [ +@ delay [ (E) : make_promise({ $1 }) ] -% force [ +@ force [ (E) : $1() ] diff --git a/res/environment.scm b/res/environment.scm index 0f2e163..a199367 100644 --- a/res/environment.scm +++ b/res/environment.scm @@ -1,18 +1,7 @@ ;------------------------------------------------------------------------------ ; Built-in Operators ;------------------------------------------------------------------------------ -(define ADD +) -(define SUB -) -(define MUL *) -(define DIV /) -(define NOT not) -(define EQ equal?) (define (NE a b) (not (equal? a b))) -(define LT <) -(define GT >) -(define LTE <=) -(define GTE >=) -(define FN_CALL apply) (define (ARRY_IDX coll idx) (cond ((list? coll) @@ -27,11 +16,7 @@ ;------------------------------------------------------------------------------ ; Built-in datatype constructors ;------------------------------------------------------------------------------ -(define VECTOR vector) -(define LIST list) -(define PARAMS list) (define (MAP . args) (alist->hash-table args)) -(define (MACRO) '()) ;------------------------------------------------------------------------------ ; Import necessary libs diff --git a/source/dllexer/dllexer.cpp b/source/dllexer/dllexer.cpp index 94d218b..6186adb 100644 --- a/source/dllexer/dllexer.cpp +++ b/source/dllexer/dllexer.cpp @@ -4,7 +4,7 @@ using namespace std; -#define NUM_SINGLE_CHAR_MATCHES 14 +#define NUM_SINGLE_CHAR_MATCHES 12 SingleCharMatch_T Single_Character_Matches[ NUM_SINGLE_CHAR_MATCHES ] = { { '[', LBRACK }, { ']', RBRACK }, @@ -17,8 +17,6 @@ SingleCharMatch_T Single_Character_Matches[ NUM_SINGLE_CHAR_MATCHES ] = { { '-', SUB }, { '*', MUL }, { '/', DIV }, - { '%', MACRO }, - { ':', SEP }, { '.', MEMB }, }; @@ -52,7 +50,9 @@ bool DLLexer::isOperator(void) || (current == '<') || (current == '>') || (current == '|') - || (current == '&')); + || (current == '&') + || (current == ':') + || (current == '@')); } bool DLLexer::isStringChar(void) @@ -317,6 +317,30 @@ void DLLexer::MultiCharOp(Token& tok) consume(); tok = Token(AND, line, column); } + else if(last == ':') + { + if(current == '=') + { + consume(); + tok = Token(DEFN, line, column); + } + else + { + tok = Token(SEP, line, column); + } + } + else if(last == '@') + { + if(current == '=') + { + consume(); + tok = Token(IMPORT, line, column); + } + else + { + tok = Token(MACRO, line, column); + } + } else { throw Exception(line,column); diff --git a/source/dllexer/dllexer.h b/source/dllexer/dllexer.h index a9d8be5..3e1c10b 100644 --- a/source/dllexer/dllexer.h +++ b/source/dllexer/dllexer.h @@ -37,22 +37,24 @@ typedef enum TokenTypes GT = 26, LTE = 27, GTE = 28, - ASSIGN = 29, - ADD = 30, - SUB = 31, - MUL = 32, - DIV = 33, - MEMB = 34, + ADD = 29, + SUB = 30, + MUL = 31, + DIV = 32, + DEFN = 33, + ASSIGN = 34, + MEMB = 35, + SEP = 36, + ARRY_IDX = 37, + MACRO = 38, + IMPORT = 39, // AST "Virtual" Node Types - MACRO = 40, - SEP = 41, - PROGRAM = 42, - BLOCK = 43, - FN_CALL = 44, - PARAMS = 45, - ARRY_IDX = 46, - PATT = 47, + PROGRAM = 40, + BLOCK = 41, + FN_CALL = 42, + PARAMS = 43, + PATT = 44, } eTokenTypes; typedef struct { diff --git a/source/dlparser/dlparser.cpp b/source/dlparser/dlparser.cpp index cff6417..7334642 100644 --- a/source/dlparser/dlparser.cpp +++ b/source/dlparser/dlparser.cpp @@ -224,7 +224,12 @@ AST* DLParser::Expression(void) AST* DLParser::AssignExpr(void) { AST* ret = LogicalExpr(); - if(lookaheadType(1) == ASSIGN) + if(lookaheadType(1) == DEFN) + { + match(DEFN); + ret = new AST(DEFN, 2, ret, LogicalExpr()); + } + else if(lookaheadType(1) == ASSIGN) { match(ASSIGN); ret = new AST(ASSIGN, 2, ret, LogicalExpr()); diff --git a/source/main.cpp b/source/main.cpp index d53bad1..dbea301 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -41,8 +41,9 @@ int main(int argc, char** argv) output.close(); // Compile the temporary file with chicken scheme - system( string("csc -O5 " + temp_fname).c_str() ); + system( string("csc -O5 -v " + temp_fname).c_str() ); + cout << "Removing temporary files..." << endl; (void)remove( temp_fname.c_str() ); } else diff --git a/source/visitors/scheme/scheme.cpp b/source/visitors/scheme/scheme.cpp index 1d69f8f..f1aa4a0 100644 --- a/source/visitors/scheme/scheme.cpp +++ b/source/visitors/scheme/scheme.cpp @@ -32,55 +32,61 @@ string Scheme::typeToString(ASTNodeType type) case CHAR: ret << "CHAR "; break; case ADD: - ret << "ADD "; break; + ret << "+ "; break; case SUB: - ret << "SUB "; break; + ret << "- "; break; case MUL: - ret << "MUL "; break; + ret << "* "; break; case DIV: - ret << "DIV "; break; + ret << "/ "; break; case AND: ret << "and "; break; case OR: ret << "or "; break; case NOT: - ret << "NOT "; break; + ret << "not "; break; case EQ: - ret << "EQ "; break; + ret << "equal? "; break; case NE: ret << "NE "; break; case LT: - ret << "LT "; break; + ret << "< "; break; case GT: - ret << "GT "; break; + ret << "> "; break; case LTE: - ret << "LTE "; break; + ret << "<= "; break; case GTE: - ret << "GTE "; break; - case MACRO: - ret << "MACRO "; break; - case ASSIGN: + ret << ">= "; break; + case DEFN: ret << "define "; break; + case ASSIGN: + ret << "set! "; break; case PROGRAM: ret << "begin "; break; case VECTOR: - ret << "VECTOR "; break; + ret << "vector "; break; case LIST: - ret << "LIST "; break; + ret << "list "; break; case BLOCK: ret << "begin "; break; case FUNC: ret << "lambda "; break; case FN_CALL: - ret << "FN_CALL "; break; + ret << "apply "; break; case ARRY_IDX: ret << "ARRY_IDX "; break; case SEP: ret << "cons "; break; case MEMB: ret << "hash-table-ref "; break; + + // Print nothing for the following nodes + case MACRO: case PARAMS: break; + + // Print out the type id (this will probably cause an error but also + // alert us to the fact that it is not properly handled) default: ret << type; break; } @@ -99,24 +105,27 @@ void Scheme::afterVisit(AST* cur, int depth) void Scheme::beforeChildren(AST* cur, int depth) { - if (cur->type() == MEMB) + if( cur->type() != MACRO ) { - cur->children()->back()->type(STRING); - } + if (cur->type() == MEMB) + { + cur->children()->back()->type(STRING); + } - if( isDatatype( cur->type() ) ) - { - printDatatype( cur ); - } - else - { - stream << "(" << typeToString( cur->type() ) << cur->text(); + if( isDatatype( cur->type() ) ) + { + printDatatype( cur ); + } + else + { + stream << "(" << typeToString( cur->type() ) << cur->text(); + } } } void Scheme::afterChildren(AST* cur, int depth) { - if( !isDatatype( cur->type() ) ) + if( !isDatatype( cur->type() ) && (cur->type() != MACRO)) { stream << ")"; }