Skip to content

Commit

Permalink
feat: add a simple REPL to print lexing results (#2)
Browse files Browse the repository at this point in the history
## What does this PR do?

add a simple REPL to print lexing results
  • Loading branch information
Zecheng authored Apr 28, 2024
1 parent 1410f00 commit 92b760a
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
version: 2
updates:
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "monthly"
18 changes: 18 additions & 0 deletions .github/workflows/check-cc.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: Check for conventional commit compliance

on:
pull_request:
types: [opened, ready_for_review, edited]

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
check-cc:
name: Check for conventional commit compliance
runs-on: ubuntu-latest
steps:
- uses: agenthunt/[email protected]
with:
pr-title-regex: '^(build|chore|ci|docs|feat|fix|perf|refactor|revert|style|test)(\((\w|-)+\))?(!)?:.*'
15 changes: 8 additions & 7 deletions .github/workflows/go.yaml → .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go

name: Go
name: Lint, build, test

on:
push:
Expand All @@ -11,9 +8,13 @@ on:
branches:
- main

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build-test:
runs-on: ubuntu-22.04
lint-build-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

Expand All @@ -36,4 +37,4 @@ jobs:
- name: test
run: |
go install github.com/onsi/ginkgo/v2/ginkgo
ginkgo run -r -cover
ginkgo run -r -race -cover
10 changes: 3 additions & 7 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
name: Bump version
name: Release

on:
pull_request:
types:
- closed
push:
branches:
- main

jobs:
tag-release:
if: github.event.pull_request.merged == true
runs-on: ubuntu-22.04
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.pull_request.merge_commit_sha }}
fetch-depth: '0'

- name: Bump version and push tag
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ An interpreted language written in Go
- [ ] feat: add logical operators ||, &&
- [ ] feat: add bitwise operators ^, |, &
- [ ] refactor: unary operators, binary operators, ternary operators


- [ ] feat: use Cobra to enable multiple modes when launching the REPL
- [ ] feat: use quit(), exit(), or Ctrl-D to exit



Expand Down
13 changes: 12 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ package cmd

import (
"fmt"
"log"
"os"
"os/user"

"github.com/Aden-Q/monkey/internal/repl"
"github.com/spf13/cobra"
)

Expand All @@ -33,7 +36,15 @@ func Execute() {
}

func run(cmd *cobra.Command, args []string) {
fmt.Println("Hello, World!")
user, err := user.Current()
if err != nil {
log.Fatalf("user error: %v", err)
}

fmt.Printf("Hello %s! This is the Monkey programming language!\n", user.Username)

r := repl.New(repl.Config{})
r.Start(os.Stdin, os.Stdout)
}

func init() {
Expand Down
46 changes: 46 additions & 0 deletions internal/repl/repl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package repl

import (
"bufio"
"fmt"
"io"

"github.com/Aden-Q/monkey/internal/lexer"
"github.com/Aden-Q/monkey/internal/token"
)

const PROMPT = ">>> "

type REPL interface {
Start(in io.ReadCloser, out io.WriteCloser)
}

type Config struct {
}

type repl struct {
}

func New(config Config) REPL {
return &repl{}
}

func (r *repl) Start(in io.ReadCloser, out io.WriteCloser) {
scanner := bufio.NewScanner(in)

for {
fmt.Print(PROMPT)

scanned := scanner.Scan()
if !scanned {
return
}

line := scanner.Text()
l := lexer.New(line)

for tok, ok := l.NextToken(); ok && tok.Type != token.EOF; tok, ok = l.NextToken() {
fmt.Printf("%+v\n", tok)
}
}
}
13 changes: 13 additions & 0 deletions internal/repl/repl_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package repl_test

import (
"testing"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestRepl(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Repl Suite")
}
10 changes: 10 additions & 0 deletions internal/repl/repl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package repl_test

import (
. "github.com/onsi/ginkgo/v2"
_ "github.com/onsi/gomega"
)

var _ = Describe("Repl", func() {

})
6 changes: 3 additions & 3 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
@vendor:
go mod vendor

# run all tests with ginkgo
# run all unit tests with ginkgo
@test:
ginkgo run -r -cover -coverprofile=coverage.out
ginkgo run -r -race -cover -coverprofile=coverage.out

# build a binary executable
@build:
go build .

# run the Monkey language interpreter in an interactive shell env
# run the Monkey language interpreter in interactive mode
@run:
go run main.go

Expand Down

0 comments on commit 92b760a

Please sign in to comment.