Skip to content

Commit

Permalink
asm: rename constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
fjl committed Nov 27, 2024
1 parent 1ad7606 commit 5508a73
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 8 deletions.
18 changes: 16 additions & 2 deletions asm/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import (
"github.com/fjl/geas/internal/evm"
)

// Compiler performs the assembling.
// Compiler turns assembly source into bytecode.
type Compiler struct {
fsys fs.FS
lexDebug bool
Expand All @@ -43,8 +43,16 @@ type Compiler struct {
errors errorList
}

// NewCompiler creates a compiler. The passed file system is used to resolve import file names.
// NewCompiler creates a compiler.
// Deprecated: use New.
func NewCompiler(fsys fs.FS) *Compiler {
return New(fsys)
}

// New creates a compiler.
// The file system is used to resolve import file names. If a nil FS is given,
// #import cannot be used.
func New(fsys fs.FS) *Compiler {
return &Compiler{
fsys: fsys,
macroStack: make(map[*ast.InstructionMacroDef]struct{}),
Expand All @@ -55,6 +63,12 @@ func NewCompiler(fsys fs.FS) *Compiler {
}
}

// SetFilesystem sets the file system used for resolving #include files.
// Note: if set to a nil FS, #include is not allowed.
func (c *Compiler) SetFilesystem(fsys fs.FS) {
c.fsys = fsys
}

// SetDebugLexer enables/disables printing of the token stream to stdout.
func (c *Compiler) SetDebugLexer(on bool) {
c.lexDebug = on
Expand Down
2 changes: 1 addition & 1 deletion asm/compiler_expand.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (inst includeStatement) expand(c *Compiler, doc *ast.Document, prog *compil

// expand of #assemble performs compilation of the given assembly file.
func (inst assembleStatement) expand(c *Compiler, doc *ast.Document, prog *compilerProg) error {
subc := NewCompiler(c.fsys)
subc := New(c.fsys)
subc.SetIncludeDepthLimit(c.maxIncDepth)
subc.SetMaxErrors(math.MaxInt)
subc.SetDefaultFork(prog.evm.Name())
Expand Down
8 changes: 3 additions & 5 deletions asm/compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package asm
import (
"bytes"
"encoding/hex"
"io/fs"
"maps"
"os"
"path/filepath"
Expand Down Expand Up @@ -63,15 +62,14 @@ func TestCompiler(t *testing.T) {
for _, name := range names {
test := tests[name]
t.Run(name, func(t *testing.T) {
var fsys fs.FS
c := New(nil)
if len(test.Input.Files) > 0 {
fm := make(fstest.MapFS, len(test.Input.Files))
for name, content := range test.Input.Files {
fm[name] = &fstest.MapFile{Data: []byte(content)}
}
fsys = fm
c.SetFilesystem(fm)
}
c := NewCompiler(fsys)
output := c.CompileString(test.Input.Code)

if len(test.Output.Errors) > 0 {
Expand Down Expand Up @@ -162,7 +160,7 @@ func TestExamplePrograms(t *testing.T) {
}

func compileExample(t *testing.T, exampleDir string, file string) string {
c := NewCompiler(os.DirFS(exampleDir))
c := New(os.DirFS(exampleDir))
output := c.CompileFile(file)
for _, err := range c.ErrorsAndWarnings() {
t.Log(err)
Expand Down

0 comments on commit 5508a73

Please sign in to comment.