Skip to content

Commit

Permalink
removing the internal dir
Browse files Browse the repository at this point in the history
  • Loading branch information
dfirebaugh committed Jan 4, 2025
1 parent 12a8797 commit 24051ea
Show file tree
Hide file tree
Showing 30 changed files with 63 additions and 80 deletions.
File renamed without changes.
3 changes: 2 additions & 1 deletion internal/ast/expressions.go → ast/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"strings"

"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/token"
)

type Identifier struct {
Expand Down Expand Up @@ -150,6 +150,7 @@ func (pe *PrefixExpression) TokenLiteral() string {
}
return pe.TokenLiteral()
}

func (pe *PrefixExpression) String() string {
var out bytes.Buffer

Expand Down
2 changes: 1 addition & 1 deletion internal/ast/function.go → ast/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"strings"

"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/token"
)

type Parameter struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/ast/literals.go → ast/literals.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strconv"
"strings"

"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/token"
)

type IntegerLiteral struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/ast/statements.go → ast/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ast
import (
"bytes"

"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/token"
)

type ExpressionStatement struct {
Expand Down
2 changes: 1 addition & 1 deletion internal/ast/struct.go → ast/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"strings"

"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/token"
)

type StructField struct {
Expand Down
6 changes: 3 additions & 3 deletions cmd/punch/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"os"
"os/exec"

"github.com/dfirebaugh/punch/internal/emitters/js"
"github.com/dfirebaugh/punch/internal/lexer"
"github.com/dfirebaugh/punch/internal/parser"
"github.com/dfirebaugh/punch/emitters/js"
"github.com/dfirebaugh/punch/lexer"
"github.com/dfirebaugh/punch/parser"
)

func main() {
Expand Down
2 changes: 1 addition & 1 deletion cmd/repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"os/user"

"github.com/dfirebaugh/punch/internal/repl"
"github.com/dfirebaugh/punch/repl"
)

func main() {
Expand Down
6 changes: 3 additions & 3 deletions internal/compiler/compiler.go → compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package compiler

import (
"github.com/bytecodealliance/wasmtime-go"
"github.com/dfirebaugh/punch/internal/emitters/wat"
"github.com/dfirebaugh/punch/internal/lexer"
"github.com/dfirebaugh/punch/internal/parser"
"github.com/dfirebaugh/punch/emitters/wat"
"github.com/dfirebaugh/punch/lexer"
"github.com/dfirebaugh/punch/parser"
)

const (
Expand Down
4 changes: 2 additions & 2 deletions internal/emitters/js/js.go → emitters/js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"fmt"
"strings"

"github.com/dfirebaugh/punch/internal/ast"
"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/ast"
"github.com/dfirebaugh/punch/token"
)

const (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"strings"

"github.com/dfirebaugh/punch/internal/ast"
"github.com/dfirebaugh/punch/ast"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions internal/emitters/wat/wat.go → emitters/wat/wat.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"log"
"strings"

"github.com/dfirebaugh/punch/internal/ast"
"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/ast"
"github.com/dfirebaugh/punch/token"
)

const (
Expand Down
1 change: 0 additions & 1 deletion examples/struct.pun
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

pkg main

struct extra {
Expand Down
3 changes: 2 additions & 1 deletion internal/lexer/collector.go → lexer/collector.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package lexer

import "github.com/dfirebaugh/punch/internal/token"
import "github.com/dfirebaugh/punch/token"

type Collector struct {
result []token.Token
Expand All @@ -9,6 +9,7 @@ type Collector struct {
func (c *Collector) Collect(t token.Token) {
c.result = append(c.result, t)
}

func (c *Collector) Result() []token.Token {
return c.result
}
2 changes: 1 addition & 1 deletion internal/lexer/lexer.go → lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"strings"
"text/scanner"

"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/token"
)

type Lexer struct {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package lexer_test
import (
"testing"

"github.com/dfirebaugh/punch/internal/lexer"
"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/lexer"
"github.com/dfirebaugh/punch/token"
)

func TestLexer(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion internal/lexer/lexer_test.go → lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"testing"
"text/scanner"

"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/token"
)

type testCollector struct{}
Expand Down
2 changes: 1 addition & 1 deletion internal/parser/errors.go → parser/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"strings"

"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/token"
"github.com/sirupsen/logrus"
)

Expand Down
4 changes: 2 additions & 2 deletions internal/parser/function.go → parser/function.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package parser
import (
"fmt"

"github.com/dfirebaugh/punch/internal/ast"
"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/ast"
"github.com/dfirebaugh/punch/token"
)

func (p *Parser) parseFunctionStatement() *ast.FunctionStatement {
Expand Down
6 changes: 3 additions & 3 deletions internal/parser/parser.go → parser/parser.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package parser

import (
"github.com/dfirebaugh/punch/internal/ast"
"github.com/dfirebaugh/punch/internal/lexer"
"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/ast"
"github.com/dfirebaugh/punch/lexer"
"github.com/dfirebaugh/punch/token"
)

type Parser struct {
Expand Down
22 changes: 2 additions & 20 deletions internal/parser/parsers.go → parser/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"strconv"

"github.com/dfirebaugh/punch/internal/ast"
"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/ast"
"github.com/dfirebaugh/punch/token"
)

func (p *Parser) ParseProgram(filename string) *ast.Program {
Expand Down Expand Up @@ -354,18 +354,15 @@ func (p *Parser) parseComment() {
p.nextToken()
}
case token.SLASH_ASTERISK:
// Advance past the opening token
p.nextToken()

// Loop until we find the closing token
for !(p.curTokenIs(token.ASTERISK) && p.peekTokenIs(token.SLASH)) {
if p.curTokenIs(token.EOF) {
return
}
p.nextToken()
}

// Advance past the closing token
p.nextToken()
default:
return
Expand Down Expand Up @@ -460,14 +457,10 @@ func (p *Parser) inferType(value ast.Expression) token.Type {
func (p *Parser) parseForStatement() *ast.ForStatement {
p.trace("parsing for statement", p.curToken.Literal, p.peekToken.Literal)

// Create a ForStatement node in your AST
stmt := &ast.ForStatement{Token: p.curToken}

// Consume the "for" token so p.curToken is now what's after "for"
p.nextToken()

// -- Step 1: Parse the init statement --------------------------------
// e.g., "i32 i = 1;"
stmt.Init = p.parseStatement()
if stmt.Init == nil {
p.error("expected initialization statement in for loop")
Expand All @@ -476,8 +469,6 @@ func (p *Parser) parseForStatement() *ast.ForStatement {

p.trace("current token", p.curToken.Literal)

// After parsing the init statement, we must see a semicolon.
// For example, "i32 i = 1;" must end with ';'
if !p.curTokenIs(token.SEMICOLON) {
p.error("expected ';' after for-init statement")
return nil
Expand All @@ -486,10 +477,6 @@ func (p *Parser) parseForStatement() *ast.ForStatement {
p.nextToken()

p.trace("current token", p.curToken.Literal)
// -- Step 2: Parse the condition -------------------------------------
// e.g., "i <= n;"
// Condition can be empty in Go-like syntax, but your grammar might require it.
// We'll parse an expression if we don't see another semicolon right away.
if !p.curTokenIs(token.SEMICOLON) {
stmt.Condition = p.parseExpression(LOWEST)
if stmt.Condition == nil {
Expand All @@ -506,20 +493,15 @@ func (p *Parser) parseForStatement() *ast.ForStatement {
// consume the second ';'
p.nextToken()

// -- Step 3: Parse the post statement --------------------------------
// e.g., "i = i + 1"
// This can also be empty in many “Go-like” syntaxes, but typically you have something.
if !p.curTokenIs(token.LBRACE) {
stmt.Post = p.parseStatement()
}

// -- Step 4: Finally, we expect '{' to parse the for-block -----------
if !p.curTokenIs(token.LBRACE) {
p.error("expected '{' after for loop post statement")
return nil
}

// parseBlockStatement parses everything in { ... } until matching '}'
stmt.Body = p.parseBlockStatement()

if !p.curTokenIs(token.LBRACE) {
Expand Down
2 changes: 1 addition & 1 deletion internal/parser/precedence.go → parser/precedence.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package parser

import "github.com/dfirebaugh/punch/internal/token"
import "github.com/dfirebaugh/punch/token"

// precedence order
const (
Expand Down
34 changes: 17 additions & 17 deletions internal/parser/struct.go → parser/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package parser
import (
"fmt"

"github.com/dfirebaugh/punch/internal/ast"
"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/ast"
"github.com/dfirebaugh/punch/token"
)

func (p *Parser) parseStructDefinition() *ast.StructDefinition {
Expand Down Expand Up @@ -134,24 +134,24 @@ func (p *Parser) parseStructLiteral() ast.Expression {
}

func (p *Parser) parseStructFieldAccess(left ast.Expression) ast.Expression {
p.nextToken() // consume the dot
p.nextToken() // consume the dot

if !p.expectPeek(token.IDENTIFIER) {
p.error("expected identifier after dot operator")
return nil
}
if !p.expectPeek(token.IDENTIFIER) {
p.error("expected identifier after dot operator")
return nil
}

fieldAccess := &ast.StructFieldAccess{
Token: p.curToken, // the dot
Left: left,
Field: &ast.Identifier{Token: p.peekToken, Value: p.peekToken.Literal},
}
fieldAccess := &ast.StructFieldAccess{
Token: p.curToken, // the dot
Left: left,
Field: &ast.Identifier{Token: p.peekToken, Value: p.peekToken.Literal},
}

p.nextToken() // consume the field identifier
p.nextToken() // consume the field identifier

if p.peekTokenIs(token.DOT) {
return p.parseStructFieldAccess(fieldAccess)
}
if p.peekTokenIs(token.DOT) {
return p.parseStructFieldAccess(fieldAccess)
}

return fieldAccess
return fieldAccess
}
Binary file added punch
Binary file not shown.
6 changes: 3 additions & 3 deletions internal/repl/repl.go → repl/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"io"
"strings"

"github.com/dfirebaugh/punch/internal/emitters/wat"
"github.com/dfirebaugh/punch/internal/lexer"
"github.com/dfirebaugh/punch/internal/parser"
"github.com/dfirebaugh/punch/emitters/wat"
"github.com/dfirebaugh/punch/lexer"
"github.com/dfirebaugh/punch/parser"

"github.com/chzyer/readline"
)
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
10 changes: 5 additions & 5 deletions tools/ast_explorer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"net/http"
"os"

"github.com/dfirebaugh/punch/internal/emitters/js"
"github.com/dfirebaugh/punch/internal/emitters/wat"
"github.com/dfirebaugh/punch/internal/lexer"
"github.com/dfirebaugh/punch/internal/parser"
"github.com/dfirebaugh/punch/internal/token"
"github.com/dfirebaugh/punch/emitters/js"
"github.com/dfirebaugh/punch/emitters/wat"
"github.com/dfirebaugh/punch/lexer"
"github.com/dfirebaugh/punch/parser"
"github.com/dfirebaugh/punch/token"
)

func parseHandler(w http.ResponseWriter, r *http.Request) {
Expand Down
10 changes: 5 additions & 5 deletions tools/punchgen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
"fmt"
"syscall/js"

js_gen "github.com/dfirebaugh/punch/internal/emitters/js"
"github.com/dfirebaugh/punch/internal/emitters/wat"
"github.com/dfirebaugh/punch/internal/lexer"
"github.com/dfirebaugh/punch/internal/parser"
"github.com/dfirebaugh/punch/internal/token"
js_gen "github.com/dfirebaugh/punch/emitters/js"
"github.com/dfirebaugh/punch/emitters/wat"
"github.com/dfirebaugh/punch/lexer"
"github.com/dfirebaugh/punch/parser"
"github.com/dfirebaugh/punch/token"
)

func parse(this js.Value, p []js.Value) interface{} {
Expand Down

0 comments on commit 24051ea

Please sign in to comment.