Skip to content
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

Adds pattern matching to GDScript #6845

Merged
merged 3 commits into from
Jan 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 66 additions & 1 deletion modules/gdscript/gd_compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,72 @@ Error GDCompiler::_parse_block(CodeGen& codegen,const GDParser::BlockNode *p_blo

switch(cf->cf_type) {


case GDParser::ControlFlowNode::CF_MATCH: {
GDParser::MatchNode *match = cf->match;

GDParser::IdentifierNode *id = memnew(GDParser::IdentifierNode);
id->name = "#match_value";

// var #match_value
// copied because there is no _parse_statement :(
codegen.add_stack_identifier(id->name, p_stack_level++);
codegen.alloc_stack(p_stack_level);
new_identifiers++;

GDParser::OperatorNode *op = memnew(GDParser::OperatorNode);
op->op=GDParser::OperatorNode::OP_ASSIGN;
op->arguments.push_back(id);
op->arguments.push_back(match->val_to_match);

int ret = _parse_expression(codegen, op, p_stack_level);
if (ret < 0) {
return ERR_PARSE_ERROR;
}

// break address
codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
codegen.opcodes.push_back(codegen.opcodes.size() + 3);
int break_addr = codegen.opcodes.size();
codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
codegen.opcodes.push_back(0); // break addr

for (int j = 0; j < match->compiled_pattern_branches.size(); j++) {
GDParser::MatchNode::CompiledPatternBranch branch = match->compiled_pattern_branches[j];

// jump over continue
// jump unconditionally
// continue address
// compile the condition
int ret = _parse_expression(codegen, branch.compiled_pattern, p_stack_level);
if (ret < 0) {
return ERR_PARSE_ERROR;
}

codegen.opcodes.push_back(GDFunction::OPCODE_JUMP_IF);
codegen.opcodes.push_back(ret);
codegen.opcodes.push_back(codegen.opcodes.size() + 3);
int continue_addr = codegen.opcodes.size();
codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
codegen.opcodes.push_back(0);



Error err = _parse_block(codegen, branch.body, p_stack_level, p_break_addr, continue_addr);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If p_break_addr is changed to break_addr then break will work inside of a pattern branch. So no big changes needed to support that. However I think it would be a bad idea to do that since it will cause a lot of confusion amongst newbies and may lead people the wrong way.

break isn't match-idiomatic, so I think it would be a mistake to implement it that way.

if (err) {
return ERR_PARSE_ERROR;
}

codegen.opcodes.push_back(GDFunction::OPCODE_JUMP);
codegen.opcodes.push_back(break_addr);

codegen.opcodes[continue_addr + 1] = codegen.opcodes.size();
}

codegen.opcodes[break_addr + 1] = codegen.opcodes.size();


} break;

case GDParser::ControlFlowNode::CF_IF: {

#ifdef DEBUG_ENABLED
Expand Down
Loading