Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 23 additions & 33 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
name: goreleaser

on:
push:
branches:
- "main"

tags:
- "*"

pull_request:
push:
tags:
- "*"

permissions:
contents: write
contents: write

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: "1.19"

- name: Tests
run: go test -v ./...

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
distribution: goreleaser
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
goreleaser:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0

- uses: actions/setup-go@v4
with:
go-version: "1.20"

- name: goreleaser
uses: goreleaser/goreleaser-action@v2
with:
distribution: goreleaser
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
35 changes: 35 additions & 0 deletions .github/workflows/tests-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: tests-lint

on:
push:
branches:
- "main"
pull_request:

permissions:
contents: read

jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.20"

- name: tests
run: go test -v ./...

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v4
with:
go-version: "1.20"

- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: "latest"
6 changes: 4 additions & 2 deletions ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package ast

type Node interface {
TokenLiteral() string
String() string
}

type Statement interface{ Node }
type Expression interface{ Node }
type Expression interface {
Node
String() string
}
57 changes: 1 addition & 56 deletions ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,6 @@ type PrefixExpression struct {
}

func (pe *PrefixExpression) TokenLiteral() string { return pe.Token.Literal }
func (pe *PrefixExpression) String() string {
var out bytes.Buffer
out.WriteString("(")
out.WriteString(pe.Operator)
out.WriteString(pe.Right.String())
out.WriteString(")")
return out.String()
}

type InfixExpression struct {
Expression
Expand All @@ -75,40 +67,6 @@ type InfixExpression struct {
}

func (oe *InfixExpression) TokenLiteral() string { return oe.Token.Literal }
func (oe *InfixExpression) String() string {
var out bytes.Buffer
out.WriteString("(")
out.WriteString(oe.Left.String())
out.WriteString(" " + oe.Operator + " ")
out.WriteString(oe.Right.String())
out.WriteString(")")
return out.String()
}

type IfExpression struct {
Expression

Token token.Token // The 'if' token
Condition Expression
ThenBranch Statement
ElseBranch Statement
}

func (ie *IfExpression) TokenLiteral() string { return ie.Token.Literal }
func (ie *IfExpression) String() string {
var out bytes.Buffer
out.WriteString("if")
out.WriteString(ie.Condition.String())
out.WriteString(" ")
out.WriteString(ie.ThenBranch.String())

if ie.ElseBranch != nil {
out.WriteString("else ")
out.WriteString(ie.ElseBranch.String())
}

return out.String()
}

type FunctionLiteral struct {
Expression
Expand All @@ -120,20 +78,7 @@ type FunctionLiteral struct {

func (fl *FunctionLiteral) TokenLiteral() string { return fl.Token.Literal }
func (fl *FunctionLiteral) String() string {
var out bytes.Buffer

params := []string{}
for _, p := range fl.Parameters {
params = append(params, p.String())
}

out.WriteString(fl.TokenLiteral())
out.WriteString("(")
out.WriteString(strings.Join(params, ", "))
out.WriteString(") ")
out.WriteString(fl.Body.String())

return out.String()
return "fn"
}

type CallExpression struct {
Expand Down
31 changes: 11 additions & 20 deletions ast/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,6 @@ func (p *Program) TokenLiteral() string {
}
}

func (p *Program) String() string {
var out bytes.Buffer

for _, s := range p.Statements {
out.WriteString(s.String())
out.WriteString("\n")
}

return out.String()
}

type LetStatement struct {
Statement

Expand Down Expand Up @@ -99,15 +88,6 @@ type BlockStatement struct {
}

func (bs *BlockStatement) TokenLiteral() string { return bs.Token.Literal }
func (bs *BlockStatement) String() string {
var out bytes.Buffer

for _, s := range bs.Statements {
out.WriteString(s.String())
}

return out.String()
}

type ForStatement struct {
Statement
Expand Down Expand Up @@ -165,3 +145,14 @@ type EchoStatement struct {

func (es *EchoStatement) TokenLiteral() string { return es.Token.Literal }
func (es *EchoStatement) String() string { return "" }

type IfStatement struct {
Statement

Token token.Token // The 'if' token
Condition Expression
ThenBranch Statement
ElseBranch Statement
}

func (ie *IfStatement) TokenLiteral() string { return ie.Token.Literal }
67 changes: 0 additions & 67 deletions cmd/run.go

This file was deleted.

14 changes: 12 additions & 2 deletions cmd/vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,18 @@ var vmCommand = &cobra.Command{
compiler := compiler.NewCompiler()
instructions := compiler.Compile(program)

virtualM := vm.NewVM(compiler.Constants)
virtualM.Interpret(instructions)
if debug {
fmt.Println(instructions)
}

mainFrame := compiler.Frames[0]
virtualM := vm.NewVM(compiler.Constants,
vm.Frame{
Instructions: instructions,
NumOfLocals: len(mainFrame.Locals),
},
)
virtualM.Interpret()
},
}

Expand Down
Loading