Permalink
Browse files

Dropped multiple calc demos

Dropped multiple calc demos because it's too much work.  Also added
better error handling.
  • Loading branch information...
1 parent d3a1889 commit 2617cdd6c19beccf12df951aa2d3fae3537394f0 @kfields committed Dec 18, 2012
View
@@ -35,5 +35,4 @@ solution "LemonWreck"
--buildoptions { "-std=c++11" }
--buildoptions { "-std=gnu++0x" }
- include "../src/lwastcalc"
include "../src/lwcalc"
View
@@ -1,83 +0,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-#include <malloc.h>
-
-#include "token.h"
-#include "calculator.h"
-#include "scanner.h"
-#include "parser.h"
-
-scanner_token *eval_ast(calculator* calc, ast_node* ast)
-{
- switch(ast->kind){
- case AST_LITERAL:
- return ast->token;
- break;
- case AST_BINARY:
- switch(ast->token->kind){
- case TOKEN_ADD:
- return add(calc, eval_ast(calc, ast->node.binary.left), eval_ast(calc, ast->node.binary.right));
- case TOKEN_SUB:
- return subtract(calc, eval_ast(calc, ast->node.binary.left), eval_ast(calc, ast->node.binary.right));
- case TOKEN_MUL:
- return multiply(calc, eval_ast(calc, ast->node.binary.left), eval_ast(calc, ast->node.binary.right));
- case TOKEN_DIV:
- return divide(calc, eval_ast(calc, ast->node.binary.left), eval_ast(calc, ast->node.binary.right));
- }
- }
- return NULL;
-}
-
-int eval_string(calculator* calc, char* s)
-{
- scanner_token *token;
- scanner_state *state;
-
- void* pParser = ParseAlloc(malloc);
- ParseTrace(stdout, ">>");
-
- state = malloc(sizeof(scanner_state));
-
- state->cursor = s;
-
- for(;;) {
- token = scan(state);
- if(token == NULL) break;
- Parse(pParser, token->kind, token, calc);
- }
- Parse(pParser, 0, 0, calc);
- ParseFree(pParser, free);
-
- calc->answer = eval_ast(calc, calc->ast)->data.num;
- return 0;
-}
-void error(calculator *calc, char *errmsg)
-{
- calc->errormsg = errmsg;
-}
-void clear(calculator* calc)
-{
- calc->answer = 0;
- calc->errormsg = NULL;
-}
-scanner_token* add(calculator* calc, scanner_token* b, scanner_token* c)
-{
- return create_float_token(b->data.num + c->data.num);
-}
-scanner_token* subtract(calculator* calc, scanner_token* b, scanner_token* c)
-{
- return create_float_token(b->data.num - c->data.num);
-}
-scanner_token* multiply(calculator* calc, scanner_token* b, scanner_token* c)
-{
- return create_float_token(b->data.num * c->data.num);
-}
-scanner_token* divide(calculator* calc, scanner_token* b, scanner_token* c)
-{
- if(c->data.num == 0){
- error(calc, "Division by zero");
- return create_float_token(0);
- }
- else
- return create_float_token(b->data.num / c->data.num);
-}
View
@@ -1,20 +0,0 @@
-#ifndef _CALCULATOR_H
-#define _CALCULATOR_H
-
-#include "ast.h"
-
-typedef struct _calculator {
- ast_node *ast;
- float answer;
- char* errormsg;
-} calculator;
-
-int eval_string(calculator* calc, char* s);
-void error(calculator *calc, char *errmsg);
-
-scanner_token* add(calculator* calc, scanner_token* B, scanner_token* C);
-scanner_token* subtract(calculator* calc, scanner_token* B, scanner_token* C);
-scanner_token* multiply(calculator* calc, scanner_token* B, scanner_token* C);
-scanner_token* divide(calculator* calc, scanner_token* B, scanner_token* C);
-
-#endif //_CALCULATOR_H
Oops, something went wrong.

0 comments on commit 2617cdd

Please sign in to comment.