Commit 7878d091 authored by Pascal Held's avatar Pascal Held
Browse files

Add assert directive to parser and type checker

The parser now accepts 'assert(e)' where 'e' is an arbitrary expression.

The type checker verifies that 'e' evaluates to bool.
parent 2e5f8d7f
......@@ -162,6 +162,8 @@ class PCProgram extends PCNode
env.endProcedure()
env.beginNewProcedure(@, "signalAll", new PCTType(PCTType.VOID), [])
env.endProcedure()
env.beginNewProcedure(@, "assert", new PCTType(PCTType.VOID), [])
env.endProcedure()
collectClasses: (env) ->
for c in @children
......@@ -1422,7 +1424,7 @@ class PCProcedureCall extends PCExpression
_getType: (env, className) ->
noArguments = ({"line" : @line, "column" : @column, "wholeFile" : false, "name" : "InvalidType", "message" : "No arguments for procedure that requires arguments!"})
switch @procedureName
when "join", "lock", "unlock", "waitForCondition", "signal"
when "join", "lock", "unlock", "waitForCondition", "signal", "assert"
throw noArguments
throw ({"line" : @line, "column" : @column, "wholeFile" : false, "name" : "InvalidCall", "message" : "Procedures must be called within a procedure definition!"}) if not @insideProcedure()
......@@ -2120,10 +2122,12 @@ class PCPrimitiveStmt extends PCNode
throw ({"line" : @line, "column" : @column, "wholeFile" : false, "name" : "InvalidType", "message" : "lock and unlock must be applied on lock objects, not #{@_type}!"}) if not @_type.isEqual(new PCTType(PCTType.LOCK))
when PCPrimitiveStmt.WAIT
throw ({"line" : @line, "column" : @column, "wholeFile" : false, "name" : "InvalidType", "message" : "waitForCondition can only be used in monitors!"}) if not @insideMonitor()
throw ({"line" : @line, "column" : @column, "wholeFile" : false, "name" : "InvalidType", "message" : "waitForCondition must be applied on condition or boolean objects, not #{@_type}!"}) if not @_type.isEqual(new PCTType(PCTType.Bool)) and not @_type.isEqual(new PCTType(PCTType.CONDITION))
throw ({"line" : @line, "column" : @column, "wholeFile" : false, "name" : "InvalidType", "message" : "waitForCondition must be applied on condition or boolean objects, not #{@_type}!"}) if not @_type.isEqual(new PCTType(PCTType.BOOL)) and not @_type.isEqual(new PCTType(PCTType.CONDITION))
when PCPrimitiveStmt.SIGNAL, PCPrimitiveStmt.SIGNAL_ALL
throw ({"line" : @line, "column" : @column, "wholeFile" : false, "name" : "InvalidType", "message" : "signal and signalAll can only be used in monitors!"}) if not @insideMonitor()
throw ({"line" : @line, "column" : @column, "wholeFile" : false, "name" : "InvalidType", "message" : "signal and signalAll must be applied on condition objects, not #{@_type}!"}) if @children.length > 0 and not @_type.isEqual(new PCTType(PCTType.CONDITION))
when PCPrimitiveStmt.ASSERT
throw ({"line" : @line, "column" : @column, "wholeFile" : false, "name" : "InvalidType", "message" : "assert must be applied on boolean objects, not #{@_type}!"}) if not @_type.isEqual(new PCTType(PCTType.BOOL))
new PCTType(PCTType.VOID)
PCPrimitiveStmt.JOIN = 0
......@@ -2132,6 +2136,7 @@ PCPrimitiveStmt.UNLOCK = 2
PCPrimitiveStmt.WAIT = 3
PCPrimitiveStmt.SIGNAL = 4
PCPrimitiveStmt.SIGNAL_ALL = 5
PCPrimitiveStmt.ASSERT = 6
PCPrimitiveStmt.kindToString = (kind) ->
switch kind
......@@ -2141,6 +2146,7 @@ PCPrimitiveStmt.kindToString = (kind) ->
when PCPrimitiveStmt.WAIT then "waitForCondition"
when PCPrimitiveStmt.SIGNAL then "signal"
when PCPrimitiveStmt.SIGNAL_ALL then "signalAll"
when PCPrimitiveStmt.ASSERT then "assert"
###
# @brief Representation of the println statement.
......
......@@ -945,6 +945,11 @@ PrimitiveStatement
location().start.column,
PCPrimitiveStmt.SIGNAL_ALL);
}
/ "assert" __ "(" __ exp:Expression __ ")" __ ";"
{
return new PCPrimitiveStmt(location().start.line,
location().start.column, PCPrimitiveStmt.ASSERT, exp);
}
/*
* The print line statement print a single line and start with the keyword
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment