Skip to content

Commit

Permalink
Source comment output, error lengths, dependency bump
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Feb 23, 2021
1 parent 29eb63e commit 0624ef3
Show file tree
Hide file tree
Showing 14 changed files with 235 additions and 119 deletions.
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func init() {

rootCmd.PersistentFlags().Bool("numbers", false, "Output line numbers")
rootCmd.PersistentFlags().Bool("comments", false, "Output comments")
rootCmd.PersistentFlags().Int("comment-offset", 50, "Comment offset from line start")
rootCmd.PersistentFlags().Int("comment-offset", 60, "Comment offset from line start")
rootCmd.PersistentFlags().String("stacked", "", "Use a provided memory cell/bank as a stack")
rootCmd.PersistentFlags().Bool("source", false, "Output source code after comment")

Expand Down
8 changes: 4 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ module github.com/Vilsol/go-mlog
go 1.15

require (
github.com/davecgh/go-spew v1.1.1
github.com/sirupsen/logrus v1.7.0
github.com/spf13/cobra v1.1.1
github.com/olekukonko/tablewriter v0.0.5
github.com/sirupsen/logrus v1.8.0
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.3.0
github.com/stretchr/testify v1.7.0
)
21 changes: 21 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,16 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/magefile/mage v1.10.0 h1:3HiXzCUY12kh9bIuyXShaVe529fJfyqoVM42o/uom2g=
github.com/magefile/mage v1.10.0/go.mod h1:z5UZb/iS3GoOSn0JgWuiw7dxlurVYTu+/jHXqQg881A=
github.com/magiconair/properties v1.8.1 h1:ZC2Vc7/ZFkGmsVC9KvOjumD+G5lXy2RtTKyzRKO2BQ4=
github.com/magiconair/properties v1.8.1/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.0.9/go.mod h1:9vuHe8Xs5qXnSaW/c/ABM9alt+Vo+STaOChaDxuIBZU=
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-runewidth v0.0.7 h1:Ei8KR0497xHyKJPAv59M1dkC+rOZCMBJ+t3fZ+twI54=
github.com/mattn/go-runewidth v0.0.7/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/mattn/go-runewidth v0.0.9 h1:Lm995f3rfxdpd6TSmuVCHVb/QhupuXlYr8sCI/QdE+0=
github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
Expand All @@ -129,6 +135,10 @@ github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJ
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/mwitkow/go-conntrack v0.0.0-20161129095857-cc309e4a2223/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/oklog/ulid v1.3.1/go.mod h1:CirwcVhetQ6Lv90oh/F+FBtV6XMibvdAFo93nm5qn4U=
github.com/olekukonko/tablewriter v0.0.4 h1:vHD/YYe1Wolo78koG299f7V/VAS08c6IpCLn+Ejf/w8=
github.com/olekukonko/tablewriter v0.0.4/go.mod h1:zq6QwlOf5SlnkVbMSr5EoBv3636FWnp+qbPhuoO21uA=
github.com/olekukonko/tablewriter v0.0.5 h1:P2Ga83D34wi1o9J6Wh1mRuqd4mF/x/lgBS7N7AbDhec=
github.com/olekukonko/tablewriter v0.0.5/go.mod h1:hPp6KlRPjbx+hW8ykQs1w3UBbZlj6HuIJcUGPhkA7kY=
github.com/pascaldekloe/goe v0.0.0-20180627143212-57f6aae5913c/go.mod h1:lzWF7FIEvWOWxwDKqyGYQf6ZUaNfKdP144TG7ZOy1lc=
github.com/pelletier/go-toml v1.2.0 h1:T5zMGML61Wp+FlcbWjRDT7yAxhJNAiPPLOFECq181zc=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand All @@ -155,6 +165,8 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeV
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.8.0 h1:nfhvjKcUMhBMVqbKHJlk5RPrrfYr/NMo3692g0dwfWU=
github.com/sirupsen/logrus v1.8.0/go.mod h1:4GuYW9TZmE769R5STWrRakJc4UqQ3+QQ95fyz7ENv1A=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
Expand All @@ -167,6 +179,8 @@ github.com/spf13/cast v1.3.0 h1:oget//CVOEoFewqQxwr0Ej5yjygnqGkvggSE/gB35Q8=
github.com/spf13/cast v1.3.0/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
github.com/spf13/cobra v1.1.1 h1:KfztREH0tPxJJ+geloSLaAkaPkr4ki2Er5quFV1TDo4=
github.com/spf13/cobra v1.1.1/go.mod h1:WnodtKOvamDL/PwE2M4iKs8aMDBZ5Q5klgD3qfVJQMI=
github.com/spf13/cobra v1.1.3 h1:xghbfqPkxzxP3C/f3n5DdpAbdKLj4ZE4BWQI362l53M=
github.com/spf13/cobra v1.1.3/go.mod h1:pGADOWyqRD/YMrPZigI/zbliZ2wVD/23d+is3pSWzOo=
github.com/spf13/jwalterweatherman v1.0.0 h1:XHEdyB+EcvlqZamSM4ZOMGlc93t6AcsBEu9Gc1vn7yk=
github.com/spf13/jwalterweatherman v1.0.0/go.mod h1:cQK4TGJAtQXfYWX+Ddv3mKDzgVb68N+wFjFa4jdeBTo=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
Expand All @@ -176,10 +190,13 @@ github.com/spf13/viper v1.7.0/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5q
github.com/spf13/viper v1.7.1 h1:pM5oEahlgWv/WnHXpgbKz7iLIxRf65tye2Ci+XFK5sk=
github.com/spf13/viper v1.7.1/go.mod h1:8WkrPz2fc9jxqZNCJI/76HCieCp4Q8HaLFoCha5qpdg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.1.1 h1:2vfRuCMp5sSVIDSqO8oNnWJq7mPa6KVP3iPIwFBuy8A=
github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U=
Expand Down Expand Up @@ -308,6 +325,10 @@ gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
Expand Down
4 changes: 2 additions & 2 deletions tests/errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func sample1(arg hello.world) int {
{
name: "CallToUnknownFunction",
input: TestMain(`foo()`),
output: `error at 103: unknown function: foo`,
output: `error at 103-108: unknown function: foo`,
},
{
name: "InvalidConstant",
Expand Down Expand Up @@ -144,7 +144,7 @@ func sample() (int, int) {
{
name: "ErrorIncorrectVarCount",
input: TestMain(`a, b := m.Read("bank1", 0)`),
output: `error at 111: function requires 1 variables, provided: 2`,
output: `error at 111-129: function requires 1 variables, provided: 2`,
},
{
name: "ErrorMismatchedSides",
Expand Down
141 changes: 85 additions & 56 deletions tests/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,22 +32,22 @@ func foo(x int) int {
options: transpiler.Options{
Numbers: true,
},
output: ` 0: jump 5 always
1: set _foo_x @funcArg_foo_0
2: op add _foo_0 _foo_x 20
3: set @return _foo_0
4: set @counter @funcTramp_foo
5: set _main_i 0
6: jump 8 lessThan _main_i 10
7: jump 16 always
8: set @funcArg_foo_0 _main_i
9: set @funcTramp_foo 11
10: jump 1 always
11: set _main_0 @return
12: print _main_0
13: print "\n"
14: op add _main_i _main_i 1
15: jump 8 lessThan _main_i 10`,
output: `jump 5 always # 0
set _foo_x @funcArg_foo_0 # 1
op add _foo_0 _foo_x 20 # 2
set @return _foo_0 # 3
set @counter @funcTramp_foo # 4
set _main_i 0 # 5
jump 8 lessThan _main_i 10 # 6
jump 16 always # 7
set @funcArg_foo_0 _main_i # 8
set @funcTramp_foo 11 # 9
jump 1 always # 10
set _main_0 @return # 11
print _main_0 # 12
print "\n" # 13
op add _main_i _main_i 1 # 14
jump 8 lessThan _main_i 10 # 15 `,
},
{
name: "Comments",
Expand All @@ -56,55 +56,84 @@ func foo(x int) int {
Comments: true,
CommentOffset: 45,
},
output: `jump 5 always // Jump to start of main
// Function: foo //
set _foo_x @funcArg_foo_0 // Read parameter into variable
op add _foo_0 _foo_x 20 // Execute operation
set @return _foo_0 // Set return data
set @counter @funcTramp_foo // Trampoline back
// Function: main //
set _main_i 0 // Set the variable to the value
jump 8 lessThan _main_i 10 // Jump into the loop
jump 16 always // Jump to end of loop
set @funcArg_foo_0 _main_i // Set foo argument: 0
set @funcTramp_foo 11 // Set Trampoline Address
jump 1 always // Jump to function: foo
set _main_0 @return // Set variable to returned value
print _main_0 // Call to native function
print "\n" // Call to native function
op add _main_i _main_i 1 // Execute increment/decrement
jump 8 lessThan _main_i 10 // Jump to start of loop`,
output: `jump 5 always # Jump to start of main
#
# Function: foo #
#
set _foo_x @funcArg_foo_0 # Read parameter into variable
op add _foo_0 _foo_x 20 # Execute operation
set @return _foo_0 # Set return data
set @counter @funcTramp_foo # Trampoline back
#
# Function: main #
#
set _main_i 0 # Assign value to variable
jump 8 lessThan _main_i 10 # Jump into the loop
jump 16 always # Jump to end of loop
set @funcArg_foo_0 _main_i # Set foo argument: 0
set @funcTramp_foo 11 # Set Trampoline Address
jump 1 always # Jump to function: foo
set _main_0 @return # Set variable to returned value
print _main_0 # Call to native function
print "\n" # Call to native function
op add _main_i _main_i 1 # Execute increment/decrement
jump 8 lessThan _main_i 10 # Jump to start of loop `,
},
{
name: "Comments",
input: testInput,
options: transpiler.Options{
Source: true,
CommentOffset: 45,
},
output: `jump 5 always
set _foo_x @funcArg_foo_0
op add _foo_0 _foo_x 20 # x + 20
set @return _foo_0 # return x + 20
set @counter @funcTramp_foo
set _main_i 0 # i := 0
jump 8 lessThan _main_i 10
jump 16 always
set @funcArg_foo_0 _main_i
set @funcTramp_foo 11
jump 1 always # foo(i)
set _main_0 @return
print _main_0 # println(foo(i))
print "\n" # println(foo(i))
op add _main_i _main_i 1 # i++
jump 8 lessThan _main_i 10 `,
},
{
name: "All",
input: testInput,
options: transpiler.Options{
Numbers: true,
Comments: true,
Source: true,
CommentOffset: 45,
},
output: ` 0: jump 5 always // Jump to start of main
// Function: foo //
1: set _foo_x @funcArg_foo_0 // Read parameter into variable
2: op add _foo_0 _foo_x 20 // Execute operation
3: set @return _foo_0 // Set return data
4: set @counter @funcTramp_foo // Trampoline back
// Function: main //
5: set _main_i 0 // Set the variable to the value
6: jump 8 lessThan _main_i 10 // Jump into the loop
7: jump 16 always // Jump to end of loop
8: set @funcArg_foo_0 _main_i // Set foo argument: 0
9: set @funcTramp_foo 11 // Set Trampoline Address
10: jump 1 always // Jump to function: foo
11: set _main_0 @return // Set variable to returned value
12: print _main_0 // Call to native function
13: print "\n" // Call to native function
14: op add _main_i _main_i 1 // Execute increment/decrement
15: jump 8 lessThan _main_i 10 // Jump to start of loop`,
output: `jump 5 always # 0 # Jump to start of main
#
# Function: foo #
#
set _foo_x @funcArg_foo_0 # 1 # Read parameter into variable
op add _foo_0 _foo_x 20 # 2 # Execute operation # x + 20
set @return _foo_0 # 3 # Set return data # return x + 20
set @counter @funcTramp_foo # 4 # Trampoline back
#
# Function: main #
#
set _main_i 0 # 5 # Assign value to variable # i := 0
jump 8 lessThan _main_i 10 # 6 # Jump into the loop
jump 16 always # 7 # Jump to end of loop
set @funcArg_foo_0 _main_i # 8 # Set foo argument: 0
set @funcTramp_foo 11 # 9 # Set Trampoline Address
jump 1 always # 10 # Jump to function: foo # foo(i)
set _main_0 @return # 11 # Set variable to returned value
print _main_0 # 12 # Call to native function # println(foo(i))
print "\n" # 13 # Call to native function # println(foo(i))
op add _main_i _main_i 1 # 14 # Execute increment/decrement # i++
jump 8 lessThan _main_i 10 # 15 # Jump to start of loop `,
},
}
for _, test := range tests {
Expand Down
7 changes: 3 additions & 4 deletions transpiler/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import (
"errors"
"fmt"
"go/ast"
"go/token"
)

type ContextualError struct {
error
Context context.Context
Pos *token.Pos
Pos *ast.Node
}

func (e ContextualError) Error() string {
if e.Pos != nil {
return fmt.Sprintf("error at %d: %s", *e.Pos, e.error.Error())
return fmt.Sprintf("error at %d-%d: %s", (*e.Pos).Pos(), (*e.Pos).End(), e.error.Error())
}

if e.Context != nil {
Expand All @@ -40,7 +39,7 @@ func Err(ctx context.Context, err string) ContextualError {
}
}

func ErrPos(ctx context.Context, pos token.Pos, err string) ContextualError {
func ErrPos(ctx context.Context, pos ast.Node, err string) ContextualError {
return ContextualError{
error: errors.New(err),
Context: ctx,
Expand Down
Loading

0 comments on commit 0624ef3

Please sign in to comment.