Skip to content

Commit 01fff27

Browse files
committed
Thoeretically complete semantic analysis
1 parent 72156eb commit 01fff27

File tree

3 files changed

+50
-31
lines changed

3 files changed

+50
-31
lines changed

lang-silk/src/main/scala/net/codingwell/weave/languages/silk/Semantic.scala

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,7 @@ class Connection () extends ConnectionSignal {
3737
def isDriven() = { ! input.isEmpty }
3838
}
3939

40-
class Value () { }
41-
42-
class ConnectionValue () extends Value {}
43-
44-
class ModuleInstance () extends Value {}
40+
class ModuleInstance ( module:ModuleSymbol, lhs:ConnectionSignal, rhs:ConnectionSignal ) extends ConnectionSignal {}
4541

4642
class TemporaryConnection () extends ConnectionSignal {}
4743

@@ -52,9 +48,11 @@ class Gate_OR ( val a:ConnectionSignal, val b:ConnectionSignal ) extends Connec
5248
trait ExpressionState
5349
{
5450
def processExpression( expression:ast.Expression ):Unit
51+
52+
def getValue():ConnectionSignal
5553
}
5654

57-
class ExpressionModuleState( val machine:ExpressionStateMachine, val value:Unit ) extends ExpressionState {
55+
class ExpressionModuleState( val machine:ExpressionStateMachine, val value:ConnectionSignal ) extends ExpressionState {
5856
def processExpression( expression:ast.Expression ) = {
5957
expression.next match {
6058
case None =>
@@ -67,10 +65,10 @@ class ExpressionModuleState( val machine:ExpressionStateMachine, val value:Unit
6765
symbol match {
6866
case Some( DeclarationSymbol( connection ) ) =>
6967
throw new Exception("Value followed by value")
70-
case Some( ModuleSymbol( name, parameters ) ) =>
68+
case Some( symbol @ ModuleSymbol( name, parameters ) ) =>
7169
//TODO: Handle parameters
7270
println( "ID is Module in Module State" )
73-
machine.state = new ExpressionModuleHalfState( machine, (), () )
71+
machine.state = new ExpressionModuleHalfState( machine, symbol, value )
7472
case None =>
7573
println( "No ID" )
7674
}
@@ -85,9 +83,11 @@ class ExpressionModuleState( val machine:ExpressionStateMachine, val value:Unit
8583
println("Chain Expressions not implemented." )
8684
}
8785
}
86+
87+
def getValue() = { value }
8888
}
8989

90-
class ExpressionModuleHalfState( val machine:ExpressionStateMachine, val module:Unit, val rhs:Unit ) extends ExpressionState {
90+
class ExpressionModuleHalfState( val machine:ExpressionStateMachine, val module:ModuleSymbol, val rhs:ConnectionSignal ) extends ExpressionState {
9191
def processExpression( expression:ast.Expression ) = {
9292
expression.next match {
9393
case None =>
@@ -100,7 +100,7 @@ class ExpressionModuleHalfState( val machine:ExpressionStateMachine, val module:
100100
symbol match {
101101
case Some( DeclarationSymbol( connection ) ) =>
102102
println( "ID is Value in HalfState" )
103-
machine.state = new ExpressionModuleState( machine, () )
103+
machine.state = new ExpressionModuleState( machine, new ModuleInstance( module, connection, rhs ) )
104104
case Some( ModuleSymbol( name, parameters ) ) =>
105105
println( "ID is Module" )
106106
throw new Exception("Value expected got module")
@@ -115,7 +115,7 @@ class ExpressionModuleHalfState( val machine:ExpressionStateMachine, val module:
115115
val submachine = new ExpressionStateMachine( machine.scope, machine.table )
116116
expressions.reverseIterator foreach ( submachine.processExpression _ )
117117

118-
machine.state = new ExpressionModuleState( machine, () )
118+
machine.state = new ExpressionModuleState( machine, new ModuleInstance( module, submachine.getValue, rhs ) )
119119

120120
case unknown =>
121121
println( "Value State Unknown: " + unknown.toString() )
@@ -124,9 +124,11 @@ class ExpressionModuleHalfState( val machine:ExpressionStateMachine, val module:
124124
println("Chain Expressions not implemented." )
125125
}
126126
}
127+
128+
def getValue():ConnectionSignal = { throw new Exception("Module missing left-hand-side") }
127129
}
128130

129-
class ExpressionValueState( val machine:ExpressionStateMachine, val value:Unit ) extends ExpressionState {
131+
class ExpressionValueState( val machine:ExpressionStateMachine, val value:ConnectionSignal ) extends ExpressionState {
130132
def processExpression( expression:ast.Expression ) = {
131133
expression.next match {
132134
case None =>
@@ -139,10 +141,10 @@ class ExpressionValueState( val machine:ExpressionStateMachine, val value:Unit )
139141
symbol match {
140142
case Some( DeclarationSymbol( connection ) ) =>
141143
throw new Exception("Value followed by value")
142-
case Some( ModuleSymbol( name, parameters ) ) =>
144+
case Some( symbol @ ModuleSymbol( name, parameters ) ) =>
143145
//TODO: Handle parameters
144146
println( "ID is Module" )
145-
machine.state = new ExpressionModuleHalfState( machine, (), () )
147+
machine.state = new ExpressionModuleHalfState( machine, symbol, value )
146148
case None =>
147149
println( "No ID" )
148150
}
@@ -158,9 +160,7 @@ class ExpressionValueState( val machine:ExpressionStateMachine, val value:Unit )
158160
}
159161
}
160162

161-
def getValue():Unit = {
162-
value
163-
}
163+
def getValue():ConnectionSignal = { value }
164164
}
165165

166166
class ExpressionValueEmptyState( val machine:ExpressionStateMachine ) extends ExpressionState {
@@ -176,7 +176,7 @@ class ExpressionValueEmptyState( val machine:ExpressionStateMachine ) extends Ex
176176

177177
symbol match {
178178
case Some( DeclarationSymbol( connection ) ) =>
179-
machine.state = new ExpressionValueState( machine, () )
179+
machine.state = new ExpressionValueState( machine, connection )
180180
case Some( ModuleSymbol( name, parameters ) ) =>
181181
println( "ID is Module" )
182182
throw new Exception("Value expected got module")
@@ -192,7 +192,7 @@ class ExpressionValueEmptyState( val machine:ExpressionStateMachine ) extends Ex
192192
expressions.reverseIterator foreach ( submachine.processExpression _ )
193193
println( ")" )
194194

195-
machine.state = new ExpressionValueState( machine, () )
195+
machine.state = new ExpressionValueState( machine, submachine.getValue )
196196
case unknown =>
197197
println( "Value State Unknown: " + unknown.toString() )
198198
}
@@ -201,17 +201,15 @@ class ExpressionValueEmptyState( val machine:ExpressionStateMachine ) extends Ex
201201
}
202202
}
203203

204-
def getValue():Unit = {
205-
throw new Exception("Missing Value")
206-
}
204+
def getValue() = { throw new Exception("Missing Value") }
207205
}
208206

209207
class ExpressionStateMachine( val scope:SymbolScope, val table:SymbolTable ) {
210208
var state:ExpressionState = new ExpressionValueEmptyState( this )
211209

212210
def processExpression( expression:ast.Expression ):Unit = { state.processExpression( expression ) }
213211

214-
def getValue():Unit = { }
212+
def getValue():ConnectionSignal = { state.getValue }
215213
}
216214

217215
class SemanticModule( ) {

lang-silk/src/main/scala/net/codingwell/weave/languages/silk/SilkCompiler.scala

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,39 @@ import akka.util.duration._
2121
import scala.collection.mutable.{Map => MutableMap}
2222

2323
class SilkCompiler extends Actor {
24-
24+
def timed[R](blockName:String)(block: =>R) = {
25+
val start = System.currentTimeMillis
26+
val result = block
27+
println("Block (" + blockName + ") took " + (System.currentTimeMillis - start) + "ms.")
28+
result
29+
}
2530
// val map = new MutableMap[UntypedChannel,String]
2631

2732
def receive = {
2833
case WeaveCompiler.NotifyWork(actor, source,target) =>
2934
implicit val timeout = Timeout(5 seconds)
3035
val future = actor.ask( WeaveCompiler.RequestWork( source, target ) ).mapTo[WeaveCompiler.Work[WeaveFile]]
3136
val work = Await.result( future, timeout.duration )
32-
if(work != null) compile( work.value )
37+
if(work != null) timed("Compiling")( compile( work.value ) )
3338
}
3439

3540
def compile( file:WeaveFile ):Unit = {
36-
println("File")
41+
val start = System.currentTimeMillis
3742

3843
val buffer = new IncludableInputBuffer[String]
39-
buffer.include(0, Preprocessor.StripComments( file.contents ), file.name, 0);//Load the first file
44+
timed("Stripping")( buffer.include(0, Preprocessor.StripComments( file.contents ), file.name, 0) )//Load the first file
45+
println( ":1: " + (System.currentTimeMillis - start) )
4046

4147
val parser = new net.codingwell.weave.languages.silk.SilkParser(buffer)
42-
val parserunner = RecoveringParseRunner(parser.File)
48+
println( ":2: " + (System.currentTimeMillis - start) )
49+
val parserunner = RecoveringParseRunner(parser.File)//TODO: Cache this, takes 200ms to construct.
50+
println( ":3: " + (System.currentTimeMillis - start) )
4351

4452
val input = new Input( null, (A:Array[Char]) => buffer ) //Forces the use of our buffer
53+
println( ":4: " + (System.currentTimeMillis - start) )
4554

46-
val result = parserunner.run( input )
55+
val result = timed("Parsing")( parserunner.run( input ) )
56+
println( ":5: " + (System.currentTimeMillis - start) )
4757

4858
if( result.hasErrors() )
4959
{
@@ -58,12 +68,13 @@ class SilkCompiler extends Actor {
5868
case Some(file:ast.File) =>
5969
val symboltable = new SymbolTable
6070
val visitor = new ASTRTLVisitor( symboltable )
61-
visitor visit file
71+
timed("Visiting")( visitor visit file )
6272
val semantic = new Semantic( symboltable )
63-
semantic.process()
73+
timed("Semantic")( semantic.process )
6474
case _ => throw new Error("Slik AST missing")
6575
}
6676
}
77+
println( ":: " + (System.currentTimeMillis - start) )
6778
}
6879

6980
def supportedLanguages() = Set("Silk")

samples/test3.silk

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,13 @@ module fulladder
1313
sum = s1^ c_in;
1414
c_out = ( a & b ) | (s1&c_in);
1515
}
16+
17+
module halfadder
18+
in bit a;
19+
in bit b;
20+
out bit sum;
21+
out bit c_out;
22+
{
23+
sum = a ^ b;
24+
c_out = a & b;
25+
}

0 commit comments

Comments
 (0)