Skip to content

Commit

Permalink
refactor: move to Standard Go Project Layout
Browse files Browse the repository at this point in the history
  • Loading branch information
Houcine EL ADDALI committed Dec 18, 2023
1 parent fad3808 commit 7d7b92d
Show file tree
Hide file tree
Showing 15 changed files with 78 additions and 62 deletions.
45 changes: 0 additions & 45 deletions REPL/repel.go

This file was deleted.

60 changes: 60 additions & 0 deletions cmd/REPL/repel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package repl

import (
"bufio"
"fmt"
"io"

"github.com/houcine7/JIPL/internal/lexer"
"github.com/houcine7/JIPL/internal/parser"
)

// REPL :Read --> Evaluate --> Print --> loop
// the repl used to interact with users to read from console
// and send to interpreter to evaluate then prints back the result

/*
* Function as the start method of the repl
* To interact with the user via terminal
*/

const PROMPT = ">"

func Start(in io.Reader, out io.Writer) {
scanner := bufio.NewScanner(in)

fmt.Println(" ********** ")
fmt.Println("------------- Welcome to JIPL REPL ------------")
fmt.Println(" ********** ")

for {
fmt.Print(PROMPT)
scanned := scanner.Scan()
if !scanned {
return
}
line := scanner.Text()

if line == "exit" || line == "exit;" {
break
}

replLexer := lexer.InitLexer(line)
repParser := parser.InitParser(replLexer)

pr := repParser.Parse()
errs := repParser.Errors()

if len(errs) != 0 {
io.WriteString(out, fmt.Sprintf("%d errors ❌❌ occurred while parsing your input \n", len(errs)))
for idx, e := range errs {
io.WriteString(out, fmt.Sprintf("error number:%d with message: %s \n", idx, e))
}
continue
}

io.WriteString(out, pr.ToString())
io.WriteString(out, "\n")

}
}
2 changes: 1 addition & 1 deletion main.go → cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"os/user"

repl "github.com/houcine7/JIPL/REPL"
repl "github.com/houcine7/JIPL/cmd/REPL"
)

func main() {
Expand Down
3 changes: 2 additions & 1 deletion AST/ast_imp.go → internal/AST/ast_imp.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ast
import (
"bytes"

"github.com/houcine7/JIPL/token"
"github.com/houcine7/JIPL/internal/token"
)

/*
Expand Down Expand Up @@ -102,6 +102,7 @@ func (resStm *ReturnStatement) ToString() string {
var bf bytes.Buffer
bf.WriteString(resStm.TokenLiteral())
if resStm.ReturnValue != nil {
bf.WriteRune(' ')
bf.WriteString(resStm.ReturnValue.ToString())
}
bf.WriteString(";")
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion AST/ast_test.go → internal/AST/ast_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package ast
import (
"testing"

"github.com/houcine7/JIPL/token"
"github.com/houcine7/JIPL/internal/token"
)

// test toString method
Expand Down
4 changes: 2 additions & 2 deletions lexer/lexer.go → internal/lexer/lexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package lexer
import (
"unicode/utf8"

"github.com/houcine7/JIPL/token"
"github.com/houcine7/JIPL/utils"
"github.com/houcine7/JIPL/internal/token"
"github.com/houcine7/JIPL/pkg/utils"
)

type Lexer struct {
Expand Down
2 changes: 1 addition & 1 deletion lexer/lexer_test.go → internal/lexer/lexer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"log"
"testing"

"github.com/houcine7/JIPL/token"
"github.com/houcine7/JIPL/internal/token"
)

//basic test
Expand Down
File renamed without changes.
16 changes: 8 additions & 8 deletions parser/parser.go → internal/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"strconv"

ast "github.com/houcine7/JIPL/AST"
"github.com/houcine7/JIPL/lexer"
"github.com/houcine7/JIPL/token"
ast "github.com/houcine7/JIPL/internal/AST"
"github.com/houcine7/JIPL/internal/lexer"
"github.com/houcine7/JIPL/internal/token"
)

/*
Expand Down Expand Up @@ -181,14 +181,14 @@ func (p *Parser) parseFunctionExpression() ast.Expression {
if !p.expectedNextToken(token.NewToken(token.IDENTIFIER, "")) {
return nil
}
fmt.Println("------ in Identifier ----")
//fmt.Println("------ in Identifier ----")
exp.Name = p.parseIdentifier().(*ast.Identifier)
if !p.expectedNextToken(token.NewToken(token.LP, "(")) {
return nil
}

fmt.Println("function.Name is ", exp.Name)
fmt.Println(p.currToken)
//fmt.Println("function.Name is ", exp.Name)
//fmt.Println(p.currToken)

// fn params
exp.Parameters = p.parsePramas()
Expand Down Expand Up @@ -342,7 +342,7 @@ func (p *Parser) parseBlocStatements() *ast.BlockStm {
func (p *Parser) parseExpressionStatement() *ast.ExpressionStatement {

//debuggin puropos
defer trace("parse expression statements called..", p)
//defer trace("parse expression statements called..", p)
//fmt.Println("------->", p.currToken)
stm := &ast.ExpressionStatement{Token: p.currToken}

Expand Down Expand Up @@ -392,7 +392,7 @@ func (p *Parser) parseInt() ast.Expression {
val, err := strconv.ParseInt(p.currToken.Value, 0, 0)

if err != nil {
fmt.Println(p.currToken)
//fmt.Println(p.currToken)
errMsg := fmt.Sprintf("Parsing error, couldn't parse string %s to Integer value",
p.currToken.Value)
p.errors = append(p.errors, errMsg)
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions parser/parser_test.go → internal/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"fmt"
"testing"

ast "github.com/houcine7/JIPL/AST"
"github.com/houcine7/JIPL/lexer"
"github.com/houcine7/JIPL/parser/data"
ast "github.com/houcine7/JIPL/internal/AST"
"github.com/houcine7/JIPL/internal/lexer"
"github.com/houcine7/JIPL/internal/parser/data"
)

/*TEST functions*/
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 7d7b92d

Please sign in to comment.