Created
August 22, 2013 10:13
-
-
Save anonymous/6305491 to your computer and use it in GitHub Desktop.
This file contains 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
/* * | |
* Bython * | |
* */ | |
#include <stdio.h> | |
#include <string> | |
void substr(const char* src, int offset, int length, char* return_buffer) { | |
strncpy(return_buffer, src+offset, length); | |
} | |
typedef struct{ | |
int index; | |
int error; | |
} parse; | |
int expr1(const char* const, parse*); | |
int expr2(const char* const, parse*); | |
int expr3(const char* const, parse*); | |
/* | |
{0 to 9} ... number | |
S {a to z | A to Z | _} S ... name | |
?????? ... funarg | |
name \( funarg \) ... function | |
expr2 {(+ | -) expr2} ... expr1 | |
expr3 {(* | /) expr3} ... expr2 | |
S ( \( expr1 \) | number ) S ... expr3 | |
*/ | |
void _(char* program, char* result){ | |
parse option = {0, 0}; | |
int ret = expr1(program, &option); | |
sprintf(result, "%d(%sindex: %d)", ret, option.error?"Error, ":"", option.index); | |
} | |
void skips(const char* const program, parse* option){ | |
while(program[option->index] == ' '){ | |
option->index++; | |
} | |
} | |
int number(const char* const program, parse* option){ | |
int result = 0; | |
if(program[option->index] < '0' || '9' < program[option->index]){ | |
option->error = 1; | |
return 0; | |
} | |
while('0' <= program[option->index] && program[option->index] <= '9'){ | |
result = result * 10 + program[option->index] - '0'; | |
option->index++; | |
} | |
return result; | |
} | |
void name(const char* const program, parse* option, char* name){ | |
int old_index = option->index; | |
if(!( 'a' <= program[option->index] && program[option->index] <= 'z' | |
|| 'A' <= program[option->index] && program[option->index] <= 'Z' | |
|| '_' == program[option->index])){ | |
option->error = 1; | |
return; | |
} | |
while( 'a' <= program[option->index] && program[option->index] <= 'z' | |
|| 'A' <= program[option->index] && program[option->index] <= 'Z' | |
|| '0' <= program[option->index] && program[option->index] <= '9' | |
|| '_' == program[option->index]){ | |
option->index++; | |
} | |
substr(program, old_index, option->index-old_index, name); | |
} | |
//int function(const char* const program, parse* option){ | |
/* name \( funarg \) ... function*/ | |
/* int old_index = option->index; | |
char name[255]; | |
int args[255]; | |
name(program, option, name); | |
if(program[option->index] == '('){ | |
option->index++; | |
args = funarg(program, option); | |
if(program[option->index] != ')'){ | |
option->index = old_index; | |
option->error = 1; | |
return 0; | |
} | |
option->index++; | |
}else{ | |
option->error = 1; | |
return 0; | |
} | |
name | |
return 0; | |
}*/ | |
int expr1(const char* const program, parse* option){ | |
int result = expr2(program, option); | |
while(1){ | |
switch(program[option->index]){ | |
case '+': | |
option->index++; | |
result += expr2(program, option); | |
break; | |
case '-': | |
option->index++; | |
result -= expr2(program, option); | |
break; | |
default: | |
return result; | |
} | |
if(option->error){ | |
option->index--; | |
option->error=0; | |
return result; | |
} | |
} | |
} | |
int expr2(const char* const program, parse* option){ | |
int result = expr3(program, option); | |
while(1){ | |
switch(program[option->index]){ | |
case '*': | |
option->index++; | |
result *= expr3(program, option); | |
break; | |
case '/': | |
option->index++; | |
result /= expr3(program, option); | |
break; | |
default: | |
return result; | |
} | |
if(option->error){ | |
option->index--; | |
option->error=0; | |
return result; | |
} | |
} | |
} | |
int expr3(const char* const program, parse* option){ | |
int result = 0; | |
int old_index = option->index; | |
skips(program, option); | |
if(program[option->index] == '('){ | |
option->index++; | |
result = expr1(program, option); | |
if(program[option->index] != ')'){ | |
option->index = old_index; | |
option->error = 1; | |
return 0; | |
} | |
option->index++; | |
}else{ | |
result = number(program, option); | |
} | |
skips(program, option); | |
return result; | |
} | |
int main(void){ | |
string s; | |
while(1){ | |
char result[50]; | |
printf("> "); | |
fgets(s, 50, stdin); | |
if(s == NULL){ | |
printf("ERROR.\n"); | |
return -1; | |
}else if(!strcmp(s, "exit\n")){ | |
printf("Exit.\n"); | |
return 0; | |
} | |
_(s, result); | |
printf("%s\n", result); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment