Skip to content

Commit

Permalink
grammar updates and converting string to boolean in ir
Browse files Browse the repository at this point in the history
  • Loading branch information
mbellotti committed Apr 28, 2023
1 parent 1e39479 commit addce77
Show file tree
Hide file tree
Showing 9 changed files with 1,169 additions and 871 deletions.
7 changes: 6 additions & 1 deletion grammar/FaultParser.g4
Expand Up @@ -9,7 +9,7 @@ options {
*/

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

sysClause
Expand Down Expand Up @@ -64,6 +64,7 @@ declaration
| structDecl
| assertion
| assumption
| stringDecl
;

comparison
Expand All @@ -83,6 +84,10 @@ constSpec
: identList ('=' constants)?
;

stringDecl
: IDENT '=' string_ eos
;

identList
: operandName (',' operandName)*
;
Expand Down
19 changes: 19 additions & 0 deletions listener/listener.go
Expand Up @@ -1218,6 +1218,25 @@ func (l *FaultListener) ExitFloat_(c *parser.Float_Context) {
})
}

func (l *FaultListener) ExitStringDecl(c *parser.StringDeclContext) {
token := util.GenerateToken("STRING", "STRING", c.GetStart(), c.GetStop())

val := l.pop()
token2 := util.GenerateToken("IDENT", "IDENT", c.GetStart(), c.GetStop())

ident := &ast.Identifier{
Token: token2,
Value: c.IDENT().GetText(),
Spec: l.currSpec,
}

if _, ok := val.(*ast.StringLiteral); !ok {
panic(fmt.Sprintf("top of the stack is not a string got %T: line %d col %d", val, c.GetStart().GetLine(), c.GetStart().GetColumn()))
}

l.push(&ast.DefStatement{Token: token, Name: ident, Value: val.(ast.Expression)})
}

func (l *FaultListener) ExitString_(c *parser.String_Context) {
token := util.GenerateToken("STRING", "STRING", c.GetStart(), c.GetStop())

Expand Down
23 changes: 17 additions & 6 deletions llvm/compiler.go
Expand Up @@ -184,17 +184,22 @@ func (c *Compiler) processSpec(root ast.Node, isImport bool) ([]*ast.AssertionSt
c.hasRunBlock = true
continue
case *ast.DefStatement:
if cm, ok := n.Value.(*ast.ComponentLiteral); ok {
switch d := n.Value.(type) {
case *ast.ComponentLiteral:
//assembling component parts as params
id := cm.Id()
id := d.Id()
s := c.specStructs[id[0]]
branches, err := s.FetchComponent(id[1])
if err != nil {
panic(err)
}
params := c.generateParameters(cm.Id(), branches, true)
params := c.generateParameters(d.Id(), branches, true)
c.sysGlobals = append(c.sysGlobals, params...)
case *ast.StringLiteral:
value := c.compileValue(d)
c.globalVariable(d.ProcessedName, value, d.Position())
}

}
}
default:
Expand Down Expand Up @@ -233,8 +238,13 @@ func (c *Compiler) compile(node ast.Node) {
case *ast.ConstantStatement:
c.compileConstant(v)
case *ast.DefStatement:
c.compileStruct(v)

switch v.Value.(type) {
case *ast.FlowLiteral, *ast.StockLiteral, *ast.ComponentLiteral:
c.compileStruct(v)
case *ast.StringLiteral:
value := c.compileValue(v)
c.globalVariable(v.Name.ProcessedName, value, v.Position())
}
case *ast.FunctionLiteral:

case *ast.InfixExpression:
Expand Down Expand Up @@ -357,7 +367,8 @@ func (c *Compiler) compileValue(node ast.Node) value.Value {
case *ast.FloatLiteral:
return constant.NewFloat(irtypes.Double, v.Value)
case *ast.StringLiteral:
return constant.NewCharArrayFromString(v.Value)
return constant.NewBool(false)
//return constant.NewCharArrayFromString(v.Value)
case *ast.Boolean:
return constant.NewBool(v.Value)
case *ast.Natural:
Expand Down
33 changes: 32 additions & 1 deletion llvm/llvm_test.go
Expand Up @@ -33,7 +33,7 @@ func TestSimpleConst(t *testing.T) {
@test1_y = global double 0x3FF3333333333333
@test1_a = global i1 true
@test1_b = global i1 false
@test1_c = global [14 x i8] c"\22Hello World!\22"
@test1_c = global i1 false
define void @__run() {
block-0:
Expand Down Expand Up @@ -682,6 +682,37 @@ func TestIndexExp(t *testing.T) {
}
}

func TestStringExp(t *testing.T) {
test := `spec test;
str1 = "is a fish";
str2 = "tastes delicious with ginger";
str3 = "native to North America";
assume str1 && str3;
assert str3;
`

expecting := ``

llvm, err := prepTest(test, true)

if err != nil {
t.Fatalf("compilation failed on valid spec. got=%s", err)
}

ir, err := validateIR(llvm)

if err != nil {
t.Fatalf("generated IR is not valid. got=%s", err)
}

err = compareResults(llvm, expecting, string(ir))

if err != nil {
t.Fatalf(err.Error())
}
}

func compareResults(llvm string, expecting string, ir string) error {
if !strings.Contains(ir, "source_filename = \"<stdin>\"") {
return fmt.Errorf("optimized ir not valid. \ngot=%s", ir)
Expand Down
1,939 changes: 1,076 additions & 863 deletions parser/fault_parser.go

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions parser/faultparser_base_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions parser/faultparser_base_visitor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions parser/faultparser_listener.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions parser/faultparser_visitor.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit addce77

Please sign in to comment.