Skip to content

Commit

Permalink
Initial compiler outline
Browse files Browse the repository at this point in the history
This puts in place an initial outline of the compiler, though there
remains quite a lot of work to be done.
  • Loading branch information
DavePearce committed Nov 25, 2024
1 parent b256e1b commit b52abec
Show file tree
Hide file tree
Showing 33 changed files with 1,422 additions and 1,018 deletions.
2 changes: 0 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ linters-settings:
severity: warning
confidence: 0.8
rules:
- name: indent-error-flow
severity: warning
- name: errorf
severity: warning
- name: context-as-argument
Expand Down
6 changes: 5 additions & 1 deletion cmd/testgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import (

"github.com/consensys/gnark-crypto/ecc/bls12-377/fr"
cmdutil "github.com/consensys/go-corset/pkg/cmd"
"github.com/consensys/go-corset/pkg/corset"
"github.com/consensys/go-corset/pkg/hir"
sc "github.com/consensys/go-corset/pkg/schema"
"github.com/consensys/go-corset/pkg/sexp"
tr "github.com/consensys/go-corset/pkg/trace"
"github.com/consensys/go-corset/pkg/trace/json"
"github.com/consensys/go-corset/pkg/util"
Expand Down Expand Up @@ -185,8 +187,10 @@ func readSchemaFile(filename string) *hir.Schema {
fmt.Println(err)
os.Exit(1)
}
// Package up as source file
srcfile := sexp.NewSourceFile(filename, bytes)
// Attempt to parse schema
schema, err2 := hir.ParseSchemaString(string(bytes))
schema, err2 := corset.CompileSourceFile(srcfile)
// Check whether parsed successfully or not
if err2 == nil {
// Ok
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var checkCmd = &cobra.Command{
//
stats := util.NewPerfStats()
// Parse constraints
hirSchema = readSchemaFile(args[1])
hirSchema = readSchema(args[1:])
//
stats.Log("Reading constraints file")
// Parse trace file
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var debugCmd = &cobra.Command{
air := GetFlag(cmd, "air")
stats := GetFlag(cmd, "stats")
// Parse constraints
hirSchema := readSchemaFile(args[0])
hirSchema := readSchema(args)

// Print constraints
if stats {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var testCmd = &cobra.Command{
//
stats := util.NewPerfStats()
// Parse constraints
hirSchema = readSchemaFile(args[0])
hirSchema = readSchema(args)
//
stats.Log("Reading constraints file")
//
Expand Down
86 changes: 56 additions & 30 deletions pkg/cmd/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/consensys/go-corset/pkg/binfile"
"github.com/consensys/go-corset/pkg/corset"
"github.com/consensys/go-corset/pkg/hir"
"github.com/consensys/go-corset/pkg/sexp"
"github.com/consensys/go-corset/pkg/trace"
Expand Down Expand Up @@ -133,54 +134,79 @@ func readTraceFile(filename string) []trace.RawColumn {
return nil
}

// Parse a constraints schema file using a parser based on the extension of the
// filename.
func readSchemaFile(filename string) *hir.Schema {
func readSchema(filenames []string) *hir.Schema {
if len(filenames) == 0 {
fmt.Println("source or binary constraint(s) file required.")
os.Exit(5)
} else if len(filenames) == 1 && path.Ext(filenames[0]) == "bin" {
// Single (binary) file supplied
return readBinaryFile(filenames[0])
}
// Must be source files
return readSourceFiles(filenames)
}

// Read a "bin" file.
func readBinaryFile(filename string) *hir.Schema {
var schema *hir.Schema
// Read schema file
bytes, err := os.ReadFile(filename)
// Handle errors
if err == nil {
// Check file extension
ext := path.Ext(filename)
//
switch ext {
case ".lisp":
// Parse bytes into an S-Expression
schema, err = hir.ParseSchemaString(string(bytes))
if err == nil {
return schema
}
case ".bin":
schema, err = binfile.HirSchemaFromJson(bytes)
if err == nil {
return schema
}
default:
err = fmt.Errorf("Unknown schema file format: %s\n", ext)
// Read the binary file
schema, err = binfile.HirSchemaFromJson(bytes)
if err == nil {
return schema
}
}
// Handle error
if e, ok := err.(*sexp.SyntaxError); ok {
printSyntaxError(filename, e, string(bytes))
} else {
fmt.Println(err)
}

// Handle error & exit
fmt.Println(err)
os.Exit(2)
// unreachable
return nil
}

// Parse a set of source files and compile them into a single schema. This can
// result, for example, in a syntax error, etc.
func readSourceFiles(filenames []string) *hir.Schema {
srcfiles := make([]*sexp.SourceFile, len(filenames))
// Read each file
for i, n := range filenames {
// Read source file
bytes, err := os.ReadFile(n)
// Sanity check for errors
if err != nil {
fmt.Println(err)
os.Exit(3)
}
//
srcfiles[i] = sexp.NewSourceFile(n, bytes)
}
// Parse and compile source files
schema, errs := corset.CompileSourceFiles(srcfiles)
// Check for any errors
if errs == nil {
return schema
}
// Report errors
for _, err := range errs {
printSyntaxError(&err)
}
// Fail
os.Exit(4)
// unreachable
return nil
}

// Print a syntax error with appropriate highlighting.
func printSyntaxError(filename string, err *sexp.SyntaxError, text string) {
func printSyntaxError(err *sexp.SyntaxError) {
span := err.Span()
// Construct empty source map in order to determine enclosing line.
srcmap := sexp.NewSourceMap[sexp.SExp]([]rune(text))
srcmap := sexp.NewSourceMap[sexp.SExp](err.SourceFile().Contents())
//
line := srcmap.FindFirstEnclosingLine(span)
// Print error + line number
fmt.Printf("%s:%d: %s\n", filename, line.Number(), err.Message())
fmt.Printf("%s:%d: %s\n", err.SourceFile().Filename(), line.Number(), err.Message())
// Print separator line
fmt.Println()
// Print line
Expand Down
Loading

0 comments on commit b52abec

Please sign in to comment.