Skip to content

Commit

Permalink
CLI & IR optimization (#7)
Browse files Browse the repository at this point in the history
* Add basic optimization passes

* Add basic cli

* Add more cli options

* Add pending analyzer passes

* Fix Go CI workflow
  • Loading branch information
marcauberer authored Jul 16, 2021
1 parent 5a63f6a commit 530127b
Show file tree
Hide file tree
Showing 450 changed files with 199,449 additions and 52 deletions.
15 changes: 15 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
version: 2
updates:
# CLI dependency updates
- package-ecosystem: gomod
directory: /cli
schedule:
interval: daily
time: "04:00"
timezone: Europe/Berlin
open-pull-requests-limit: 15
target-branch: main
reviewers:
- chillibits/compiler-team
assignees:
- chillibits/compiler-team
File renamed without changes.
46 changes: 46 additions & 0 deletions .github/workflows/ci-go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Go CI
name: Go CI

on:
push:
paths:
- 'cli/**'
pull_request:
branches: [ main, dev, feature/** ]

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
go-version: [1.15.x, 1.16.x]
os: [ubuntu-latest, windows-latest]
defaults:
run:
working-directory: ./cli

steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v2

- name: Prepare environment
run: |
go version
go get -u golang.org/x/lint/golint
go mod vendor
- name: Run build
run: go build -x

- name: Run vet & lint
run: |
go vet .
golint .
- name: Run tests
run: go test
21 changes: 21 additions & 0 deletions .github/workflows/misspell.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Workflow to detect typos in docs and source code
name: Misspell
on:
push:
branches:
- main
- docs/**
pull_request:
jobs:
spellcheck:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2

- name: Misspell
uses: reviewdog/action-misspell@v1
with:
github_token: ${{ secrets.github_token }}
exclude: ./cli/vendor/**
locale: "US"
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ The Spice grammar can be found [here](./compiler/src/grammar/Spice.g4) as a ANTL
- [x] Additive operators are only applied to some type combinations
- [x] Multiplicative operators are only applied to some type combinations
- [x] Prefix/postfix unary operators are only applied to integers
- [ ] Program contains main function
- [ ] `++` and `--` are only applied on identifiers
- [x] Program contains main function
- [x] `++` and `--` are only applied on identifiers

## Special language features
- Something like `"Test" * 3` is valid and will evaluate to `"TestTestTest"`
- Alternatively to the return statement in a function, you can also assign variable `result` with a value, which was automatically declared by the function header
- Unary minus has to be applied without a space between (e.g.: `-3.4`) and binary minus has to be applied with a space beween (e.g.: `n - 5`)
- Unary minus has to be applied without a space between (e.g.: `-3.4`) and binary minus has to be applied with a space between (e.g.: `n - 5`)
- Default values of function/procedure parameters are possible e.g.: `f<int> test(int param = 2) {}`

## CMake instructions for building
Expand Down
9 changes: 9 additions & 0 deletions build-cli.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@echo off
mkdir bin 2> NUL

cd cli
go env -w GOOS=windows
go env -w GOARCH=amd64
go build -o ../bin/spice.exe .

cd ..
8 changes: 6 additions & 2 deletions build.bat
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
@echo off
mkdir bin 2> NUL
cd bin

cd cli
go env -w GOOS=windows
go env -w GOARCH=amd64
go build -o ../bin/spice.exe .

cd ../bin
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -G "CodeBlocks - MinGW Makefiles" ../compiler
cmake --build . --target Spice_run -- -j 6

move src\Spice_run.exe spicec.exe

cd ..
18 changes: 18 additions & 0 deletions cli/cmd/build.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package cmd

import (
"spice/internal"
"strings"
)

func Build(sourceFile string, targetTriple string, outputPath string) {
// Set default value for outputPath
if outputPath == "" {
outputPath = "./output.o"
} else if !strings.HasSuffix(outputPath, ".o") {
outputPath += ".o"
}

// Compile program ane emit executable to output file
internal.Compile(sourceFile, targetTriple, outputPath)
}
6 changes: 6 additions & 0 deletions cli/cmd/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package cmd

func Install(sourceFile string) {
// Compile program and emit executable to local bin dir

}
16 changes: 16 additions & 0 deletions cli/cmd/run.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package cmd

import (
"os"
"spice/internal"
)

func Run(sourceFile string) {
// Compile program and emit executable file to tmp dir
tmpPath := os.TempDir()
println(tmpPath)
internal.Compile(sourceFile, "", tmpPath+"/spice-output.o")

// Run executable

}
16 changes: 16 additions & 0 deletions cli/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module spice

go 1.16

require (
github.com/cli/safeexec v1.0.0
github.com/cpuguy83/go-md2man/v2 v2.0.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/fatih/color v1.12.0
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/urfave/cli/v2 v2.3.0
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)
37 changes: 37 additions & 0 deletions cli/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/cli/safeexec v1.0.0 h1:0VngyaIyqACHdcMNWfo6+KdUYnqEr2Sg+bSP1pdF+dI=
github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0 h1:EoUDS0afbrsXAZ9YQ9jdu/mZ2sXgT1/2yyNng4PGlyM=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.12.0 h1:mRhaKNwANqRgUBGKmnI5ZxEk7QXmjQeCcuYFMX2bfcc=
github.com/fatih/color v1.12.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/mattn/go-isatty v0.0.13 h1:qdl+GuBjcsKKDco5BsxPJlId98mSWNKqYA+Co0SC1yA=
github.com/mattn/go-isatty v0.0.13/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c h1:F1jZWGFhYfh0Ci55sIpILtKKK8p3i2/krTr0H1rg74I=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
40 changes: 40 additions & 0 deletions cli/internal/interface.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package internal

import (
"os/exec"
"spice/util"
"strconv"
"syscall"

"github.com/cli/safeexec"
)

const COMPILER_EXECUTABLE_NAME = "spicec"

func Compile(sourceFile string, targetTriple string, outputPath string) {
// Check if compiler executable exists
if !CommandExists(COMPILER_EXECUTABLE_NAME) {
util.Error("Compiler executable not found. Please check your installation / re-install Spice", true)
}

// Execute compiler executable. e.g.: spicec "./sourceFile.spice" "x86_64-w64-windows-gnu"
cmd := exec.Command(COMPILER_EXECUTABLE_NAME, sourceFile, targetTriple, outputPath)
output, err := cmd.CombinedOutput()
if err != nil {
if exiterr, ok := err.(*exec.ExitError); ok {
if status, ok := exiterr.Sys().(syscall.WaitStatus); ok {
util.Pel()
util.Pel()
util.Error("Compiler exited with status code "+strconv.Itoa(status.ExitStatus())+"\nFailed to compile: "+string(output), true)
}
} else {
util.Error("Failed to call compiler executable", true)
}
}
}

// CommandExists checks if the stated command exists on the host system
func CommandExists(cmd string) bool {
_, err := safeexec.LookPath(cmd)
return err == nil
}
79 changes: 79 additions & 0 deletions cli/spice.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
"log"
"os"
"spice/cmd"
"spice/util"

"github.com/urfave/cli/v2"
)

// nolint: gochecknoglobals
var (
version = "dev"
commit = ""
date = ""
builtBy = ""
)

func main() {
// Version flag
cli.VersionFlag = &cli.BoolFlag{
Name: "version",
Aliases: []string{"v"},
Usage: "Prints the Spice version, you're running",
}

// Main cli configuration
app := &cli.App{
Name: "spice",
Version: util.BuildVersion(version, commit, date, builtBy),
Authors: []*cli.Author{
{Name: "Marc Auberer", Email: "marc.auberer@chillibits.com"},
},
Copyright: "© 2021 Marc Auberer",
Usage: "Official CLI for the Spice programming language.",
Commands: []*cli.Command{
{
Name: "build",
Aliases: []string{"b"},
Usage: "Builds your Spice program and emits an executable",
Flags: []cli.Flag{
&cli.StringFlag{Name: "target", Aliases: []string{"t"}, Usage: "Target triple for the emitted executable (for cross-compiling)"},
&cli.PathFlag{Name: "output", Aliases: []string{"o"}, Usage: "Path to the location where the output executable should go"},
},
Action: func(c *cli.Context) error {
cmd.Build(c.Args().Get(0), c.String("target"), c.Path("output"))
return nil
},
},
{
Name: "install",
Aliases: []string{"i"},
Usage: "Builds your Spice program, installs it and adds it to the path env variable",
Flags: []cli.Flag{},
Action: func(c *cli.Context) error {
cmd.Install(c.Args().Get(0))
return nil
},
},
{
Name: "run",
Aliases: []string{"r"},
Usage: "Builds your Spice program and runs it",
Flags: []cli.Flag{},
Action: func(c *cli.Context) error {
cmd.Run(c.Args().Get(0))
return nil
},
},
},
UseShortOptionHandling: true,
}

err := app.Run(os.Args)
if err != nil {
log.Fatal(err)
}
}
Loading

0 comments on commit 530127b

Please sign in to comment.