Init project base

This commit is contained in:
2024-02-23 02:44:19 -06:00
parent f699d59ba8
commit 23713169d5
4 changed files with 44 additions and 39 deletions

View File

@@ -1,38 +1,15 @@
module Main (main) where
import Lib
import Text.Parsec
import Text.Parsec.String
import Text.ParserCombinators.Parsec.Number
import Parser (parseFile)
type Var = String
data Statement = Print Var | Assign Var Int | Add Var Var | While Var Code deriving (Eq, Show)
type Code = [Statement]
-- Part 1: Parser
statementParser :: Parser Statement
statementParser = try printParser <|> try assignParser <|> try addParser <|> whileParser
printParser :: Parser Statement
printParser = Print <$> (string "print" >> many1 space >> many1 letter)
assignParser :: Parser Statement
assignParser = Assign <$> many1 letter <*> (many1 space >> char '=' >> many1 space >> int)
addParser :: Parser Statement
addParser = Add <$> many1 letter <*> (many1 space >> string "+=" >> many1 space >> many1 letter)
whileParser :: Parser Statement
whileParser = While <$> (string "while" >> many1 space >> many1 letter) <*> (many1 space >> string "positive" >> many1 space >> char '{' >> many1 space >> many (statementParser <* many1 space) <* char '}')
codeParser :: Parser Code
codeParser = spaces >> many (statementParser <* many1 space) <* eof
process :: String -> IO ()
process line = do
let res = parseFile line
case res of
Left err -> print err
Right ex -> mapM_ print ex
main :: IO ()
main = do
fileText <- readFile "input.txt"
let parsed = parse codeParser "input.txt" fileText
print parsed
fileText <- readFile "test.vi"
process fileText

View File

@@ -1,6 +0,0 @@
module Lib
( someFunc
) where
someFunc :: IO ()
someFunc = putStrLn "someFunc"

32
test.vi Normal file
View File

@@ -0,0 +1,32 @@
---
Here is a brief sample of the
language and its syntax
---
-- Import statements
import std.collections {
Heap,
HashMap,
}
-- Function declaration with types
fn fizz-buzz(i: Num): Str {
--- This is technically a legal comment ---
let fb = "";
if i % 3 == 0 {
fb = fb ++ "fizz";
}
if i % 5 == 0 {
fb ++= "buzz";
}
return i if Str.is-empty(fb) else fb;
}
-- Function declaration without types
fn main() {
const fb: Str = fizz-buzz();
print(fb);
}

View File

@@ -25,7 +25,9 @@ source-repository head
library
exposed-modules:
Lib
Lexer
Parser
Syntax
other-modules:
Paths_violet
autogen-modules: