Skip to content

Commit 42f1133

Browse files
authored
feat: adding public method (#46)
1 parent 12977d3 commit 42f1133

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

numscript.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,23 @@ type ParseResult struct {
1212
parseResult parser.ParseResult
1313
}
1414

15-
// ---- TODO useful for the playground
16-
// func (*ParseResult) GetNeededVariables() map[string]ValueType {}
15+
// Returns a map from a variable's name to its type.
16+
//
17+
// doesn't include variables whose value is already defined within the script
18+
func (p ParseResult) GetNeededVariables() map[string]string {
19+
m := make(map[string]string)
20+
21+
for _, varDecl := range p.parseResult.Value.Vars {
22+
if varDecl.Name == nil || varDecl.Origin != nil {
23+
continue
24+
}
25+
26+
m[varDecl.Name.Name] = varDecl.Type.Name
27+
}
28+
29+
return m
30+
}
31+
1732
// func (*ParseResult) GetDiagnostics() []Diagnostic {}
1833

1934
type ParserError = parser.ParserError

numscript_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@ import (
1111
"github.com/stretchr/testify/require"
1212
)
1313

14+
func TestGetVars(t *testing.T) {
15+
parseResult := numscript.Parse(`
16+
vars {
17+
monetary $mon
18+
account $acc
19+
account $acc2
20+
21+
monetary $do_not_include_in_output = balance(@acc, USD/2)
22+
}
23+
`)
24+
25+
require.Empty(t, parseResult.GetParsingErrors(), "There should not be parsing errors")
26+
27+
require.Equal(t,
28+
map[string]string{
29+
"mon": "monetary",
30+
"acc": "account",
31+
"acc2": "account",
32+
},
33+
parseResult.GetNeededVariables(),
34+
)
35+
36+
}
37+
1438
func TestDoNotGetWorldBalance(t *testing.T) {
1539
parseResult := numscript.Parse(`send [COIN 100] (
1640
source = @world

0 commit comments

Comments
 (0)