From 36798cbabac865188cd43b04cce2b818d39ce140 Mon Sep 17 00:00:00 2001 From: John Hodge Date: Tue, 7 Sep 2010 18:20:44 +0800 Subject: [PATCH] SpiderScript - For loop, #if'd out experimental code --- .../Libraries/libspiderscript.so_src/ast.c | 3 ++ .../Libraries/libspiderscript.so_src/ast.h | 1 + .../Libraries/libspiderscript.so_src/parse.c | 30 +++++++++++++++++-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/Usermode/Libraries/libspiderscript.so_src/ast.c b/Usermode/Libraries/libspiderscript.so_src/ast.c index 7d644cdc..b662e320 100644 --- a/Usermode/Libraries/libspiderscript.so_src/ast.c +++ b/Usermode/Libraries/libspiderscript.so_src/ast.c @@ -188,6 +188,7 @@ size_t AST_GetNodeSize(tAST_Node *Node) return ret; } +#if 0 /** * \brief Write a node to a file */ @@ -195,6 +196,7 @@ void AST_WriteNode(FILE *FP, tAST_Node *Node) { tAST_Node *node; intptr_t ptr; + int ret; if(!Node) return ; @@ -315,6 +317,7 @@ void AST_WriteNode(FILE *FP, tAST_Node *Node) } return ret; } +#endif /** * \brief Free a node and all subnodes diff --git a/Usermode/Libraries/libspiderscript.so_src/ast.h b/Usermode/Libraries/libspiderscript.so_src/ast.h index 1c4ab4b6..565c94aa 100644 --- a/Usermode/Libraries/libspiderscript.so_src/ast.h +++ b/Usermode/Libraries/libspiderscript.so_src/ast.h @@ -203,6 +203,7 @@ extern tAST_Node *AST_NewCodeBlock(void); extern void AST_AppendNode(tAST_Node *Parent, tAST_Node *Child); extern tAST_Node *AST_NewIf(tParser *Parser, tAST_Node *Condition, tAST_Node *True, tAST_Node *False); +extern tAST_Node *AST_NewLoop(tParser *Parser, tAST_Node *Init, int bPostCheck, tAST_Node *Condition, tAST_Node *Increment, tAST_Node *Code); extern tAST_Node *AST_NewAssign(tParser *Parser, int Operation, tAST_Node *Dest, tAST_Node *Value); extern tAST_Node *AST_NewCast(tParser *Parser, int Target, tAST_Node *Value); diff --git a/Usermode/Libraries/libspiderscript.so_src/parse.c b/Usermode/Libraries/libspiderscript.so_src/parse.c index 3ac6fa84..3ad4081c 100644 --- a/Usermode/Libraries/libspiderscript.so_src/parse.c +++ b/Usermode/Libraries/libspiderscript.so_src/parse.c @@ -48,7 +48,7 @@ void SyntaxAssert(tParser *Parser, int Have, int Want); tAST_Script *Parse_Buffer(tSpiderVariant *Variant, char *Buffer) { tParser parser = {0}; - tParser *Parser = &parser; //< Keeps code consitent + tParser *Parser = &parser; //< Keeps code consistent tAST_Script *ret; tAST_Node *mainCode; char *name; @@ -194,6 +194,10 @@ tAST_Node *Parse_DoBlockLine(tParser *Parser) switch(LookAhead(Parser)) { + // Empty statement + case TOK_SEMICOLON: + GetToken(Parser); + return NULL; // Return from a method case TOK_RWD_RETURN: @@ -219,9 +223,31 @@ tAST_Node *Parse_DoBlockLine(tParser *Parser) } return ret; case TOK_RWD_FOR: + { + tAST_Node *init=NULL, *cond=NULL, *inc=NULL, *code; + GetToken(Parser); // Eat 'for' + SyntaxAssert(Parser, GetToken(Parser), TOK_PAREN_OPEN); + + if(LookAhead(Parser) != TOK_SEMICOLON) + init = Parse_DoExpr0(Parser); + + SyntaxAssert(Parser, GetToken(Parser), TOK_SEMICOLON); + if(LookAhead(Parser) != TOK_SEMICOLON) + cond = Parse_DoExpr0(Parser); + + SyntaxAssert(Parser, GetToken(Parser), TOK_SEMICOLON); + if(LookAhead(Parser) != TOK_SEMICOLON) + inc = Parse_DoExpr0(Parser); + + SyntaxAssert(Parser, GetToken(Parser), TOK_PAREN_CLOSE); + + code = Parse_DoCodeBlock(Parser); + ret = AST_NewLoop(Parser, init, 0, cond, inc, code); + } + return ret; case TOK_RWD_DO: case TOK_RWD_WHILE: - TODO(Parser, "Implement if, for, do and while\n"); + TODO(Parser, "Implement do and while\n"); break; // Define Variables -- 2.20.1