You are given a string expression representing a Lisp-like expression to return the integer value of.
The syntax for these expressions is given as follows.
An expression is either an integer, a let-expression, an add-expression, a mult-expression, or an assigned variable. Expressions always evaluate to a single integer.
(An integer could be positive or negative.)
A let-expression takes the form (let v1 e1 v2 e2 ... vn en expr), where let is always the string "let", then there are 1 or more pairs of alternating variables and expressions, meaning that the first variable v1 is assigned the value of the expression e1, the second variable v2 is assigned the value of the expression e2, and so on sequentially; and then the value of this let-expression is the value of the expression expr.
An add-expression takes the form (add e1 e2) where add is always the string "add", there are always two expressions e1, e2, and this expression evaluates to the addition of the evaluation of e1 and the evaluation of e2.
A mult-expression takes the form (mult e1 e2) where mult is always the string "mult", there are always two expressions e1, e2, and this expression evaluates to the multiplication of the evaluation of e1 and the evaluation of e2.
For the purposes of this question, we will use a smaller subset of variable names. A variable starts with a lowercase letter, then zero or more lowercase letters or digits. Additionally for your convenience, the names "add", "let", or "mult" are protected and will never be used as variable names.
Finally, there is the concept of scope. When an expression of a variable name is evaluated, within the context of that evaluation, the innermost scope (in terms of parentheses) is checked first for the value of that variable, and then outer scopes are checked sequentially. It is guaranteed that every expression is legal. Please see the examples for more details on scope.
Evaluation Examples:
Input: (add 1 2)
Output: 3
Input: (mult 3 (add 2 3))
Output: 15
Input: (let x 2 (mult x 5))
Output: 10
Input: (let x 2 (mult x (let x 3 y 4 (add x y))))
Output: 14
Explanation: In the expression (add x y), when checking for the value of the variable x,
we check from the innermost scope to the outermost in the context of the variable we are trying to evaluate.
Since x = 3 is found first, the value of x is 3.
Input: (let x 3 x 2 x)
Output: 2
Explanation: Assignment in let statements is processed sequentially.
Input: (let x 1 y 2 x (add x y) (add x y))
Output: 5
Explanation: The first (add x y) evaluates as 3, and is assigned to x.
The second (add x y) evaluates as 3+2 = 5.
Input: (let x 2 (add (let x 3 (let x 4 x)) x))
Output: 6
Explanation: Even though (let x 4 x) has a deeper scope, it is outside the context
of the final x in the add-expression. That final x will equal 2.
Input: (let a1 3 b2 (add a1 1) b2)
Output 4
Explanation: Variable names can contain digits after the first character.
Note:
The given string expression is well formatted: There are no leading or trailing spaces, there is only a single space separating different components of the string, and no space between adjacent parentheses. The expression is guaranteed to be legal and evaluate to an integer.
The length of expression is at most 2000. (It is also non-empty, as that would not be a legal expression.)
The answer and all intermediate calculations of that answer are guaranteed to fit in a 32-bit integer.
typedef struct LinkedListNodeStruct {
void *val;
char *key;
struct LinkedListNodeStruct *next;
}*Node;
typedef Node SequentialSearchST;
//Support set key/value to a empty list(NULL), under which circumstance a new list's returned.
SequentialSearchST SequentialSearchSTSet(SequentialSearchST st, char *key, void *val) {
if (st != NULL) {
Node node = st;
while (node != NULL) {
if (strcmp(key, node->key) == 0) {
node->val = val;
return st;
}
}
}
Node node = (Node)malloc(sizeof(*node));
node->key = key;
node->val = val;
node->next = st;
return node;
}
SequentialSearchST SequentialSearchSTCopy(SequentialSearchST st) {
SequentialSearchST nst = NULL;
Node node = st;
while (node != NULL) {
nst = SequentialSearchSTSet(nst, node->key, node->val);
node = node->next;
}
return nst;
}
/**
* Return 0 for empty list(NULL) and absent keys.
*/
void* SequentialSearchSTGet(SequentialSearchST st, char *key) {
Node node = st;
while (node != NULL) {
if (node->key != NULL && strcmp(node->key, key) == 0) { return node->val; }
node = node->next;
}
return 0;
}
bool SequentialSearchSTContains(SequentialSearchST st, char *key) {
Node node = st;
while (node != NULL) {
if (node->key != NULL && strcmp(node->key, key) == 0) { return true; }
node = node->next;
}
return false;
}
/**
* Just set the hit key to -1 so that further searches will just miss.
*/
void SequentialSearchSTRemove(SequentialSearchST st, char *key) {
Node node = st;
while (node != NULL) {
if (node->key != NULL && strcmp(node->key, key) == 0) {
node->key = NULL;
return;
}
node = node->next;
}
}
typedef Node Stack;
Stack StackPush(Stack stk, void *val) {
Node node = (Node)malloc(sizeof(*node));
node->val = val;
node->next = stk;
stk = node;
return stk;
}
Stack StackPop(Stack stk) {
if (stk == NULL) return NULL;
Node node = stk;
stk = stk->next;
free(node);
return stk;
}
void* StackTop(Stack stk) { return stk != NULL ? stk->val : NULL; }
#define H_SIZE 2003
unsigned long str_hash(unsigned char *str) {
unsigned long hash = 5381;
int c;
while (c = *str++)
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
return hash % H_SIZE;
}
int* Int(int i) {
int *p = (int*)malloc(sizeof(*p));
*p = i;
return p;
}
char* Char(char c) {
char *p = (char*)malloc(sizeof(*p));
*p = c;
return p;
}
typedef struct {
SequentialSearchST *st;
}* HashMap;
HashMap HashMapCreate() {
HashMap hm = (HashMap)malloc(sizeof(*hm));
hm->st = (SequentialSearchST*)malloc(H_SIZE * sizeof(SequentialSearchST));
for (int i = 0; i < H_SIZE; i++) { hm->st[i] = NULL; }
return hm;
}
HashMap HashMapCopy(HashMap hm) {
HashMap nhm = HashMapCreate();
for (int i = 0; i < H_SIZE; i++) { if (hm->st[i] != NULL) { nhm->st[i] = SequentialSearchSTCopy(hm->st[i]); } }
return nhm;
}
bool HashMapContains(HashMap hm, char *key) {
int hash = str_hash(key);
if (hm->st[hash] == NULL) return false;
return SequentialSearchSTContains(hm->st[hash], key);
}
void HashMapSet(HashMap hm, char *key, int val) {
int hash = str_hash(key);
hm->st[hash] = SequentialSearchSTSet(hm->st[hash], key, Int(val));
}
int HashMapGet(HashMap hm, char *key) {
int hash = str_hash(key);
SequentialSearchST st = hm->st[hash];
return *(int*)SequentialSearchSTGet(st, key);
}
int add(int a, int b) { return a + b; }
int mul(int a, int b) { return a * b; }
#define LET 1
#define ADD 2
#define MULT 3
#define VAR 4
#define VAL 5
/**
0: let
1: add
2: mul
3: var
4: val
*/
int TypeOfToken(char *token) {
if (strcmp(token, "let") == 0) return LET;
else if (strcmp(token, "add") == 0) return ADD;
else if (strcmp(token, "mult") == 0) return MULT;
else if (!isdigit(*token)) return VAR;
else return VAL;
}
char* KeywordInToken(char *token) {
token = token + (*token == '(' ? 1 : 0);
size_t size = strchr(token, ')') - token;
if (size < strlen(token)) {
char *kw = (char*)malloc((size + 1) * sizeof(*kw));
strncpy(kw, token, size);
kw[size] = '\0';
return kw;
} else {
return token;
}
}
char* firstToken(char *expression, size_t *toklen) {
char *schr = strchr(expression, ' ');
char *pchr = strchr(expression, ')');
if (schr != NULL && pchr != NULL) {
*toklen = (schr < pchr ? schr : pchr) - expression;
} else if (schr != NULL) {
*toklen = schr - expression;
} else {
*toklen = pchr - expression;
}
char *token = (char*)malloc((*toklen + 1) * sizeof(*token));
token[*toklen] = '\0';
strncpy(token, expression, *toklen);
return token;
}
int evaluate(char * expression){
Stack ops = NULL;
Stack vals = NULL;
Stack hms = NULL;
Stack toks = NULL;//tokens to be mapped...
while (*expression != '\0') {
char c = *expression;
if (c == '(') {
if (*(expression + 1) == 'l') {
ops = StackPush(ops, Int(LET));
HashMap hm = NULL;
if (StackTop(hms) != NULL) {
hm = HashMapCopy(StackTop(hms));
} else {
hm = HashMapCreate();
}
hms = StackPush(hms, hm);
expression += 5;
} else if (*(expression + 1) == 'a') {
ops = StackPush(ops, Int(ADD));
expression += 5;
} else if (*(expression + 1) == 'm') {
ops = StackPush(ops, Int(MULT));
expression += 6;
}
} else if (c == ')') {
int op = *(int*)StackTop(ops);
ops = StackPop(ops);
int val;
if (op == LET) {
val = *(int*)StackTop(vals);
vals = StackPop(vals);
hms = StackPop(hms);
} else if (op == ADD) {
int v1 = *(int*)StackTop(vals);
vals = StackPop(vals);
int v2 = *(int*)StackTop(vals);
vals = StackPop(vals);
val = v1 + v2;
} else if (op == MULT) {
int v1 = *(int*)StackTop(vals);
vals = StackPop(vals);
int v2 = *(int*)StackTop(vals);
vals = StackPop(vals);
val = v1 * v2;
}
if (ops == NULL) {
return val;
} else {
if (toks != NULL) {
char *tok = StackTop(toks);
HashMap hm = StackTop(hms);
HashMapSet(hm, tok, val);
toks = StackPop(toks);
} else {
vals = StackPush(vals, Int(val));
}
}
expression += 1;
expression += *expression == ' ' ? 1 : 0;
} else if (isdigit(c) || c == '-') {//number
int sign = c == '-' ? -1 : 1;
if (c == '-') { expression += 1; }
size_t numlen;
char *str = firstToken(expression, &numlen);
int val = sign * atoi(str);
if (toks != NULL) {
if (*(int*)StackTop(ops) == LET) {
char *tok = StackTop(toks);
HashMap hm = StackTop(hms);
HashMapSet(hm, tok, val);
toks = StackPop(toks);
} else {
vals = StackPush(vals, Int(val));
}
} else {
vals = StackPush(vals, Int(val));
}
expression += numlen;
expression += *expression == ' ' ? 1 : 0;
} else {//token
size_t toklen;
char *tok = firstToken(expression, &toklen);
HashMap hm = StackTop(hms);
if (HashMapContains(hm, tok)) {
if (*(int*)StackTop(ops) == LET) {
if (toks == NULL) {
if (*(expression + toklen) == ')') {
vals = StackPush(vals, Int(HashMapGet(hm, tok)));
} else {
toks = StackPush(toks, tok);
}
} else {
char *stok = StackTop(toks);
toks = StackPop(toks);
HashMapSet(hm, stok, HashMapGet(hm, tok));
}
} else {
vals = StackPush(vals, Int(HashMapGet(hm, tok)));
}
} else {
toks = StackPush(toks, tok);
}
expression += toklen;
expression += *expression == ' ' ? 1 : 0;
}
}
return 0;
}
网友评论