Strings
Some checks failed
Build / build (push) Failing after 1m35s
Test / build (push) Failing after 1m31s

This commit is contained in:
Chuck Smith
2024-02-21 16:29:53 -05:00
parent 8caeaca559
commit e4bca02235
5 changed files with 189 additions and 5 deletions

93
Makefile Normal file
View File

@@ -0,0 +1,93 @@
.PHONY: dev build cli server install image release profile compare bench test clean
CGO_ENABLED=0
VERSION ?= $(shell git describe 2>/dev/null || echo "")
DESTDIR ?= $(GOBIN)
ifeq ($(LOCAL), 1)
IMAGE := r.mills.io/prologic/monkey
TAG := dev
else
ifeq ($(BRANCH), master)
IMAGE := prologic/monkey
TAG := latest
else
IMAGE := prologic/monkey
TAG := dev
endif
endif
all: help
help: ## Show this help message
@echo "monkey - Monkey Lang"
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[$$()% a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)
dev: cli ## Build monkey and run the REPL
@./monkey
build: clean cli server ## Build monkey
cli: ## Build monkey CLI
@go build \
-tags "netgo static_build" -installsuffix netgo \
-ldflags "-w -X go.mills.io/monkey/v2.MonkeyVersion=$(VERSION)" \
./cmd/monkey/...
server: ## Build Monkey server
@go build \
-tags "netgo static_build" -installsuffix netgo \
-ldflags "-w -X go.mills.io/monkey/v2.MonkeyVersion=$(VERSION)" \
./cmd/monkey-server/...
install: cli server ## Install monkey to $DESTDIR
@install -D -m 755 monkey $(DESTDIR)/monkey
@install -D -m 755 monkey-server $(DESTDIR)monkey-server
ifeq ($(PUBLISH), 1)
image: ## Build and Publish the Docker image
@docker buildx build \
--build-arg VERSION="$(VERSION)" \
--build-arg COMMIT="$(COMMIT)" \
--build-arg BUILD="$(BUILD)" \
--platform linux/amd64,linux/arm64 --push -t $(IMAGE):$(TAG) .
else
image: ## Build the Docker image
@docker build \
--build-arg VERSION="$(VERSION)" -t $(IMAGE):$(TAG) .
endif
release: ## Release monkey
@./tools/release.sh
profile: ## Run tests with profiling enabled
@go test -cpuprofile cpu.prof -memprofile mem.prof -v -bench ./...
compare: ## Run benchmarks comparing Monkey with other languages
@hyperfine -w 5 -p 'make build; gcc -o examples/fib examples/fib.c' \
-n c -n go -n tengo -n python -n tauc -n taugo -n monkey \
--sort mean-time --export-markdown Benchmark.md \
'./examples/fib' \
'go run examples/fib.go' \
'tengo examples/fib.tengo' \
'python3 examples/fib.py' \
'tauc examples/fib.tau' \
'taugo examples/fib.tau' \
'./monkey examples/fib.m'
bench: # Run test benchmarks
@go test -v -benchmem -bench=. ./...
test: ## Run unit tests
@go test -v \
-cover \
-coverprofile coverage.out \
-covermode atomic \
-coverpkg ./... \
-race \
./...
clean: ## Cleanup untrakced files
@git clean -f -d -X 2> /dev/null || true

View File

@@ -190,6 +190,10 @@ func (c *Compiler) Compile(node ast.Node) error {
c.emit(code.OpGetGlobal, symbol.Index)
case *ast.StringLiteral:
str := &object.String{Value: node.Value}
c.emit(code.OpConstant, c.addConstant(str))
}
return nil

View File

@@ -277,6 +277,31 @@ func TestGlobalLetStatements(t *testing.T) {
runCompilerTests(t, tests)
}
func TestStringExpressions(t *testing.T) {
tests := []compilerTestCase{
{
input: `"monkey"`,
expectedConstants: []interface{}{"monkey"},
expectedInstructions: []code.Instructions{
code.Make(code.OpConstant, 0),
code.Make(code.OpPop),
},
},
{
input: `"mon" + "key"`,
expectedConstants: []interface{}{"mon", "key"},
expectedInstructions: []code.Instructions{
code.Make(code.OpConstant, 0),
code.Make(code.OpConstant, 1),
code.Make(code.OpAdd),
code.Make(code.OpPop),
},
},
}
runCompilerTests(t, tests)
}
func runCompilerTests(t *testing.T, tests []compilerTestCase) {
t.Helper()
@@ -347,6 +372,12 @@ func testConstants(t *testing.T, expected []interface{}, actual []object.Object)
if err != nil {
return fmt.Errorf("constant %d = testIntegerObject failed : %s", i, err)
}
case string:
err := testStringObject(constant, actual[i])
if err != nil {
return fmt.Errorf("constant %d = testStringObject failed : %s", i, err)
}
}
}
return nil
@@ -364,3 +395,16 @@ func testIntegerObject(expected int64, actual object.Object) interface{} {
return nil
}
func testStringObject(expected string, actual object.Object) error {
result, ok := actual.(*object.String)
if !ok {
return fmt.Errorf("object is not String. got %T (%+v", actual, actual)
}
if result.Value != expected {
return fmt.Errorf("object has wrong value. got=%q, want=%q", result.Value, expected)
}
return nil
}

View File

@@ -177,11 +177,14 @@ func (vm *VM) executeBinaryOperation(op code.Opcode) error {
leftType := left.Type()
rightRight := right.Type()
if leftType == object.INTEGER_OBJ && rightRight == object.INTEGER_OBJ {
switch {
case leftType == object.INTEGER_OBJ && rightRight == object.INTEGER_OBJ:
return vm.executeBinaryIntegerOperation(op, left, right)
}
case leftType == object.STRING_OBJ && rightRight == object.STRING_OBJ:
return vm.executeBinaryStringOperation(op, left, right)
default:
return fmt.Errorf("unsupported types for binary operation: %s %s", leftType, rightRight)
}
}
func (vm *VM) executeBinaryIntegerOperation(op code.Opcode, left, right object.Object) error {
@@ -206,6 +209,17 @@ func (vm *VM) executeBinaryIntegerOperation(op code.Opcode, left, right object.O
return vm.push(&object.Integer{Value: result})
}
func (vm *VM) executeBinaryStringOperation(op code.Opcode, left, right object.Object) error {
if op != code.OpAdd {
return fmt.Errorf("unknown string operator: %d", op)
}
leftValue := left.(*object.String).Value
rightValue := right.(*object.String).Value
return vm.push(&object.String{Value: leftValue + rightValue})
}
func (vm *VM) executeComparison(op code.Opcode) error {
right := vm.pop()
left := vm.pop()

View File

@@ -55,6 +55,12 @@ func testExpectedObject(t *testing.T, expected interface{}, actual object.Object
t.Errorf("testBooleanObject failed: %s", err)
}
case string:
err := testStringObject(expected, actual)
if err != nil {
t.Errorf("testStringObject failed: %s", err)
}
case *object.Null:
if actual != Null {
t.Errorf("object is not Null: %T (%+v)", actual, actual)
@@ -68,7 +74,7 @@ func parse(input string) *ast.Program {
return p.ParseProgram()
}
func testIntegerObject(expected int64, actual object.Object) interface{} {
func testIntegerObject(expected int64, actual object.Object) error {
result, ok := actual.(*object.Integer)
if !ok {
return fmt.Errorf("object is not Integer. got=%T (%+v", actual, actual)
@@ -81,7 +87,7 @@ func testIntegerObject(expected int64, actual object.Object) interface{} {
return nil
}
func testBooleanObject(expected bool, actual object.Object) interface{} {
func testBooleanObject(expected bool, actual object.Object) error {
result, ok := actual.(*object.Boolean)
if !ok {
return fmt.Errorf("object is not Boolean. got=%T (%+v", actual, actual)
@@ -94,6 +100,19 @@ func testBooleanObject(expected bool, actual object.Object) interface{} {
return nil
}
func testStringObject(expected string, actual object.Object) error {
result, ok := actual.(*object.String)
if !ok {
return fmt.Errorf("object is not String. got=%T (%+v", actual, actual)
}
if result.Value != expected {
return fmt.Errorf("object has wrong value. got=%q, want=%q", result.Value, expected)
}
return nil
}
func TestIntegerArithmetic(t *testing.T) {
tests := []vmTestCase{
{"1", 1},
@@ -175,3 +194,13 @@ func TestGlobalLetStatements(t *testing.T) {
runVmTests(t, tests)
}
func TestStringExpressions(t *testing.T) {
tests := []vmTestCase{
{`"monkey"`, "monkey"},
{`"mon" + "key"`, "monkey"},
{`"mon" + "key" + "banana"`, "monkeybanana"},
}
runVmTests(t, tests)
}