Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions numscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,23 @@ type ParseResult struct {
parseResult parser.ParseResult
}

// ---- TODO useful for the playground
// func (*ParseResult) GetNeededVariables() map[string]ValueType {}
// Returns a map from a variable's name to its type.
//
// doesn't include variables whose value is already defined within the script
func (p ParseResult) GetNeededVariables() map[string]string {
m := make(map[string]string)

for _, varDecl := range p.parseResult.Value.Vars {
if varDecl.Name == nil || varDecl.Origin != nil {
continue
}

m[varDecl.Name.Name] = varDecl.Type.Name
}

return m
}

// func (*ParseResult) GetDiagnostics() []Diagnostic {}

type ParserError = parser.ParserError
Expand Down
24 changes: 24 additions & 0 deletions numscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@ import (
"github.com/stretchr/testify/require"
)

func TestGetVars(t *testing.T) {
parseResult := numscript.Parse(`
vars {
monetary $mon
account $acc
account $acc2

monetary $do_not_include_in_output = balance(@acc, USD/2)
}
`)

require.Empty(t, parseResult.GetParsingErrors(), "There should not be parsing errors")

require.Equal(t,
map[string]string{
"mon": "monetary",
"acc": "account",
"acc2": "account",
},
parseResult.GetNeededVariables(),
)

}

func TestDoNotGetWorldBalance(t *testing.T) {
parseResult := numscript.Parse(`send [COIN 100] (
source = @world
Expand Down