-
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 3 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,113 @@ | ||
|
|
||
| #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; | ||
| 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; | ||
| default: | ||
| printf("invalid expression type\n"); | ||
| 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); | ||
| } | ||
|
|
||
| return evalDoubleBinaryExpression(type, leftVal.u.double_value, rightVal.u.double_value); | ||
| } | ||
|
|
||
| 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 ADD_EXPRESSION: | ||
| case SUB_EXPRESSION: | ||
| case MUL_EXPRESSION: | ||
| case DIV_EXPRESSION: | ||
| return evalBinaryExpression(expr->type, expr->u.binary_expression); | ||
| default: | ||
| printf("invalid expression 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; | ||
| default: | ||
| printf("invalid expression 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; | ||
| } | ||
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,74 @@ | ||
| #ifndef _SUMMONER_H_ | ||
| #define _SUMMONER_H_ | ||
|
|
||
| #include <stdbool.h> | ||
|
|
||
| typedef enum | ||
| { | ||
| EXPR_BOOL_VALUE = 1, | ||
| EXPR_INT_VALUE, | ||
| EXPR_DOUBLE_VALUE, | ||
| } ExprValueType; | ||
|
|
||
| typedef struct ExprValue | ||
| { | ||
| ExprValueType type; | ||
| union | ||
| { | ||
| bool boolean_value; | ||
| int int_value; | ||
| double double_value; | ||
| } u; | ||
|
|
||
| } ExprValue; | ||
|
|
||
| typedef enum | ||
| { | ||
| BOOL_EXPRESSION = 1, | ||
| INT_EXPRESSION, | ||
| DOUBLE_EXPRESSION, | ||
| ADD_EXPRESSION, | ||
| SUB_EXPRESSION, | ||
| MUL_EXPRESSION, | ||
| DIV_EXPRESSION, | ||
| MINUS_EXPRESSION, | ||
| MOD_EXPRESSION, /* % */ | ||
| EQ_EXPRESSION, /* == */ | ||
| NE_EXPRESSION, /* != */ | ||
| GT_EXPRESSION, /* > */ | ||
| GE_EXPRESSION, /* >= */ | ||
| LT_EXPRESSION, /* < */ | ||
| LE_EXPRESSION, /* <= */ | ||
|
|
||
| } ExpressionType; | ||
|
|
||
| typedef struct BinaryExpression BinaryExpression; | ||
|
|
||
| typedef struct Expression | ||
| { | ||
| ExpressionType type; | ||
| union | ||
| { | ||
| bool boolean_value; | ||
| int int_value; | ||
| double double_value; | ||
| BinaryExpression *binary_expression; | ||
| struct Expression *unary_expression; | ||
| } u; | ||
|
|
||
| } Expression; | ||
|
|
||
| typedef struct BinaryExpression | ||
| { | ||
| Expression *left; | ||
| Expression *right; | ||
| } BinaryExpression; | ||
|
|
||
| Expression *allocExpression(ExpressionType type); | ||
| Expression *allocIntExpression(int value); | ||
| Expression *allocDoubleExpression(double value); | ||
| Expression *allocBoolExpression(bool value); | ||
| Expression *allocUnaryExpression(ExpressionType type, Expression *unaryExpr); | ||
| Expression *allocBinaryExpression(ExpressionType type, Expression *left, Expression *right); | ||
|
|
||
| #endif |
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.
后面把所有的运算符都加上