Skip to content

Commit

Permalink
Feat: add ref-expression for make sure to reference a value
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaokang00010 committed Feb 4, 2023
1 parent 6bf1606 commit 24be87f
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion frontend/ast.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace rex {
memberExpression, // member expression like aaa.bbb.ccc,
// both 2 terms in expression are subscriptExpression
// primary means match a literal or memberExpression
uniqueExpression, // like -[primary], --[primary], ++[primary], ![primary]
uniqueExpression, // like -[primary], --[primary], ++[primary], ![primary], *[primary], #[primary]
multiplicationExpression, // multiplication expressions like a * b, a / b, a % b
additionExpression, // addition expressions like a + b, a - b
binaryShiftExpression, // like a << b, a >> b
Expand Down
10 changes: 9 additions & 1 deletion frontend/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ namespace rex {
return curToken = orStart();
} else if (curCh == L'^') {
return curToken = xorStart();
} else if (curCh == L'\0') {
} else if (curCh == L'#') {
return curToken = sharpStart();
}else if (curCh == L'\0') {
return curToken = {line, col, token::tokenKind::eof, false};
} else {
throw unexpectedTokenException(line, col, curCh);
Expand Down Expand Up @@ -418,6 +420,12 @@ namespace rex {
return tok;
}

lexer::token lexer::sharpStart() {
lexer::token tok{line, col, token::tokenKind::sharp};
getCh();
return tok;
}

lexer::token::vBasicValue::vBasicValue(vint v) : vInt(v) {

}
Expand Down
3 changes: 3 additions & 0 deletions frontend/lexer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ namespace rex {
colon, // :
comma, // ,
dot, // .
sharp, // #
kFor,
kForEach,
kWhile,
Expand Down Expand Up @@ -167,6 +168,8 @@ namespace rex {

token dotStart();

token sharpStart();

token leftParenthesesStart();

token rightParenthesesStart();
Expand Down
3 changes: 2 additions & 1 deletion frontend/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,8 @@ namespace rex {
lex.curToken.kind == lexer::token::tokenKind::decrementSign or
lex.curToken.kind == lexer::token::tokenKind::minus or
lex.curToken.kind == lexer::token::tokenKind::binaryAnd or
lex.curToken.kind == lexer::token::tokenKind::asterisk) {
lex.curToken.kind == lexer::token::tokenKind::asterisk or
lex.curToken.kind == lexer::token::tokenKind::sharp) {
AST vOperator = {AST::treeKind::operators, lex.curToken};
lex.scan();
AST vItem = parseMemberExpression();
Expand Down
5 changes: 4 additions & 1 deletion interpreter/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ namespace rex {
}
case AST::treeKind::uniqueExpression: {
value rhs = interpret(target.child[1]);
value &r = rhs.isRef() ? rhs.getRef() : rhs;
value &r = eleGetRef(rhs);
switch (target.child[0].leaf.kind) {
case lexer::token::tokenKind::minus: {
switch (r.kind) {
Expand Down Expand Up @@ -428,6 +428,9 @@ namespace rex {
}
return dest;
}
case lexer::token::tokenKind::sharp: {
return eleRefObj(rhs);
}
default: {
return {};
}
Expand Down
2 changes: 0 additions & 2 deletions interpreter/value.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ namespace rex {
case vKind::vObject:
case vKind::vNull:
case vKind::vNativeFuncPtr:
break;
case vKind::vRef:
this->getRef().deepCopy(dest);
break;
case vKind::vLinkedListIter: {
dest.linkedListIterObj = managePtr(*linkedListIterObj);
Expand Down

0 comments on commit 24be87f

Please sign in to comment.