Skip to content

Commit

Permalink
state charts support in parser
Browse files Browse the repository at this point in the history
  • Loading branch information
mbellotti committed Sep 20, 2022
1 parent 9b351fd commit 1d0cc33
Show file tree
Hide file tree
Showing 18 changed files with 3,457 additions and 1,190 deletions.
104 changes: 104 additions & 0 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,14 @@ type Expression interface {
expressionNode()
}

type Operand interface {
Expression
operandNode()
}

type Spec struct {
Statements []Statement
Ext string // Is this a fspec or fsystem?
}

func (s *Spec) TokenLiteral() string {
Expand Down Expand Up @@ -130,6 +136,26 @@ func (sd *SpecDeclStatement) Type() string {
return ""
}

type SysDeclStatement struct {
Token Token
Name *Identifier
}

func (sd *SysDeclStatement) statementNode() {}
func (sd *SysDeclStatement) TokenLiteral() string { return sd.Token.Literal }
func (sd *SysDeclStatement) Position() []int { return sd.Token.Position }
func (sd *SysDeclStatement) String() string {
var out bytes.Buffer

out.WriteString(sd.TokenLiteral() + " ")
out.WriteString(sd.Name.String())
out.WriteString(";")
return out.String()
}
func (sd *SysDeclStatement) Type() string {
return ""
}

type ImportStatement struct {
Token Token
Name *Identifier
Expand Down Expand Up @@ -346,6 +372,7 @@ type Identifier struct {
Value string
}

func (i *Identifier) operandNode() {}
func (i *Identifier) expressionNode() {}
func (i *Identifier) TokenLiteral() string { return i.Token.Literal }
func (i *Identifier) Position() []int { return i.Token.Position }
Expand All @@ -365,6 +392,7 @@ type ParameterCall struct {
Value []string
}

func (p *ParameterCall) operandNode() {}
func (p *ParameterCall) expressionNode() {}
func (p *ParameterCall) TokenLiteral() string { return p.Token.Literal }
func (p *ParameterCall) Position() []int { return p.Token.Position }
Expand Down Expand Up @@ -804,6 +832,57 @@ func (fl *FunctionLiteral) String() string {
}
func (fl *FunctionLiteral) Type() string { return fl.Body.Type() }

type StateLiteral struct {
Token Token
Parameters []*Identifier
Body *BlockStatement
}

func (sl *StateLiteral) expressionNode() {}
func (sl *StateLiteral) TokenLiteral() string { return sl.Token.Literal }
func (sl *StateLiteral) Position() []int { return sl.Token.Position }
func (sl *StateLiteral) String() string {
var out bytes.Buffer
params := []string{}
for _, p := range sl.Parameters {
params = append(params, p.String())
}

out.WriteString(sl.TokenLiteral())
out.WriteString("(")
out.WriteString(strings.Join(params, ", "))
out.WriteString(") ")
out.WriteString(sl.Body.String())

return out.String()
}
func (sl *StateLiteral) Type() string { return sl.Body.Type() }

type BuiltIn struct {
Token Token
Parameters []Operand
Function string
}

func (b *BuiltIn) expressionNode() {}
func (b *BuiltIn) TokenLiteral() string { return b.Token.Literal }
func (b *BuiltIn) Position() []int { return b.Token.Position }
func (b *BuiltIn) String() string {
var out bytes.Buffer
params := []string{}
for _, p := range b.Parameters {
params = append(params, p.String())
}

out.WriteString(b.Function)
out.WriteString("(")
out.WriteString(strings.Join(params, ", "))
out.WriteString(") ")

return out.String()
}
func (b *BuiltIn) Type() string { return "builtin" }

type StringLiteral struct {
Token Token
InferredType *Type
Expand Down Expand Up @@ -897,3 +976,28 @@ func (fl *FlowLiteral) String() string {
return out.String()
}
func (fl *FlowLiteral) Type() string { return "FLOW" }

type ComponentLiteral struct {
Token Token
InferredType *Type
Pairs map[Expression]Expression
}

func (cl *ComponentLiteral) expressionNode() {}
func (cl *ComponentLiteral) TokenLiteral() string { return cl.Token.Literal }
func (cl *ComponentLiteral) Position() []int { return cl.Token.Position }
func (cl *ComponentLiteral) String() string {
var out bytes.Buffer

pairs := []string{}
for key, value := range cl.Pairs {
pairs = append(pairs, key.String()+":"+value.String())
}

out.WriteString("{")
out.WriteString(strings.Join(pairs, ", "))
out.WriteString("}")

return out.String()
}
func (cl *ComponentLiteral) Type() string { return "COMPONENT" }
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ go 1.14

require (
github.com/antlr/antlr4 v0.0.0-20210127041528-be7bc5d81b91
github.com/charmbracelet/bubbletea v0.20.0
github.com/llir/ll v0.0.0-20210426224459-a0543cd69183 // indirect
github.com/llir/llvm v0.3.3
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/olekukonko/tablewriter v0.0.5
golang.org/x/mod v0.4.2 // indirect
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/tools v0.1.0 // indirect
gonum.org/v1/gonum v0.9.3
)
22 changes: 0 additions & 22 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@ github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3
github.com/antlr/antlr4 v0.0.0-20210127041528-be7bc5d81b91 h1:TdusfswSL1/GrSDGiA+AC5c5t76E3o99y1UtGqN6Crw=
github.com/antlr/antlr4 v0.0.0-20210127041528-be7bc5d81b91/go.mod h1:T7PbCXFs94rrTttyxjbyT5+/1V8T2TYDejxUfHJjw1Y=
github.com/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8=
github.com/charmbracelet/bubbletea v0.20.0 h1:/b8LEPgCbNr7WWZ2LuE/BV1/r4t5PyYJtDb+J3vpwxc=
github.com/charmbracelet/bubbletea v0.20.0/go.mod h1:zpkze1Rioo4rJELjRyGlm9T2YNou1Fm4LIJQSa5QMEM=
github.com/containerd/console v1.0.3 h1:lIr7SlA5PxZyMV30bDW0MGbiOPXwc63yRuCP0ARubLw=
github.com/containerd/console v1.0.3/go.mod h1:7LqA/THxQ86k76b8c/EMSiaJ3h1eZkMkXar0TQ1gf3U=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fogleman/gg v1.2.1-0.20190220221249-0403632d5b90/go.mod h1:R/bRT+9gY/C5z7JzPU0zXsXHKM4/ayA+zqcVNZzPa1k=
Expand All @@ -33,22 +29,11 @@ github.com/llir/ll v0.0.0-20210426224459-a0543cd69183 h1:WNFJh/ifKou10AcIrpEiLTL
github.com/llir/ll v0.0.0-20210426224459-a0543cd69183/go.mod h1:8W5HJz80PitAyPZUpOcljQxTu6LD5YKW1URTo+OjVoc=
github.com/llir/llvm v0.3.3 h1:MIoiY0L1+cXn5xIPoTIabiJsiVs11/Pmg1XAY8DqdXU=
github.com/llir/llvm v0.3.3/go.mod h1:2a5gsxl6FHDMmjCPukrHL5WkjuySxHliNtxQ8zC2RMY=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mewmew/float v0.0.0-20201204173432-505706aa38fa h1:R27wrYHe8Zik4z/EV8xxfoH3cwMJw3qI4xsI3yYkGDQ=
github.com/mewmew/float v0.0.0-20201204173432-505706aa38fa/go.mod h1:O+xb+8ycBNHzJicFVs7GRWtruD4tVZI0huVnw5TM01E=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b h1:1XF24mVaiu7u+CFywTdcDo2ie1pzzhwjt6RHqzpMU34=
github.com/muesli/ansi v0.0.0-20211018074035-2e021307bc4b/go.mod h1:fQuZ0gauxyBcmsdE3ZT4NasjaRdxmbCS0jRHsrWu3Ho=
github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739 h1:QANkGiGr39l1EESqrE0gZw0/AJNYzIvoGLhIoVYtluI=
github.com/muesli/termenv v0.11.1-0.20220212125758-44cd13922739/go.mod h1:Bd5NYQ7pd+SrtBSrSNoBBmXlcY8+Xj4BMJgh8qcZrvs=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY=
Expand All @@ -57,7 +42,6 @@ github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w=
Expand Down Expand Up @@ -100,16 +84,10 @@ golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5h
golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210304124612-50617c2ba197/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20210422114643-f5beecf764ed/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171 h1:EH1Deb8WZJ0xc0WK//leUHXcX9aLE5SymusoTmMZye8=
golang.org/x/term v0.0.0-20220411215600-e5f449aeb171/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.5/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
Expand Down
2 changes: 2 additions & 0 deletions grammar/FaultLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ FALSE: 'false';
// State Chart Keywords
ADVANCE: 'advance';
COMPONENT: 'component';
GLOBAL: 'global';
SYSTEM: 'system';
START: 'start';
STATE: 'state';
STAY: 'stay';

Expand Down
18 changes: 17 additions & 1 deletion grammar/FaultParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,37 @@ options {
*/

sysSpec
: sysClause importDecl* componentDecl* (assertion | assumption)? forStmt? eos
: sysClause importDecl* globalDecl* componentDecl* startBlock* (assertion | assumption)? forStmt? eos
;

sysClause
: 'system' IDENT eos
;

globalDecl
: 'global' IDENT '=' operand
;

componentDecl
: 'def' IDENT '=' 'component' '{' (stateLists ',')* '}' eos
;

stateLists
: structProperties
| stateInit
;

stateInit
: IDENT ':' 'state' block
;

startBlock
: 'start' '{' (startPair ',')* '}' eos
;

startPair
: IDENT ':' IDENT
;
/*
Individual specs of state changes
*/
Expand Down

0 comments on commit 1d0cc33

Please sign in to comment.