-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Houcine EL ADDALI
committed
Dec 18, 2023
1 parent
97fa1e4
commit 136dab7
Showing
4 changed files
with
140 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package runtime | ||
|
||
import ( | ||
ast "github.com/houcine7/JIPL/internal/AST" | ||
"github.com/houcine7/JIPL/internal/types" | ||
) | ||
|
||
func Eval(node ast.Node) types.ObjectJIPL { | ||
switch node := node.(type) { | ||
case *ast.Program: | ||
return evalAllStatements(node.Statements) | ||
case *ast.ExpressionStatement: | ||
return Eval(node.Expression) | ||
case *ast.IntegerLiteral: | ||
return &types.Integer{Val: node.Value} | ||
case *ast.BooleanExp: | ||
return &types.Boolean{Val: node.Value} | ||
default: | ||
return nil | ||
} | ||
} | ||
|
||
func evalAllStatements(stms []ast.Statement) types.ObjectJIPL { | ||
var resrult types.ObjectJIPL | ||
|
||
for _, stm := range stms { | ||
resrult = Eval(stm) | ||
} | ||
|
||
return resrult | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package runtime | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/houcine7/JIPL/internal/lexer" | ||
"github.com/houcine7/JIPL/internal/parser" | ||
"github.com/houcine7/JIPL/internal/types" | ||
) | ||
|
||
func TestIntegerEval(t *testing.T) { | ||
testData := []struct { | ||
input string | ||
expected int | ||
}{ | ||
{"4545;", 4545}, | ||
{"7;", 7}, | ||
} | ||
|
||
for _, test := range testData { | ||
evaluated := getEvaluated(test.input) | ||
testIntegerObject(t, evaluated, test.expected) | ||
} | ||
} | ||
|
||
func getEvaluated(input string) types.ObjectJIPL { | ||
l := lexer.InitLexer(input) | ||
p := parser.InitParser(l) | ||
program := p.Parse() | ||
return Eval(program) | ||
} | ||
|
||
func testIntegerObject(t *testing.T, obj types.ObjectJIPL, expected int) { | ||
intObj, ok := obj.(*types.Integer) | ||
if !ok { | ||
t.Fatalf("the obj is not of type types.Integer, instead got %T", | ||
obj, | ||
) | ||
} | ||
if intObj.Val != expected { | ||
t.Fatalf("the value of the integer object is not valid expected :%d and got %d", expected, intObj.Val) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package types | ||
|
||
import "fmt" | ||
|
||
type TypeObj string | ||
|
||
type ObjectJIPL interface { | ||
GetType() TypeObj | ||
ToString() string | ||
} | ||
|
||
type Integer struct { | ||
Val int | ||
} | ||
|
||
type Boolean struct { | ||
Val bool | ||
} | ||
|
||
type Undefined struct{} | ||
|
||
// implementing OBjectJIPL interface by supporeted types | ||
func (und *Undefined) ToString() string { | ||
return "undefined" | ||
} | ||
|
||
func (und *Undefined) GetType() TypeObj { | ||
return T_UNDEFINED | ||
} | ||
|
||
func (boolObj *Boolean) ToString() string { | ||
return fmt.Sprintf("%t", boolObj.Val) | ||
} | ||
|
||
func (boolObj *Boolean) GetType() TypeObj { | ||
return T_BOOLEAN | ||
} | ||
|
||
func (intObj *Integer) ToString() string { | ||
return fmt.Sprintf("%d", intObj.Val) | ||
} | ||
|
||
func (intObj *Integer) GetType() TypeObj { | ||
return T_ITNTEGER | ||
} | ||
|
||
// cte of types | ||
const ( | ||
T_ITNTEGER = "INTEGER" | ||
T_BOOLEAN = "BOOLEAN" | ||
T_UNDEFINED = "UNDEFINED" | ||
) |