package main

import "core:fmt"
import "core:os"

import "lex"
import "shuntingyard"
import "vm"


main :: proc() {

	my_vm := vm.new_vm()
	defer vm.delete_vm(my_vm)
	for {
		inp_buf := make([]byte, 256)
		defer delete(inp_buf)

		fmt.print("> ")
		char_count, err := os.read(os.stdin, inp_buf[:])
		if err < 0 {
			fmt.println("[!] ERROR", err)
			return
		}

		if char_count < 1 {
			break
		}

		// Remove new line
		src := string(inp_buf[:char_count - 1])
		// catch quit or exit
		if src == "quit" || src == "exit" {
			break
		} else if src == "" {
			continue
		}

		// src := read_file("./math.txt")
		tokens := lex.lex(src)

		ast := shuntingyard.shunting_yard(tokens)

		vm.load_code(my_vm, ast[:])
		res := vm.execute(my_vm)
		fmt.println(res)
	}
}

read_file :: proc(path: string) -> string {
	src_bytes, ok := os.read_entire_file_from_filename("./math.txt")
	src := cast(string)src_bytes
	if !ok {
		fmt.println("Could not read file")
		os.exit(1)
	}

	return src
}