Skip to content

Commit

Permalink
Merge pull request #106 from AtomCrafty/spawn-statement
Browse files Browse the repository at this point in the history
Spawn statement
  • Loading branch information
eyck authored Nov 5, 2023
2 parents 833ce12 + c9fb2d3 commit c40d654
Show file tree
Hide file tree
Showing 4 changed files with 161 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,24 @@ class CoreDslStatementTest {
'''
.testStatements()
.run();

'''
do {
break;
continue;
} while(0);
'''
.testStatements()
.run();

'''
for(;;) {
break;
continue;
}
'''
.testStatements()
.run();
}

@Test
Expand Down Expand Up @@ -453,19 +471,19 @@ class CoreDslStatementTest {
.run();

'''
void test() {
void test1() {
return;
}
int test() {
int test2() {
return;
}
void test() {
void test3() {
return 0;
}
char test() {
char test4() {
return 128;
}
'''
Expand All @@ -475,4 +493,119 @@ class CoreDslStatementTest {
.expectError(IssueCodes.ReturnTypeNotConvertible, 14)
.run();
}

@Test
def void spawnStatement() {
'''
TEST {
encoding: 0;
behavior: spawn;
}
'''
.testInstruction()
.run();

'''
TEST {
encoding: 0;
behavior: {
spawn {}
}
}
'''
.testInstruction()
.run();

'''
TEST {
encoding: 0;
behavior: {
{};
spawn;
}
}
'''
.testInstruction()
.run();

'''
TEST {
encoding: 0;
behavior: spawn {
unsigned<1> x = 2;
}
}
'''
.testInstruction()
.expectError(IssueCodes.InvalidAssignmentType, 4)
.run();

'''
TEST {
encoding: 0;
behavior: {
spawn;
spawn;
}
}
'''
.testInstruction()
.expectError(IssueCodes.InvalidSpawnStatementPlacement, 4)
.run();

'''
TEST {
encoding: 0;
behavior: {
{
spawn;
}
}
}
'''
.testInstruction()
.expectError(IssueCodes.InvalidSpawnStatementPlacement, 5)
.run();

'''
TEST {
encoding: 0;
behavior: spawn spawn;
}
'''
.testInstruction()
.expectError(IssueCodes.InvalidSpawnStatementPlacement, 3)
.run();

'''
Core X {
always {
TEST {
spawn;
}
}
}
'''
.testProgram()
.expectError(IssueCodes.InvalidSpawnStatementPlacement, 4)
.run();

'''
void test() {
spawn;
}
'''
.testFunction()
.expectError(IssueCodes.InvalidSpawnStatementPlacement, 2)
.run();

'''
void test() {
spawn;
}
'''
.testFunction()
.expectError(IssueCodes.InvalidSpawnStatementPlacement, 2)
.run();
}
}
2 changes: 1 addition & 1 deletion com.minres.coredsl/src/com/minres/coredsl/CoreDsl.xtext
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Statement:
| LoopStatement;

EmptyStatement: {EmptyStatement} ';';
SpawnStatement: 'spawn' body=Statement;
SpawnStatement: t_spawn='spawn' body=Statement;
CompoundStatement: {CompoundStatement} t_openingBrace='{' statements+=Statement* t_closingBrace='}';
ExpressionStatement: expression=AssignmentExpression ';';
DeclarationStatement: declaration=MultiInitDeclaration ';';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,28 @@ class CoreDslAnalyzer {
analyzeStatement(ctx, statement.body);
}

/**
* 1. The statement must be the last child of an instruction's behavior block. <i>(InvalidSpawnStatementPlacement)</i>
*/
def static dispatch void analyzeStatement(AnalysisContext ctx, SpawnStatement statement) {
var isDirectInstrChild = statement.eContainingFeature == CoreDslPackage.Literals.INSTRUCTION__BEHAVIOR;
var isInstrBlockChild = statement.eContainer instanceof CompoundStatement &&
statement.eContainer.eContainingFeature == CoreDslPackage.Literals.INSTRUCTION__BEHAVIOR;

// There are two valid placements for a spawn statement:
// 1. As the direct child of an instruction
// 2. As the last child of a compound statement, which in turn is the direct child of an instruction
if(isInstrBlockChild) {
var block = statement.eContainer as CompoundStatement;
if(block.statements.indexOf(statement) != block.statements.size - 1) {
ctx.acceptError("A spawn statement must be the last statement of an instruction's behavior block", statement,
CoreDslPackage.Literals.SPAWN_STATEMENT__TSPAWN, -1, IssueCodes.InvalidSpawnStatementPlacement)
}
} else if(!isDirectInstrChild) {
ctx.acceptError("A spawn statement must be the last statement of an instruction's behavior block", statement,
CoreDslPackage.Literals.SPAWN_STATEMENT__TSPAWN, -1, IssueCodes.InvalidSpawnStatementPlacement)
}

analyzeStatement(ctx, statement.body);
}

Expand Down Expand Up @@ -868,7 +889,7 @@ class CoreDslAnalyzer {
reportTarget.object, reportTarget.feature, reportTarget.index, IssueCodes.ValidConstantAssignment);
} else {
ctx.acceptError("Cannot implicitly convert " + valueType + " to " + targetType, reportTarget.object,
reportTarget.feature, reportTarget.index, issueCode);
reportTarget.feature, reportTarget.index, issueCode);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ class IssueCodes {
public static val SwitchDuplicateCaseSection = _prefix + 'SwitchDuplicateCaseSection';
public static val SwitchCaseConditionOutOfRange = _prefix + 'SwitchCaseConditionOutOfRange';
public static val StrayControlFlowStatement = _prefix + 'StrayControlFlowStatement';
public static val InvalidSpawnStatementPlacement = _prefix + 'InvalidSpawnStatementPlacement';

// expression issues
public static val ValidConstantAssignment = _prefix + 'ValidConstantAssignment';
Expand Down

0 comments on commit c40d654

Please sign in to comment.