Skip to content

Commit

Permalink
embed stdmod in binary
Browse files Browse the repository at this point in the history
  • Loading branch information
hidetatz committed Jul 31, 2023
1 parent 1b4dadf commit c5791b3
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 33 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ gotest: clean $(BIN)
.PHONY: sbtest
sbtest: clean $(BIN)
ls -1 tests/*.sb | xargs -L 1 ./shiba
ls -1 tests/**/*.sb | xargs -L 1 ./shiba

.PHONY: clean
clean:
Expand Down
24 changes: 24 additions & 0 deletions mod.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"container/list"
"embed"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -62,6 +63,9 @@ type module struct {
funcscopes *list.List
}

//go:embed std
var stdmodfs embed.FS

func newmodule(modname string) (*module, error) {
dir, mod := filepath.Split(modname)

Expand All @@ -84,6 +88,26 @@ func newmodule(modname string) (*module, error) {
}, nil
}

func newstdmodule(mod string) (*module, error) {
file := modtofile(mod)

bs, err := stdmodfs.ReadFile(filepath.Join("std/", file))
if err != nil {
return nil, err
}

content := []rune(string(bs))

return &module{
name: mod,
filename: file,
directory: "std",
content: content,
globscope: newscope(),
funcscopes: list.New(),
}, nil
}

func (m *module) createfuncscope() {
m.funcscopes.PushBack(newscope())
}
Expand Down
16 changes: 7 additions & 9 deletions process.go
Original file line number Diff line number Diff line change
Expand Up @@ -633,16 +633,14 @@ func procFuncall(mod *module, n *ndFuncall) (procResult, shibaErr) {
}

func procImport(mod *module, n *ndImport) (procResult, shibaErr) {
modname := ""
if isstdmod(n.target) {
modname = filepath.Join(stdmoddir(), n.target)
} else {
modname = filepath.Join(mod.directory, n.target)
}

m, err := newmodule(modname)
// first, try to import user-defined module
m, err := newmodule(filepath.Join(mod.directory, n.target))
if err != nil {
return nil, &errSimple{msg: fmt.Sprintf("cannot import %s: %s", n.target, err), l: n.token().loc}
// if err, try to import std module
m, err = newstdmodule(n.target)
if err != nil {
return nil, &errSimple{msg: fmt.Sprintf("cannot import %s: %s", n.target, err), l: n.token().loc}
}
}

if err := runmod(m); err != nil {
Expand Down
24 changes: 0 additions & 24 deletions stdmod.go

This file was deleted.

11 changes: 11 additions & 0 deletions tests/import_order/import.sb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import ../assert

as = assert.assert

# os is defined in stdmod, but
# the ./os.sb is read
import os

as(1, os.v)

print("import_order test succeeded")
1 change: 1 addition & 0 deletions tests/import_order/os.sb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v = 1

0 comments on commit c5791b3

Please sign in to comment.