Skip to content

Commit

Permalink
Allow string function parameters, add constant function
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Jan 7, 2021
1 parent 984ceb0 commit 33876ef
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 23 deletions.
7 changes: 5 additions & 2 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ func init() {

rootCmd.PersistentFlags().Bool("numbers", false, "Output line numbers")
rootCmd.PersistentFlags().Bool("comments", false, "Output comments")
rootCmd.PersistentFlags().Bool("debug", false, "Write to debug memory cell")
rootCmd.PersistentFlags().Int("comment-offset", 50, "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")

rootCmd.PersistentFlags().String("output", "", "Output file. Outputs to stdout if unspecified")

Expand All @@ -64,7 +65,9 @@ func init() {

_ = viper.BindPFlag("numbers", rootCmd.PersistentFlags().Lookup("numbers"))
_ = viper.BindPFlag("comments", rootCmd.PersistentFlags().Lookup("comments"))
_ = viper.BindPFlag("debug", rootCmd.PersistentFlags().Lookup("debug"))
_ = viper.BindPFlag("comment-offset", rootCmd.PersistentFlags().Lookup("comment-offset"))
_ = viper.BindPFlag("stacked", rootCmd.PersistentFlags().Lookup("stacked"))
_ = viper.BindPFlag("source", rootCmd.PersistentFlags().Lookup("source"))

_ = viper.BindPFlag("output", rootCmd.PersistentFlags().Lookup("output"))
}
8 changes: 5 additions & 3 deletions cmd/transpile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ var transpileCmd = &cobra.Command{
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
result, err := transpiler.GolangToMLOGFile(args[0], transpiler.Options{
Numbers: viper.GetBool("numbers"),
Comments: viper.GetBool("comments"),
Stacked: viper.GetString("stacked"),
Numbers: viper.GetBool("numbers"),
Comments: viper.GetBool("comments"),
CommentOffset: viper.GetInt("comment-offset"),
Stacked: viper.GetString("stacked"),
Source: viper.GetBool("source"),
})
if err != nil {
return err
Expand Down
36 changes: 36 additions & 0 deletions m/custom.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package m

import (
"github.com/Vilsol/go-mlog/transpiler"
"strings"
)

func init() {
// TODO Optimize
transpiler.RegisterFuncTranslation("m.Const", transpiler.Translator{
Count: func(args []transpiler.Resolvable, vars []transpiler.Resolvable) int {
return 1
},
Variables: 1,
Translate: func(args []transpiler.Resolvable, vars []transpiler.Resolvable) ([]transpiler.MLOGStatement, error) {
return []transpiler.MLOGStatement{
&transpiler.MLOG{
Statement: [][]transpiler.Resolvable{
{
&transpiler.Value{Value: "set"},
vars[0],
&transpiler.Value{Value: strings.Trim(args[0].GetValue(), "\"")},
},
},
},
}, nil
},
})
}

// Process provided string as a game constant
//
// Example m.Const("@copper")
func Const(constant string) string {
return ""
}
12 changes: 9 additions & 3 deletions transpiler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,17 @@ func GolangToMLOG(input string, options Options) (string, error) {
prevArgs := 0
for i, param := range castDecl.Type.Params.List {
if paramTypeIdent, ok := param.Type.(*ast.Ident); ok {
if paramTypeIdent.Name != "int" && paramTypeIdent.Name != "float64" {
return "", Err(fnCtx, "function parameters may only be integers or floating point numbers")
if options.Stacked != "" {
if paramTypeIdent.Name != "int" && paramTypeIdent.Name != "float64" {
return "", Err(fnCtx, "function parameters may only be integers or floating point numbers in stack mode")
}
} else {
if paramTypeIdent.Name != "int" && paramTypeIdent.Name != "float64" && paramTypeIdent.Name != "string" {
return "", Err(fnCtx, "function parameters may only be integers, floating point numbers or strings")
}
}
} else {
return "", Err(fnCtx, "function parameters may only be integers or floating point numbers")
return "", Err(fnCtx, "function parameters may only be basic types")
}

position := len(castDecl.Type.Params.List) - i
Expand Down
10 changes: 6 additions & 4 deletions transpiler/options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package transpiler

type Options struct {
Numbers bool
Comments bool
NoStartup bool
Stacked string
Numbers bool
Comments bool
CommentOffset int
NoStartup bool
Stacked string
Source bool
}
3 changes: 3 additions & 0 deletions transpiler/type_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package transpiler
import (
"context"
"go/ast"
"go/token"
)

type Global struct {
Expand All @@ -22,6 +23,8 @@ type Function struct {
type MLOGAble interface {
ToMLOG() [][]Resolvable
GetComment(int) string
SetSourcePos(token.Pos)
GetSourcePos() token.Pos
}

type Processable interface {
Expand Down
10 changes: 10 additions & 0 deletions transpiler/type_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package transpiler
import (
"context"
"go/ast"
"go/token"
"strconv"
)

Expand All @@ -13,6 +14,7 @@ type MLOGCustomFunction struct {
Unresolved []MLOGStatement
FunctionName string
Comments map[int]string
SourcePos token.Pos
}

func (m *MLOGCustomFunction) ToMLOG() [][]Resolvable {
Expand Down Expand Up @@ -160,3 +162,11 @@ func (m *MLOGCustomFunction) PostProcess(ctx context.Context, global *Global, fu
func (m *MLOGCustomFunction) GetComment(pos int) string {
return m.Comments[pos]
}

func (m *MLOGCustomFunction) SetSourcePos(pos token.Pos) {
m.SourcePos = pos
}

func (m *MLOGCustomFunction) GetSourcePos() token.Pos {
return m.SourcePos
}
19 changes: 19 additions & 0 deletions transpiler/type_mlog.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ package transpiler
import (
"context"
"fmt"
"go/token"
"strconv"
)

type MLOG struct {
Statement [][]Resolvable
Position int
Comment string
SourcePos token.Pos
}

func (m *MLOG) ToMLOG() [][]Resolvable {
Expand Down Expand Up @@ -59,12 +61,21 @@ func (m *MLOG) GetComment(int) string {
return m.Comment
}

func (m *MLOG) SetSourcePos(pos token.Pos) {
m.SourcePos = pos
}

func (m *MLOG) GetSourcePos() token.Pos {
return m.SourcePos
}

type MLOGFunc struct {
Position int
Function Translator
Arguments []Resolvable
Variables []Resolvable
Unresolved []MLOGStatement
SourcePos token.Pos
}

func (m *MLOGFunc) ToMLOG() [][]Resolvable {
Expand Down Expand Up @@ -133,6 +144,14 @@ func (m *MLOGFunc) GetComment(int) string {
return "Call to native function"
}

func (m *MLOGFunc) SetSourcePos(pos token.Pos) {
m.SourcePos = pos
}

func (m *MLOGFunc) GetSourcePos() token.Pos {
return m.SourcePos
}

type MLOGTrampoline struct {
MLOG
Variable string
Expand Down
25 changes: 15 additions & 10 deletions transpiler/type_native.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package transpiler

import "strconv"
import (
"context"
"strconv"
)

type MLOGStackWriter struct {
MLOG
Expand Down Expand Up @@ -30,25 +33,27 @@ type MLOGTrampolineBack struct {
Function string
}

func (m *MLOGTrampolineBack) ToMLOG() [][]Resolvable {
func (m *MLOGTrampolineBack) PreProcess(ctx context.Context, global *Global, function *Function) error {
if m.Stacked != "" {
return [][]Resolvable{
m.Statement = [][]Resolvable{
{
&Value{Value: "read"},
&Value{Value: "@counter"},
&Value{Value: m.Stacked},
&Value{Value: stackVariable},
},
}
} else {
m.Statement = [][]Resolvable{
{
&Value{Value: "set"},
&Value{Value: "@counter"},
&Value{Value: FunctionTrampolinePrefix + m.Function},
},
}
}

return [][]Resolvable{
{
&Value{Value: "set"},
&Value{Value: "@counter"},
&Value{Value: FunctionTrampolinePrefix + m.Function},
},
}
return m.MLOG.PreProcess(ctx, global, function)
}

func (m *MLOGTrampolineBack) GetComment(int) string {
Expand Down
3 changes: 2 additions & 1 deletion transpiler/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package transpiler
import (
"context"
"fmt"
"strconv"
)

func MLOGToString(ctx context.Context, statements [][]Resolvable, statement MLOGAble, lineNumber int) string {
Expand All @@ -21,7 +22,7 @@ func MLOGToString(ctx context.Context, statements [][]Resolvable, statement MLOG
}

if ctx.Value(contextOptions).(Options).Comments {
result += fmt.Sprintf("%-45s", resultLine)
result += fmt.Sprintf("%-"+strconv.Itoa(ctx.Value(contextOptions).(Options).CommentOffset)+"s", resultLine)
result += " // " + statement.GetComment(lineNumber)
} else {
result += resultLine
Expand Down

0 comments on commit 33876ef

Please sign in to comment.