Skip to content

Commit

Permalink
Add a stackless mode
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Jan 7, 2021
1 parent a91d432 commit 984ceb0
Show file tree
Hide file tree
Showing 31 changed files with 629 additions and 325 deletions.
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ Go to MLOG transpiler.

A Web IDE is available [here](https://vilsol.github.io/go-mlog-web/?1)

## Examples

There are several example programs available on the wiki. // TODO

## Supports

* Functions
Expand All @@ -27,13 +31,18 @@ A Web IDE is available [here](https://vilsol.github.io/go-mlog-web/?1)
* Contextual errors
* Tree-shaking unused functions
* Multi-pass pre/post-processing
* Stackless functions

## Roadmap

* Full variable block scoping
* Variable argument count functions
* Multiple function return values
* Optimize simple jump instructions

## Planned Optimizations

* Simple jump instructions
* Switch case jumps at the start of the block

## Design Limitations

Expand All @@ -46,7 +55,7 @@ A Web IDE is available [here](https://vilsol.github.io/go-mlog-web/?1)
* Transpilation optimizations
* MLOG Runtime

## Usage
## CLI Usage

```
Usage:
Expand Down
1 change: 1 addition & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ 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().String("stacked", "", "Use a provided memory cell/bank as a stack")

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

Expand Down
2 changes: 1 addition & 1 deletion cmd/transpile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ var transpileCmd = &cobra.Command{
result, err := transpiler.GolangToMLOGFile(args[0], transpiler.Options{
Numbers: viper.GetBool("numbers"),
Comments: viper.GetBool("comments"),
Debug: viper.GetBool("debug"),
Stacked: viper.GetString("stacked"),
})
if err != nil {
return err
Expand Down
15 changes: 8 additions & 7 deletions m/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ func init() {
Translate: func(args []transpiler.Resolvable, vars []transpiler.Resolvable) ([]transpiler.MLOGStatement, error) {
memoryName := strings.Trim(args[0].GetValue(), "\"")

if memoryName == transpiler.StackCellName {
return nil, errors.New("can't read/write to memory cell that is used for the stack: " + transpiler.StackCellName)
// TODO Remove hardcode
if memoryName == "bank1" {
return nil, errors.New("can't read/write to memory cell that is used for the stack: bank1")
}

return []transpiler.MLOGStatement{
Expand All @@ -40,8 +41,8 @@ func init() {
Translate: func(args []transpiler.Resolvable, _ []transpiler.Resolvable) ([]transpiler.MLOGStatement, error) {
memoryName := strings.Trim(args[1].GetValue(), "\"")

if memoryName == transpiler.StackCellName {
return nil, errors.New("can't read/write to memory cell that is used for the stack: " + transpiler.StackCellName)
if memoryName == "bank1" {
return nil, errors.New("can't read/write to memory cell that is used for the stack: bank1")
}

return []transpiler.MLOGStatement{
Expand Down Expand Up @@ -132,8 +133,8 @@ func init() {
{
&transpiler.Value{Value: "sensor"},
vars[0],
&transpiler.Value{Value: args[0].GetValue()},
&transpiler.Value{Value: args[1].GetValue()},
&transpiler.Value{Value: strings.Trim(args[0].GetValue(), "\"")},
&transpiler.Value{Value: strings.Trim(args[1].GetValue(), "\"")},
},
},
},
Expand Down Expand Up @@ -176,6 +177,6 @@ func Radar(from string, target1 RadarTarget, target2 RadarTarget, target3 RadarT
}

// Extract information indicated by sense from the provided block
func Sensor(block string, sense string) int {
func Sensor(block interface{}, sense string) float64 {
return 0
}
8 changes: 4 additions & 4 deletions m/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func init() {
&transpiler.Value{Value: "image"},
&transpiler.Value{Value: args[0].GetValue()},
&transpiler.Value{Value: args[1].GetValue()},
&transpiler.Value{Value: args[2].GetValue()},
&transpiler.Value{Value: strings.Trim(args[2].GetValue(), "\"")},
&transpiler.Value{Value: args[3].GetValue()},
&transpiler.Value{Value: args[4].GetValue()},
},
Expand Down Expand Up @@ -263,19 +263,19 @@ func DrawLineRect(x int, y int, width int, height int) {
}

// Draw a filled equilateral polygon centered around the provided point
func DrawPoly(x int, y int, sides int, radius float32, rotation float32) {
func DrawPoly(x int, y int, sides int, radius float64, rotation float64) {
}

// Draw an outlined equilateral polygon centered around the provided point
func DrawLinePoly(x int, y int, sides int, radius float32, rotation float32) {
func DrawLinePoly(x int, y int, sides int, radius float64, rotation float64) {
}

// Draw a filled triangle between provided points
func DrawTriangle(x1 int, y1 int, x2 int, y2 int, x3 int, y3 int) {
}

// Draw provided icon centered around the provided point
func DrawImage(x int, y int, image string, size float32, rotation float32) {
func DrawImage(x int, y int, image string, size float64, rotation float64) {
}

// Flush all draw statements to the provided display block
Expand Down
56 changes: 56 additions & 0 deletions m/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,47 @@ func init() {
}, nil
},
})
transpiler.RegisterFuncTranslation("m.Log10", 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: "op"},
&transpiler.Value{Value: "log10"},
vars[0],
&transpiler.Value{Value: args[0].GetValue()},
},
},
},
}, nil
},
})
transpiler.RegisterFuncTranslation("m.Ceil", 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: "op"},
&transpiler.Value{Value: "ceil"},
vars[0],
&transpiler.Value{Value: args[0].GetValue()},
},
},
},
}, nil
},
})
//op idiv result a b
}

// TODO Operations
Expand All @@ -52,7 +93,22 @@ func Floor(number float64) int {
return 0
}

// Ceil the provided floating point number and convert to integer
func Ceil(number float64) int {
return 0
}

// Perform an integer division on provided numbers
func IntDiv(a int, b int) int {
return 0
}

// Generate a random floating point number between 0 (inclusive) and max (exclusive)
func Random(max float64) float64 {
return 0
}

// Return a log10 function of the input number
func Log10(number float64) float64 {
return 0
}
4 changes: 3 additions & 1 deletion m/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ type Unit = interface{}

type HealthC = interface{}

type Building = interface{}
type Building = struct {
HealthC
}

type BlockFlag = string

Expand Down
8 changes: 4 additions & 4 deletions m/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func init() {
Statement: [][]transpiler.Resolvable{
{
&transpiler.Value{Value: "ubind"},
&transpiler.Value{Value: args[0].GetValue()},
&transpiler.Value{Value: strings.Trim(args[0].GetValue(), "\"")},
},
},
},
Expand Down Expand Up @@ -177,19 +177,19 @@ func UnitLocateOre(ore string) (x int, y int, found bool) {
//
// Also locates blocks outside the range of the unit
func UnitLocateBuilding(buildingType BlockFlag, enemy bool) (x int, y int, found bool, building Building) {
return 0, 0, false, nil
return 0, 0, false, Building{}
}

// Locate the enemy spawn
//
// Also locates blocks outside the range of the unit
func UnitLocateSpawn() (x int, y int, found bool, building Building) {
return 0, 0, false, nil
return 0, 0, false, Building{}
}

// Locate a damaged building
//
// Also locates blocks outside the range of the unit
func UnitLocateDamaged() (x int, y int, found bool, building Building) {
return 0, 0, false, nil
return 0, 0, false, Building{}
}
4 changes: 2 additions & 2 deletions m/unit_control.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func UnitTarget(x float64, y float64, shoot bool) {
// Shoot with the cached unit at the predicted position of target unit
//
// If shoot parameter is false, it will cease firing
func UnitTargetP(target int, shoot bool) {
func UnitTargetP(target HealthC, shoot bool) {
}

// Drops items into the provided building
Expand Down Expand Up @@ -396,7 +396,7 @@ func UnitBuild(x float64, y float64, block string, rotation int, config int) {

// Retrieve the building and its type at the specified absolute position
func UnitGetBlock(x float64, y float64) (blockType string, building Building) {
return "", nil
return "", Building{}
}

// Checks whether there is a unit within the specified radius around the provided absolute position
Expand Down
2 changes: 1 addition & 1 deletion tests/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestBase(t *testing.T) {
{
name: "Sensor",
input: TestMain(`x := m.Sensor("A", "B")`),
output: `sensor _main_x "A" "B"`,
output: `sensor _main_x A B`,
},
}
for _, test := range tests {
Expand Down
5 changes: 2 additions & 3 deletions tests/constant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ const y = x
func main() {
print(x)
}`,
output: `set @stack 0
set x 1
output: `set x 1
set y x
jump 4 always
jump 3 always
print x`,
},
}
Expand Down
2 changes: 1 addition & 1 deletion tests/draw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestDraw(t *testing.T) {
{
name: "DrawImage",
input: TestMain(`m.DrawImage(1, 2, "A", 4, 5)`),
output: `draw image 1 2 "A" 4 5`,
output: `draw image 1 2 A 4 5`,
},
{
name: "DrawFlush",
Expand Down
Loading

0 comments on commit 984ceb0

Please sign in to comment.