From 554fbb4bb0123c46db2452928e42b8ee9f96d749 Mon Sep 17 00:00:00 2001 From: Houcine EL ADDALI Date: Sun, 3 Dec 2023 15:45:22 +0100 Subject: [PATCH] feat: repel v0 to print tokens --- REPL/repel.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ main.go | 21 +++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 REPL/repel.go create mode 100644 main.go diff --git a/REPL/repel.go b/REPL/repel.go new file mode 100644 index 0000000..9dd2e01 --- /dev/null +++ b/REPL/repel.go @@ -0,0 +1,45 @@ +package repl + +import ( + "bufio" + "fmt" + "io" + + "github.com/houcine7/JIPL/lexer" + "github.com/houcine7/JIPL/token" +) + +// 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 REPLE ------------") + fmt.Println(" ********** ") + for { + fmt.Print(PROMPT) + scanned := scanner.Scan() + if !scanned { + return + } + line :=scanner.Text() + + replLexer :=lexer.InitLexer(line); + + for tok:=replLexer.NextToken(); tok.Type!=token.FILE_ENDED;{ + fmt.Printf("%+v\n",tok) + tok=replLexer.NextToken() + } + } + +} \ No newline at end of file diff --git a/main.go b/main.go new file mode 100644 index 0000000..16e884e --- /dev/null +++ b/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "fmt" + "os" + "os/user" + + repl "github.com/houcine7/JIPL/REPL" +) + +func main() { + currUser, err := user.Current(); + + if err !=nil{ + panic(err) + } + + fmt.Printf("Hello %s!, welcome to JIPL happy coding :)",currUser.Username) + fmt.Printf("Start typing JIPL code ...\n") + repl.Start(os.Stdin,os.Stdout) +} \ No newline at end of file