Skip to content

Commit

Permalink
wip(builder): merge std mod with entry mod, add lots of todos for mul…
Browse files Browse the repository at this point in the history
…timod flow
  • Loading branch information
emil14 committed Nov 15, 2023
1 parent 92eb542 commit dd3951a
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 208 deletions.
31 changes: 19 additions & 12 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,6 @@
"outFiles": ["${workspaceFolder}/web/out/**/*.js"],
"preLaunchTask": "${defaultBuildTask}"
},
// test extension
// {
// "name": "VSCode Extension Tests",
// "type": "extensionHost",
// "request": "launch",
// "args": [
// "--extensionDevelopmentPath=${workspaceFolder}",
// "--extensionTestsPath=${workspaceFolder}/out/test/suite/index"
// ],
// "outFiles": ["${workspaceFolder}/out/test/**/*.js"],
// "preLaunchTask": "${defaultBuildTask}"
// },
// run lsp in debugger
{
"name": "Language server (LSP) ",
Expand All @@ -36,6 +24,25 @@
"cwd": "${workspaceFolder}",
"args": ["-debug"]
},
// === Interpreter ===
{
"name": "Interpreter: do_nothing",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/interpreter",
"cwd": "${workspaceFolder}/examples",
"args": ["do_nothing"],
},
{
"name": "Interpreter: add_numbers",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/cmd/interpreter",
"cwd": "${workspaceFolder}/examples",
"args": ["add_numbers/with_error_handling"],
},
// === Other ===
{
"name": "antlr_000_empty.neva",
Expand Down
5 changes: 2 additions & 3 deletions cmd/interpreter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"os"
"path/filepath"

"github.com/nevalang/neva/internal/builder"
"github.com/nevalang/neva/internal/compiler"
Expand Down Expand Up @@ -68,13 +67,13 @@ func main() {
),
)

path, err := filepath.Abs(os.Args[1])
wd, err := os.Getwd()
if err != nil {
fmt.Println(err)
return
}

code, err := intr.Interpret(context.Background(), path)
code, err := intr.Interpret(context.Background(), wd, os.Args[1])
if err != nil {
fmt.Println(err)
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,22 @@

// Program that adds two numbers typed by user and prints the result.
// The algorithm is this: read first input, parse it,
// then read the second input and parse too. Add numbers and print the result.
// then read the second input and parse it too.
// Add numbers and print the result.

use {
std/tmp
std/core
}

components {
Main(enter) (exit) {
nodes {
readFirst tmp.Read
parseFirst tmp.ParseInt
readSecond tmp.Read
parseSecond tmp.ParseInt
readFirst core.Read
parseFirst core.ParseInt
readSecond core.Read
parseSecond core.ParseInt
add Add<int>
print tmp.Print<int>
print core.Print<int>
}
net {
in.enter -> readFirst.sig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
// Handle errors

use {
std/tmp
github/nevalang/neva/pkg/typesystem
some/really/deeply/nested/path/to/local/package/inthe/the/project
std/core
}

components {
Main(enter) (exit) {
nodes {
readFirst tmp.Read
readSecond tmp.Read
readFirst core.Read
readSecond core.Read

parseFirst tmp.ParseInt
parseSecond tmp.ParseInt
parseFirst core.ParseInt
parseSecond core.ParseInt

add Add<int>
print tmp.Print<any>
print core.Print<any>
}

net {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Our program turns out to be quite big! Let's split it into sub-components?

use {
std/tmp
std/core
}

components {
Expand All @@ -10,7 +10,7 @@ components {
readFirstInt ReadInt
readSecondInt ReadInt
add Add<int>
print tmp.Print<int | str>
print core.Print<int | str>
}
net {
in.enter -> readFirstInt.sig
Expand All @@ -28,8 +28,8 @@ components {

ReadInt(sig) (v int, err str) {
nodes {
read tmp.Read
parse tmp.ParseInt
read core.Read
parse core.ParseInt
}
net {
in.sig -> read.sig
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions examples/001_echo.neva → examples/echo/main.neva
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
// And it does so in an infinite loop.

use {
std/tmp
std/core
}

components {
Main(enter) (exit) {
nodes {
read tmp.Read
print tmp.Print<str>
read core.Read
print core.Print<str>
}
net {
in.enter -> read.sig
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use {
std/tmp
std/core
}

const {
Expand All @@ -9,11 +9,12 @@ const {
components {
Main(enter) (exit) {
nodes {
print tmp.Print<str>
print core.Print<str>
}
net {
enter -> out.exit
msg -> out.exit
in.enter -> out.exit
msg -> print.v
print.v -> out.exit
}
}
}
1 change: 1 addition & 0 deletions examples/neva.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
compiler: 0.0.1
55 changes: 35 additions & 20 deletions internal/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

"github.com/nevalang/neva/internal/compiler"
"github.com/nevalang/neva/internal/compiler/parser"
"github.com/nevalang/neva/internal/compiler/sourcecode"
)

type Builder struct {
Expand All @@ -21,20 +22,27 @@ type Builder struct {
}

func (b Builder) Build(ctx context.Context, workdir string) (compiler.Build, error) {
// build user module
mods := map[string]compiler.RawModule{}

// this supposed to work recursively but currently only root module is processed correctly
entryMod, err := b.buildModule(ctx, workdir, mods)
if err != nil {
return compiler.Build{}, fmt.Errorf("build entry mod: %w", err)
}
mods["entry"] = entryMod

// build stdlib module
// TODO in the future we not going to merge std module into root one
// because that won't work with normal multi-module flow
// but currently we do this because it works for single-mod flow (which we only support for the moment)
stdMod, err := b.buildModule(ctx, b.stdlibLocation, mods)
if err != nil {
return compiler.Build{}, fmt.Errorf("build entry mod: %w", err)
}
mods["std"] = stdMod

// merge std module with the entry module (TODO remove this)
for name, stdPkg := range stdMod.Packages {
mods["entry"].Packages["std/"+name] = stdPkg
}

return compiler.Build{
EntryModule: "entry",
Expand All @@ -57,20 +65,11 @@ func (b Builder) buildModule(
return compiler.RawModule{}, fmt.Errorf("parse manifest: %w", err)
}

// process module deps (download if needed and build)
// FIXME this isn't tested at all (multi-module flow isn't implemented yet)
for name, dep := range manifest.Deps {
depPath := fmt.Sprintf("%s/%s_%s", b.thirdPartyLocation, dep.Addr, dep.Version)
if _, err := os.Stat(depPath); err != nil { // check if directory with this dependency exists
if os.IsNotExist(err) { // if not, clone the repo
if _, err := git.PlainClone(depPath, false, &git.CloneOptions{
URL: fmt.Sprintf("https://%s", dep.Addr),
ReferenceName: plumbing.NewTagReferenceName(dep.Version),
}); err != nil {
return compiler.RawModule{}, err
}
} else { // if it's an unknown error then return
return compiler.RawModule{}, fmt.Errorf("os stat: %w", err)
}
depPath, err := b.downloadDep(dep)
if err != nil {
return compiler.RawModule{}, fmt.Errorf("%w", err)
}

rawMod, err := b.buildModule(ctx, depPath, mods)
Expand All @@ -93,6 +92,23 @@ func (b Builder) buildModule(
}, nil
}

func (b Builder) downloadDep(dep sourcecode.Dependency) (string, error) {
depPath := fmt.Sprintf("%s/%s_%s", b.thirdPartyLocation, dep.Addr, dep.Version)
if _, err := os.Stat(depPath); err != nil {
if os.IsNotExist(err) {
if _, err := git.PlainClone(depPath, false, &git.CloneOptions{
URL: fmt.Sprintf("https://%s", dep.Addr),
ReferenceName: plumbing.NewTagReferenceName(dep.Version),
}); err != nil {
return "", err
}
} else {
return "", fmt.Errorf("os stat: %w", err)
}
}
return depPath, nil
}

func readManifestYaml(workdir string) ([]byte, error) {
rawManifest, err := os.ReadFile(workdir + "/neva.yml")
if err == nil {
Expand All @@ -107,7 +123,6 @@ func readManifestYaml(workdir string) ([]byte, error) {
return rawManifest, nil
}

// walk recursively traverses the directory assuming that is a neva module (set of packages with source code files).
func walk(rootPath string, prog map[string]compiler.RawPackage) error {
if err := filepath.Walk(rootPath, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
Expand Down Expand Up @@ -149,10 +164,10 @@ func getPkgName(rootPath, filePath string) string {
return strings.TrimPrefix(dirPath, rootPath+"/")
}

func MustNew(stdPkgPath, thirdPartyLocation string, parser parser.Parser) Builder {
func MustNew(stdlibPath, thirdpartyPath string, parser parser.Parser) Builder {
return Builder{
stdlibLocation: stdPkgPath,
thirdPartyLocation: thirdPartyLocation,
stdlibLocation: stdlibPath,
thirdPartyLocation: thirdpartyPath,
parser: parser,
}
}
Loading

0 comments on commit dd3951a

Please sign in to comment.