-
Notifications
You must be signed in to change notification settings - Fork 0
binary expression #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4a2387a
binary expression
shenao78 4c2a26e
fix interpreter
shenao78 894c5a8
add new line for file
shenao78 edc1b87
add new line for file
shenao78 bffa9a2
add bool value
shenao78 f438040
add logic operaotr
shenao78 0ca845c
add true false literal
shenao78 464192f
opt print bool value
shenao78 7b9b3b3
opt bool literal
shenao78 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| comp: | ||
| lex src/lex.l | ||
| yacc -d src/grammar.y | ||
| gcc -o bin/main y.tab.c lex.yy.c | ||
| gcc -o bin/main y.tab.c lex.yy.c src/summoner.c src/interpreter.c |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,221 @@ | ||
|
|
||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include "interpreter.h" | ||
| #include "summoner.h" | ||
|
|
||
| ExprValue evalIntBinaryExpression(ExpressionType type, int left, int right) | ||
| { | ||
| ExprValue v; | ||
| v.type = EXPR_INT_VALUE; | ||
| switch (type) | ||
| { | ||
| case ADD_EXPRESSION: | ||
| v.u.int_value = left + right; | ||
| break; | ||
| case SUB_EXPRESSION: | ||
| v.u.int_value = left - right; | ||
| break; | ||
| case MUL_EXPRESSION: | ||
| v.u.int_value = left * right; | ||
| break; | ||
| case DIV_EXPRESSION: | ||
| v.u.int_value = left / right; | ||
| break; | ||
| case LT_EXPRESSION: | ||
| v.type = EXPR_BOOL_VALUE; | ||
| v.u.boolean_value = left < right; | ||
| break; | ||
| case LE_EXPRESSION: | ||
| v.type = EXPR_BOOL_VALUE; | ||
| v.u.boolean_value = left <= right; | ||
| break; | ||
| case GT_EXPRESSION: | ||
| v.type = EXPR_BOOL_VALUE; | ||
| v.u.boolean_value = left > right; | ||
| break; | ||
| case GE_EXPRESSION: | ||
| v.type = EXPR_BOOL_VALUE; | ||
| v.u.boolean_value = left >= right; | ||
| break; | ||
| case EQ_EXPRESSION: | ||
| v.type = EXPR_BOOL_VALUE; | ||
| v.u.boolean_value = left == right; | ||
| break; | ||
| case NE_EXPRESSION: | ||
| v.type = EXPR_BOOL_VALUE; | ||
| v.u.boolean_value = left != right; | ||
| break; | ||
| default: | ||
| printf("invalid expression type\n"); | ||
| exit(1); | ||
| } | ||
| return v; | ||
| } | ||
|
|
||
| ExprValue evalDoubleBinaryExpression(ExpressionType type, double left, double right) | ||
| { | ||
| ExprValue v; | ||
| v.type = EXPR_DOUBLE_VALUE; | ||
| switch (type) | ||
| { | ||
| case ADD_EXPRESSION: | ||
| v.u.double_value = left + right; | ||
| break; | ||
| case SUB_EXPRESSION: | ||
| v.u.double_value = left - right; | ||
| break; | ||
| case MUL_EXPRESSION: | ||
| v.u.double_value = left * right; | ||
| break; | ||
| case DIV_EXPRESSION: | ||
| v.u.double_value = left / right; | ||
| break; | ||
| case LT_EXPRESSION: | ||
| v.type = EXPR_BOOL_VALUE; | ||
| v.u.boolean_value = left < right; | ||
| break; | ||
| case LE_EXPRESSION: | ||
| v.type = EXPR_BOOL_VALUE; | ||
| v.u.boolean_value = left <= right; | ||
| break; | ||
| case GT_EXPRESSION: | ||
| v.type = EXPR_BOOL_VALUE; | ||
| v.u.boolean_value = left > right; | ||
| break; | ||
| case GE_EXPRESSION: | ||
| v.type = EXPR_BOOL_VALUE; | ||
| v.u.boolean_value = left >= right; | ||
| break; | ||
| case EQ_EXPRESSION: | ||
| v.type = EXPR_BOOL_VALUE; | ||
| v.u.boolean_value = left == right; | ||
| break; | ||
| case NE_EXPRESSION: | ||
| v.type = EXPR_BOOL_VALUE; | ||
| v.u.boolean_value = left != right; | ||
| break; | ||
| default: | ||
| printf("invalid expression type\n"); | ||
| exit(1); | ||
| } | ||
| return v; | ||
| } | ||
|
|
||
| ExprValue evalBoolBinaryExpression(ExpressionType type, bool left, bool right) | ||
| { | ||
| ExprValue v; | ||
| v.type = EXPR_BOOL_VALUE; | ||
| switch (type) | ||
| { | ||
| case AND_EXPRESSION: | ||
| v.u.boolean_value = left && right; | ||
| break; | ||
| case OR_EXPRESSION: | ||
| v.u.boolean_value = left || right; | ||
| break; | ||
| default: | ||
| printf("invalid expression type when eval bool value:%d\n", type); | ||
| exit(1); | ||
| } | ||
| return v; | ||
| } | ||
|
|
||
| ExprValue evalBinaryExpression(ExpressionType type, BinaryExpression *binaryExpression) | ||
| { | ||
| ExprValue leftVal = evalExpression(binaryExpression->left); | ||
| ExprValue rightVal = evalExpression(binaryExpression->right); | ||
| if (leftVal.type == EXPR_INT_VALUE && rightVal.type == EXPR_INT_VALUE) | ||
| { | ||
| return evalIntBinaryExpression(type, leftVal.u.int_value, rightVal.u.int_value); | ||
| } | ||
|
|
||
| if (leftVal.type == EXPR_INT_VALUE && rightVal.type == EXPR_DOUBLE_VALUE) | ||
| { | ||
| return evalDoubleBinaryExpression(type, leftVal.u.int_value, rightVal.u.double_value); | ||
| } | ||
|
|
||
| if (leftVal.type == EXPR_DOUBLE_VALUE && rightVal.type == EXPR_INT_VALUE) | ||
| { | ||
| return evalDoubleBinaryExpression(type, leftVal.u.double_value, rightVal.u.int_value); | ||
| } | ||
|
|
||
| if (leftVal.type == EXPR_DOUBLE_VALUE && rightVal.type == EXPR_DOUBLE_VALUE) | ||
| { | ||
| return evalDoubleBinaryExpression(type, leftVal.u.double_value, rightVal.u.double_value); | ||
| } | ||
|
|
||
| if (leftVal.type == EXPR_BOOL_VALUE && rightVal.type == EXPR_BOOL_VALUE) | ||
| { | ||
| return evalBoolBinaryExpression(type, leftVal.u.boolean_value, rightVal.u.boolean_value); | ||
| } | ||
|
|
||
| printf("invalid expression type, left:%d, right:%d", leftVal.type, rightVal.type); | ||
| exit(1); | ||
| } | ||
|
|
||
| ExprValue evalExpression(Expression *expr) | ||
| { | ||
| ExprValue v; | ||
| switch (expr->type) | ||
| { | ||
| case INT_EXPRESSION: | ||
| v.type = EXPR_INT_VALUE; | ||
| v.u.int_value = expr->u.int_value; | ||
| return v; | ||
| case DOUBLE_EXPRESSION: | ||
| v.type = EXPR_DOUBLE_VALUE; | ||
| v.u.double_value = expr->u.double_value; | ||
| return v; | ||
| case MINUS_EXPRESSION: | ||
| v = evalExpression(expr->u.unary_expression); | ||
| if (v.type == EXPR_INT_VALUE) | ||
| { | ||
| v.u.int_value = -v.u.int_value; | ||
| } | ||
| else | ||
| { | ||
| v.u.double_value = -v.u.double_value; | ||
| } | ||
| return v; | ||
| case NOT_EXPRESSION: | ||
| v = evalExpression(expr->u.unary_expression); | ||
| v.u.boolean_value = !v.u.boolean_value; | ||
| return v; | ||
| case ADD_EXPRESSION: | ||
| case SUB_EXPRESSION: | ||
| case MUL_EXPRESSION: | ||
| case DIV_EXPRESSION: | ||
| case LT_EXPRESSION: | ||
| case LE_EXPRESSION: | ||
| case GT_EXPRESSION: | ||
| case GE_EXPRESSION: | ||
| case EQ_EXPRESSION: | ||
| case NE_EXPRESSION: | ||
| case AND_EXPRESSION: | ||
| case OR_EXPRESSION: | ||
| return evalBinaryExpression(expr->type, expr->u.binary_expression); | ||
| default: | ||
| printf("invalid expression type when eval expression:%d\n", expr->type); | ||
| exit(1); | ||
| } | ||
| } | ||
|
|
||
| void printExprValue(ExprValue val) | ||
| { | ||
| switch (val.type) | ||
| { | ||
| case EXPR_INT_VALUE: | ||
| printf(">>>%d\n", val.u.int_value); | ||
| break; | ||
| case EXPR_DOUBLE_VALUE: | ||
| printf(">>>%lf\n", val.u.double_value); | ||
| break; | ||
| case EXPR_BOOL_VALUE: | ||
| printf(">>>%d\n", val.u.boolean_value); | ||
| break; | ||
| default: | ||
| printf("invalid expression type when print expr value:%d", val.type); | ||
| exit(1); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| #ifndef _INTERPRETER_H_ | ||
| #define _INTERPRETER_H_ | ||
|
|
||
| #include "summoner.h" | ||
|
|
||
| ExprValue evalExpression(Expression *expr); | ||
| void printExprValue(ExprValue); | ||
|
|
||
| #endif |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| #include <stdio.h> | ||
| #include <stdlib.h> | ||
| #include "summoner.h" | ||
|
|
||
| Expression *allocExpression(ExpressionType type) { | ||
| Expression *expr = malloc(sizeof(Expression)); | ||
| expr->type = type; | ||
| return expr; | ||
| } | ||
|
|
||
| Expression *allocIntExpression(int value) { | ||
| Expression *expr = allocExpression(INT_EXPRESSION); | ||
| expr->u.int_value = value; | ||
| return expr; | ||
| } | ||
|
|
||
| Expression *allocDoubleExpression(double value) { | ||
| Expression *expr = allocExpression(DOUBLE_EXPRESSION); | ||
| expr->u.double_value = value; | ||
| return expr; | ||
| } | ||
|
|
||
| Expression *allocBoolExpression(bool value) { | ||
| Expression *expr = allocExpression(BOOL_EXPRESSION); | ||
| expr->u.boolean_value = value; | ||
| return expr; | ||
| } | ||
|
|
||
| Expression *allocUnaryExpression(ExpressionType type, Expression *unaryExpr) { | ||
| Expression *expr = allocExpression(type); | ||
| expr->u.unary_expression = unaryExpr; | ||
| return expr; | ||
| } | ||
|
|
||
| Expression *allocBinaryExpression(ExpressionType type, Expression *left, Expression *right) { | ||
| Expression *expr = allocExpression(type); | ||
| BinaryExpression *binary = malloc(sizeof(BinaryExpression)); | ||
| binary->left = left; | ||
| binary->right = right; | ||
| expr->u.binary_expression = binary; | ||
| return expr; | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
grammer没定义bool expr吧
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
后面把所有的运算符都加上