Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For mit Index + unit Tests #111

Merged
merged 6 commits into from
Jan 29, 2025
Merged
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Der Changelog von DDP. Sortiert nach Release.

## In Entwicklung

- [Neu] Bei Iterierenden Schleifen kann man jetzt einen Index angeben (Für jeden Typname t mit Index i in ...)
- [Anders] Der Kompilierer benutzt jetzt LLVM Version 14.0.0 (anstatt 12.0.0)
- [Neu] Duden/Befehlszeile zum Arbeiten mit Befehlszeilenargumenten
- [Fix] Verschachtelte Struktur Literale verhalten sich jetzt mit Einbindungen korrekt
Expand Down
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ TEST_DIRS =
# will hold additional arguments to pass to kddp
KDDP_ARGS =

test-unit:
go test $(shell go list ./src/... | grep -v compiler)

test-normal: all ## runs the tests
go test -v ./tests '-run=(TestKDDP|TestStdlib|TestBuildExamples|TestStdlibCoverage)' -test_dirs="$(TEST_DIRS)" -kddp_args="$(KDDP_ARGS)" | $(SED) ''/PASS/s//$$(printf "\033[32mPASS\033[0m")/'' | $(SED) ''/FAIL/s//$$(printf "\033[31mFAIL\033[0m")/''

Expand All @@ -199,7 +202,7 @@ test-sumtypes: ## validates that sumtypes in the source tree are correctly used
coverage: all ## creates a coverage report for tests/testdata/stdlib
go test -v ./tests '-run=TestStdlibCoverage' | $(SED) -u ''/PASS/s//$$(printf "\033[32mPASS\033[0m")/'' | $(SED) -u ''/FAIL/s//$$(printf "\033[31mFAIL\033[0m")/''

test: test-sumtypes test-normal-memory coverage ## runs all the tests
test: test-unit test-sumtypes test-normal-memory coverage ## runs all the tests

test-with-optimizations: ## runs all tests with full optimizations enabled
'$(MAKE)' KDDP_ARGS="-O 2" test
Expand Down
2 changes: 1 addition & 1 deletion src/ast/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TrimStringLit(lit *token.Token) string {
if lit == nil {
return ""
}
return strings.Trim(lit.Literal, "\"")
return strings.TrimSuffix(strings.TrimPrefix(lit.Literal, "\""), "\"")
}

// returns wether table is the global scope
Expand Down
73 changes: 73 additions & 0 deletions src/ast/helper_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package ast

import (
"testing"

"github.com/DDP-Projekt/Kompilierer/src/token"
"github.com/stretchr/testify/assert"
)

func TestIsExternFunc(t *testing.T) {
assert := assert.New(t)

assert.True(IsExternFunc(&FuncDecl{
Body: nil,
Def: nil,
}))
assert.False(IsExternFunc(&FuncDecl{
Body: &BlockStmt{},
Def: nil,
}))
assert.False(IsExternFunc(&FuncDecl{
Body: nil,
Def: &FuncDef{},
}))
assert.False(IsExternFunc(&FuncDecl{
Body: &BlockStmt{},
Def: &FuncDef{},
}))
}

func TestIsForwardDecl(t *testing.T) {
assert := assert.New(t)

assert.True(IsForwardDecl(&FuncDecl{
Body: nil,
Def: &FuncDef{},
}))
assert.False(IsForwardDecl(&FuncDecl{
Body: nil,
Def: nil,
}))
assert.False(IsForwardDecl(&FuncDecl{
Body: &BlockStmt{},
Def: nil,
}))
}

func TestIsOperatorOverload(t *testing.T) {
assert := assert.New(t)

assert.True(IsOperatorOverload(&FuncDecl{
Operator: BIN_OR,
}))
assert.False(IsOperatorOverload(&FuncDecl{
Operator: nil,
}))
}

func TestTrimStringLit(t *testing.T) {
assert := assert.New(t)

assert.Equal("", TrimStringLit(nil))
assert.Equal("test", TrimStringLit(&token.Token{Literal: `"test"`}))
assert.Equal(`"test"`, TrimStringLit(&token.Token{Literal: `""test""`}))
assert.Equal("", TrimStringLit(&token.Token{Literal: `""`}))
}

func TestIsGlobalScope(t *testing.T) {
assert := assert.New(t)

assert.True(IsGlobalScope(&SymbolTable{Enclosing: nil}))
assert.False(IsGlobalScope(&SymbolTable{Enclosing: &SymbolTable{}}))
}
1 change: 1 addition & 0 deletions src/ast/statements.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type (
Range token.Range
For token.Token // Für
Initializer *VarDecl // InitVal is the same pointer as In
Index *VarDecl // optional index during iteration
In Expression // the string/list to range over
Body *BlockStmt
}
Expand Down
11 changes: 10 additions & 1 deletion src/compiler/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (

"github.com/DDP-Projekt/Kompilierer/src/ast"
"github.com/DDP-Projekt/Kompilierer/src/ast/annotators"
"github.com/DDP-Projekt/Kompilierer/src/compiler/llvm"
"github.com/DDP-Projekt/Kompilierer/src/ddperror"
"github.com/DDP-Projekt/Kompilierer/src/ddppath"
"github.com/DDP-Projekt/Kompilierer/src/ddptypes"
"github.com/DDP-Projekt/Kompilierer/src/token"
"github.com/DDP-Projekt/Kompilierer/src/compiler/llvm"

"github.com/llir/llvm/ir"
"github.com/llir/llvm/ir/constant"
Expand Down Expand Up @@ -2327,7 +2327,9 @@ func (c *compiler) VisitForRangeStmt(s *ast.ForRangeStmt) ast.VisitResult {
iter_ptr_type types.Type
end_ptr value.Value // points to the one-after-last element
length value.Value
index value.Value = c.NewAlloca(c.ddpinttyp.IrType())
)

if inTyp == c.ddpstring {
iter_ptr_type = i8ptr
iter_ptr = c.NewAlloca(iter_ptr_type)
Expand All @@ -2350,6 +2352,10 @@ func (c *compiler) VisitForRangeStmt(s *ast.ForRangeStmt) ast.VisitResult {
c.cbb = loopStart
irType := c.toIrType(s.Initializer.Type)
c.scp.addProtected(s.Initializer.Name(), c.NewAlloca(irType.IrType()), irType, false)
if s.Index != nil {
c.scp.addVar(s.Index.Name(), index, c.ddpinttyp, false)
c.cbb.NewStore(newInt(1), index)
}
c.cbb.NewBr(condBlock)

c.cbb = condBlock
Expand Down Expand Up @@ -2417,6 +2423,9 @@ func (c *compiler) VisitForRangeStmt(s *ast.ForRangeStmt) ast.VisitResult {
iter_ptr,
)
}
if s.Index != nil {
c.cbb.NewStore(c.cbb.NewAdd(c.cbb.NewLoad(c.ddpinttyp.IrType(), index), newInt(1)), index) // index += 1
}
c.cbb.NewBr(condBlock)

c.cbb = leaveBlock
Expand Down
Loading