Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/itsfuad/walrus
Browse files Browse the repository at this point in the history
  • Loading branch information
itsfuad committed Dec 4, 2024
2 parents 60feaeb + 83711fb commit 68a32c8
Show file tree
Hide file tree
Showing 41 changed files with 563 additions and 526 deletions.
9 changes: 1 addition & 8 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,12 @@
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}"
},
{
"name": "Launch Package",
"type": "go",
"request": "launch",
"mode": "auto",
"program": "${workspaceFolder}/main.go"
"program": "${workspaceFolder}",
}
]
}
9 changes: 8 additions & 1 deletion ast/expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,9 +280,16 @@ func (a Indexable) EndPos() lexer.Position {
return a.Location.End
}


type StructProp struct {
Prop IdentifierExpr
Value Node
}


type StructLiteral struct {
Identifier IdentifierExpr
Properties map[string]Node
Properties map[string]StructProp
Location
}

Expand Down
47 changes: 33 additions & 14 deletions errgen/errorPrinter.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ const (
CODE_HINT
)

type ERROR_LEVEL int

const (
ERROR_CRITICAL ERROR_LEVEL = iota
ERROR_NORMAL
WARNING
INFO
)

type Hint struct {
message string
hintType HINT
Expand All @@ -28,11 +37,7 @@ type WalrusError struct {
colEnd int
err error
hints []Hint
}

func (e *WalrusError) DisplayWithPanic() {
fmt.Printf("Total hints: %d\n", len(e.hints))
DisplayErrors()
level ERROR_LEVEL
}

func PrintError(e *WalrusError, showFileName bool) {
Expand All @@ -42,7 +47,8 @@ func PrintError(e *WalrusError, showFileName bool) {
}

if showFileName {
utils.BLUE.Printf("\nIn file: %s:%d:%d\n", e.filePath, e.lineStart, e.colStart)
utils.BLUE.Print("\nIn file: ")
utils.GREY.Printf("%s:%d:%d\n", e.filePath, e.lineStart, e.colStart)
}

lines := strings.Split(string(fileData), "\n")
Expand All @@ -61,9 +67,14 @@ func PrintError(e *WalrusError, showFileName bool) {
lineNumber := fmt.Sprintf("%d | ", e.lineStart)
utils.GREY.Print(lineNumber)
fmt.Println(line)
underLine := fmt.Sprintf("%s^%s\n", strings.Repeat(" ", (e.colStart-1) + len(lineNumber)), strings.Repeat("~", hLen))
underLine := fmt.Sprintf("%s^%s\n", strings.Repeat(" ", (e.colStart-1)+len(lineNumber)), strings.Repeat("~", hLen))

utils.RED.Print(underLine)
if e.level == ERROR_CRITICAL {
//stop further execution
utils.BOLD_RED.Print("Critical Error: ")
}

utils.RED.Println(e.err.Error())

if len(e.hints) > 0 {
Expand All @@ -76,6 +87,11 @@ func PrintError(e *WalrusError, showFileName bool) {
}
}
}

if e.level == ERROR_CRITICAL {
utils.ORANGE.Println("Compilation stopped due to critical error. Resolve the critical error to continue compilation")
os.Exit(-1)
}
}

func (e *WalrusError) AddHint(msg string, htype HINT) *WalrusError {
Expand All @@ -94,7 +110,7 @@ func (e *WalrusError) AddHint(msg string, htype HINT) *WalrusError {
return e
}

func makeError(filePath string, lineStart, lineEnd int, colStart, colEnd int, errMsg string) *WalrusError {
func makeError(filePath string, lineStart, lineEnd int, colStart, colEnd int, errMsg string, level ERROR_LEVEL) *WalrusError {
if lineStart < 1 {
lineStart = 1
}
Expand All @@ -115,32 +131,35 @@ func makeError(filePath string, lineStart, lineEnd int, colStart, colEnd int, er
colStart: colStart,
colEnd: colEnd,
err: errors.New(errMsg),
level: level,
}

globalErrors = append(globalErrors, err)

return err
}

//global errors are arrays of error pointers
// global errors are arrays of error pointers
var globalErrors []*WalrusError

// make an errorlist to add all errors and display later
func AddError(filePath string, lineStart, lineEnd int, colStart, colEnd int, err string) *WalrusError {
errItem := makeError(filePath, lineStart, lineEnd, colStart, colEnd, err)
func AddError(filePath string, lineStart, lineEnd int, colStart, colEnd int, err string, level ERROR_LEVEL) *WalrusError {
errItem := makeError(filePath, lineStart, lineEnd, colStart, colEnd, err, level)
utils.YELLOW.Printf("Error added on %s:%d:%d. %d errors available\n", filePath, lineStart, colStart, len(globalErrors))
if level == ERROR_CRITICAL {
DisplayErrors()
}
return errItem
}

func DisplayErrors() {
if len(globalErrors) == 0 {
utils.GREEN.Println("------- Passed --------")
return
} else {
utils.BOLD_RED.Printf("%d error(s) found\n", len(globalErrors))
}
for _, err := range globalErrors {
PrintError(err, true)
}
utils.BOLD_RED.Printf("%d error(s) found\n", len(globalErrors))
//os.Exit(-1)
panic("Errors found")
}
101 changes: 43 additions & 58 deletions errgen/errorPrinter_test.go
Original file line number Diff line number Diff line change
@@ -1,69 +1,54 @@
package errgen

import (
"errors"
"testing"
)

const (
filename = "testfile.go"
errorMsg = "test error"
)
func TestAddHint(t *testing.T) {
err := &WalrusError{}
err.AddHint("This is a hint", TEXT_HINT)

func TestMakeError(t *testing.T) {
tests := []struct {
filePath string
lineStart int
lineEnd int
colStart int
colEnd int
errMsg string
expected *WalrusError
}{
{
filePath: filename,
lineStart: 10,
lineEnd: 10,
colStart: 5,
colEnd: 15,
errMsg: errorMsg,
expected: &WalrusError{
filePath: filename,
lineStart: 10,
lineEnd: 10,
colStart: 5,
colEnd: 15,
err: errors.New(errorMsg),
},
},
{
filePath: filename,
lineStart: -1,
lineEnd: -1,
colStart: -1,
colEnd: -1,
errMsg: errorMsg,
expected: &WalrusError{
filePath: filename,
lineStart: 1,
lineEnd: 1,
colStart: 1,
colEnd: 1,
err: errors.New(errorMsg),
},
},
if len(err.hints) != 1 {
t.Errorf("Expected 1 hint, got %d", len(err.hints))
}

for _, tt := range tests {
result := makeError(tt.filePath, tt.lineStart, tt.lineEnd, tt.colStart, tt.colEnd, tt.errMsg)
if result.filePath != tt.expected.filePath ||
result.lineStart != tt.expected.lineStart ||
result.lineEnd != tt.expected.lineEnd ||
result.colStart != tt.expected.colStart ||
result.colEnd != tt.expected.colEnd ||
result.err.Error() != tt.expected.err.Error() {
t.Errorf("MakeError(%s, %d, %d, %d, %d, %s) = %+v; expected %+v",
tt.filePath, tt.lineStart, tt.lineEnd, tt.colStart, tt.colEnd, tt.errMsg, result, tt.expected)
}
if err.hints[0].message != "This is a hint" {
t.Errorf("Expected hint message 'This is a hint', got '%s'", err.hints[0].message)
}

if err.hints[0].hintType != TEXT_HINT {
t.Errorf("Expected hint type TEXT_HINT, got %d", err.hints[0].hintType)
}
}

func TestMakeError(t *testing.T) {
err := makeError("test.go", 1, 1, 1, 1, "Test error", ERROR_CRITICAL)

if err.filePath != "test.go" {
t.Errorf("Expected filePath 'test.go', got '%s'", err.filePath)
}

if err.lineStart != 1 {
t.Errorf("Expected lineStart 1, got %d", err.lineStart)
}

if err.lineEnd != 1 {
t.Errorf("Expected lineEnd 1, got %d", err.lineEnd)
}

if err.colStart != 1 {
t.Errorf("Expected colStart 1, got %d", err.colStart)
}

if err.colEnd != 1 {
t.Errorf("Expected colEnd 1, got %d", err.colEnd)
}

if err.err.Error() != "Test error" {
t.Errorf("Expected error message 'Test error', got '%s'", err.err.Error())
}

if err.level != ERROR_CRITICAL {
t.Errorf("Expected error level ERROR_CRITICAL, got %d", err.level)
}
}
Empty file added go.sum
Empty file.
3 changes: 2 additions & 1 deletion language/arrayAccess.wal
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ let arr2 := [[1,4], [4,7 * 5], arr, [4]];
let arp := 20;

let p : i32;
p = arr2[arp][PI as i32];

//p = arr2[arp][PI]; // cannot use type 'f32' to index array

//arr2[0][0][0]; // error - cannot access index of type i32

Expand Down
11 changes: 11 additions & 0 deletions language/expressions.wal
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ let b := a * (34 - 18);
let c : f32 = (a * 34 - 12) as f32; //12
let d : i32 = a - 3.1416 * 18;

a += 24;

//multiple variable declaration in one line
// with type
let t1: i32 = 43, t2: f32 = 3.5, t3: str;
Expand Down Expand Up @@ -40,6 +42,15 @@ let myMap : map[str]i32 = map[str]i32{

let mapValue := myMap["key1"];

type Int i32;
/*
impl Int {
fn toStr() -> str {
ret "0";
}
}
*/

//type i32 i32; // error

//let x : i32 = 10.6; // error
Expand Down
2 changes: 1 addition & 1 deletion language/function.wal
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ testFn2(4, fn (a: i32, b: f32) -> i32 {
ret a + 5;
});

fn add(a: i32, b?: i32 = 9) -> i32 {
fn add(a: i32, b?: i32 = 10) -> i32 {
ret a + b;
}

Expand Down
15 changes: 12 additions & 3 deletions language/structs.wal
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ let distance := @Distance{
min: 0,
};

//let a := distance; // error
//a = distance; // error

//distance.max = 9; // error - private prop
distance.min = 1;
Expand Down Expand Up @@ -92,7 +92,7 @@ fn getPoint() -> Point {

impl Rectangle {
fn area() -> f32 {
ret width * height;
ret this.width * height;
}

fn perimeter() -> f32 {
Expand All @@ -104,7 +104,16 @@ fn DrawShape(shape: IShape) {
//shape.radius; // error
let area := shape.area();
let perimeter := shape.perimeter();
//shape.do(); // error
}

type CC Circle;
type CC2 CC;

let cc1 : CC = @Circle{ radius: 10.0 };
let cc2 : Circle = @CC2{ radius: 10.0 };
let cc3 : Circle = i;
let cc4 : Circle = rectangle;

DrawShape(circle);
DrawShape(rectangle);
DrawShape(rectangle);
Loading

0 comments on commit 68a32c8

Please sign in to comment.