Skip to content

Commit

Permalink
wip: repo disk
Browse files Browse the repository at this point in the history
  • Loading branch information
emil14 committed Oct 3, 2023
1 parent e833ddb commit ea934c5
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion examples/001_echo.neva
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// This program reads a string and prints it.
// This program reads a str and prints it.
// And it does so in an infinite loop.

use {
Expand Down
2 changes: 1 addition & 1 deletion examples/002_hello_world_1.neva
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
}

const {
msg string 'Hello, World!\n'
msg str 'Hello, World!\n'
}

components {
Expand Down
2 changes: 1 addition & 1 deletion examples/003_hello_world_2.neva
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ components {
}
net {
in.enter -> lock.in.sig
(string 'Hello, World!\n') -> lock.in.v
(str 'Hello, World!\n') -> lock.in.v
lock.out.v -> print.in.v
print.out.v -> out.exit
}
Expand Down
2 changes: 1 addition & 1 deletion examples/004_hello_world_3.neva
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
}

components {
Main(enter string 'Hello, World!\n') (exit) {
Main(enter str 'Hello, World!\n') (exit) {
nodes {
print std.Print<str>
}
Expand Down
8 changes: 4 additions & 4 deletions examples/008_calculator.neva
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ use {
}

const {
instructions string 'Enter operation:\n\t1) +/n/t2) -/n/t3) */n/t4) /'
instructions str 'Enter operation:\n\t1) +/n/t2) -/n/t3) */n/t4) /'
availableInstructions map<str> ['*', '/', '+', '-']
enterNumMsg string 'Enter number'
unknownOpErr string 'Unknown operation'
enterNumMsg str 'Enter number'
unknownOpErr str 'Unknown operation'
}

components {
Main(enter string instructions) (exit) {
Main(enter str instructions) (exit) {
nodes {
print std.Print
readOp ReadOp
Expand Down
41 changes: 24 additions & 17 deletions internal/compiler/repo/disk/disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"os"
"path/filepath"
"strings"

"github.com/nevalang/neva/internal/compiler"
"github.com/nevalang/neva/pkg/ir"
Expand All @@ -15,19 +16,19 @@ type Repository struct {
}

func (r Repository) ByPath(ctx context.Context, pathToMainPkg string) (map[string]compiler.RawPackage, error) {
mainPkgFiles, err := readAllFilesInDir(pathToMainPkg)
if err != nil {
mainPkgFiles := map[string][]byte{}
if err := readAllFilesInDir(pathToMainPkg, mainPkgFiles); err != nil {
return nil, err
}

stdPkg, err := readAllFilesInDir(r.stdPath)
if err != nil {
stdFiles := map[string][]byte{}
if err := readAllFilesInDir(r.stdPath, stdFiles); err != nil {
return nil, err
}

return map[string]compiler.RawPackage{
"main": mainPkgFiles,
"std": stdPkg,
"std": stdFiles,
}, nil
}

Expand All @@ -39,26 +40,32 @@ func (r Repository) Save(ctx context.Context, path string, prog *ir.Program) err
return os.WriteFile(path, bb, 0644) //nolint:gofumpt
}

func readAllFilesInDir(path string) (map[string][]byte, error) {
files := make(map[string][]byte)

func readAllFilesInDir(path string, files map[string][]byte) error {
if err := filepath.Walk(path, func(filePath string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if !info.IsDir() {
fileBytes, err := os.ReadFile(filePath)
if err != nil {
return err
}
files[filePath] = fileBytes
if info.IsDir() {
// err := readAllFilesInDir(filePath, files)
// if err != nil {
// return err
// }
return nil
}
ext := filepath.Ext(info.Name())
if ext != ".neva" {
return nil
}
bb, err := os.ReadFile(filePath)
if err != nil {
return err
}
files[strings.TrimSuffix(info.Name(), ext)] = bb
return nil
}); err != nil {
return nil, err
return err
}

return files, nil
return nil
}

func MustNew(stdPkgPath string) Repository {
Expand Down

0 comments on commit ea934c5

Please sign in to comment.