diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..45ae748 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.vscode/launch.json +.vscode/tasks.json diff --git a/docs/grammar.md b/docs/grammar.md new file mode 100644 index 0000000..655587e --- /dev/null +++ b/docs/grammar.md @@ -0,0 +1,5 @@ +$$ +[\text{halt}] \to halt([\text{expression}]); +\\ +[\text{expression}] \to \text{Number} +$$ \ No newline at end of file diff --git a/haskell/Lexer.hi b/haskell/Lexer.hi new file mode 100644 index 0000000..d0b6539 Binary files /dev/null and b/haskell/Lexer.hi differ diff --git a/haskell/main.hs b/haskell/Lexer.hs similarity index 81% rename from haskell/main.hs rename to haskell/Lexer.hs index fdb893e..6d7c6bf 100644 --- a/haskell/main.hs +++ b/haskell/Lexer.hs @@ -1,8 +1,9 @@ -import System.IO +module Lexer (tokenize, Token (..), TokenType (..) ) where + import Data.List import Data.Char (ord) -data TokenType = Quotes | Dot | Colon | EndStatement | EOF | Literal | Number | Letter | Digit | Equals | OpenParen | CloseParen | BinaryOperator | Empty deriving (Show, Eq) +data TokenType = Quotes | Dot | Colon | EndStatement | EOF | QString | Literal | Number | Letter | Digit | Equals | OpenParen | CloseParen | BinaryOperator | Empty deriving (Show, Eq) data Token = Token {value :: [Char], token_type :: TokenType} deriving (Show) parseSingleChar :: Char -> Token @@ -30,7 +31,7 @@ conTokType tt reduceTokens :: [Token] -> Int -> [Token] reduceTokens all_tokens i | token_type ct == Digit || token_type ct == Letter = (Token (makeIdentifier (drop i all_tokens) 0) (conTokType $ token_type ct)):(_reduceTokens all_tokens (findFirstEmpty all_tokens i)) - | token_type ct == Quotes = (Token (_makeQuoteString (drop (succ i) all_tokens) 0) Literal):(_reduceTokens all_tokens (findFirstQuotes all_tokens (succ i))) + | token_type ct == Quotes = (Token (_makeQuoteString (drop (succ i) all_tokens) 0) QString):(_reduceTokens all_tokens (findFirstQuotes all_tokens (succ i))) | otherwise = ct:(_reduceTokens all_tokens (succ i)) where ct = all_tokens !! i @@ -66,15 +67,4 @@ findFirstQuotes :: [Token] -> Int -> Int findFirstQuotes t i | i >= length t || tt == Quotes = succ i | otherwise = findFirstQuotes t $ succ i - where tt = token_type $ t !! i - -main = do - handle <- openFile "test.pas" ReadMode - contents <- hGetContents handle - let x = tokenize contents - print x - --print $ findFirstEmpty x 0 - --print $ take (findFirstEmpty x 0) x - --print $ makeInt x - --print $ makeIdentifier x - hClose handle \ No newline at end of file + where tt = token_type $ t !! i \ No newline at end of file diff --git a/haskell/Lexer.o b/haskell/Lexer.o new file mode 100644 index 0000000..fae2c5a Binary files /dev/null and b/haskell/Lexer.o differ diff --git a/haskell/Main b/haskell/Main new file mode 100755 index 0000000..5ef3523 Binary files /dev/null and b/haskell/Main differ diff --git a/haskell/Main.hi b/haskell/Main.hi new file mode 100644 index 0000000..285e3c5 Binary files /dev/null and b/haskell/Main.hi differ diff --git a/haskell/Main.hs b/haskell/Main.hs new file mode 100644 index 0000000..f466a09 --- /dev/null +++ b/haskell/Main.hs @@ -0,0 +1,23 @@ +module Main where + +import System.IO +import Lexer +import Parser + +{- +main = do + let x = [Token "(" OpenParen, Token "1" Number, Token ")" CloseParen, Token ";" EndStatement] + print (_extractExpression x) -} + +main = do + handle <- openFile "../pascal/test.pas" ReadMode + contents <- hGetContents handle + let x = tokenize contents + print x + let y = parseIntoTree x + --print y + --print $ findFirstEmpty x 0 + --print $ take (findFirstEmpty x 0) x + --print $ makeInt x + --print $ makeIdentifier x + hClose handle \ No newline at end of file diff --git a/haskell/Main.o b/haskell/Main.o new file mode 100644 index 0000000..7ad42a2 Binary files /dev/null and b/haskell/Main.o differ diff --git a/haskell/NewParser.hs b/haskell/NewParser.hs new file mode 100644 index 0000000..a1af94b --- /dev/null +++ b/haskell/NewParser.hs @@ -0,0 +1,11 @@ +module Parser (parseIntoTree, TreeNode) where + +import Lexer (Token (..), TokenType (..) ) + +data NodeName = BinOperator | HaltNode | StringConstant | Constant | Void deriving (Show, Eq) +data TreeNode = TreeNode {name :: NodeName, children :: [TreeNode], node_val :: [Char]} deriving (Show) + +parseIntoTree :: [Token] -> [TreeNode] +parseIntoTree tokens + | length tokens > 0 = _parseIntoTree tokens + | otherwise = [] \ No newline at end of file diff --git a/haskell/Parser.hi b/haskell/Parser.hi new file mode 100644 index 0000000..84d727b Binary files /dev/null and b/haskell/Parser.hi differ diff --git a/haskell/Parser.hs b/haskell/Parser.hs new file mode 100644 index 0000000..74102ac --- /dev/null +++ b/haskell/Parser.hs @@ -0,0 +1,50 @@ +module Parser (parseIntoTree, TreeNode, _extractExpression) where + +import Lexer (Token (..), TokenType (..) ) + +data NodeName = BinOperator | HaltNode | StringConstant | Constant | Void deriving (Show, Eq) +data TreeNode = TreeNode {name :: NodeName, children :: [TreeNode], node_val :: [Char]} deriving (Show) + +processVoid :: [Token] -> [TreeNode] +processVoid tokens = (TreeNode Void (parseIntoTree (fst ft)) []):(parseIntoTree (snd ft)) + where ft = _extractExpression (tail tokens) + +_parseIntoTree :: [Token] -> [TreeNode] +_parseIntoTree tokens + | token_type ft == Literal = parseLiteral tokens + | token_type ft == QString = (TreeNode StringConstant [] (value ft)):[] + | token_type ft == Number = (TreeNode Constant [] (value ft)):[] + | token_type ft == BinaryOperator = (TreeNode BinOperator [] (value ft)):[] + | token_type ft == EndStatement = [] + | otherwise = processVoid tokens + where ft = head tokens + +parseIntoTree :: [Token] -> [TreeNode] +parseIntoTree tokens + | length tokens > 0 = _parseIntoTree tokens + | otherwise = [] + +parseLiteral :: [Token] -> [TreeNode] +parseLiteral tokens + | value ft == "halt" = processHalt tokens + where ft = head tokens + +processHalt :: [Token] -> [TreeNode] +processHalt tokens = (TreeNode HaltNode (parseIntoTree (fst hn)) []):(parseIntoTree (snd hn)) + where hn = _extractExpression (tail tokens) + +_extractExpression :: [Token] -> ([Token], [Token]) +_extractExpression tt = extractExpression tt tt 0 (-1) + +__extractExpression :: [Token] -> [Token] -> Int -> Int -> ([Token], [Token]) +__extractExpression tt at sto ski + | length tt > 0 = extractExpression tt at sto ski + | otherwise = ([], []) + +extractExpression :: [Token] -> [Token] -> Int -> Int -> ([Token], [Token]) +extractExpression tt at sto ski + | token_type ft == OpenParen = __extractExpression (tail tt) at (sto+1) (ski+1) + | token_type ft == CloseParen && ski > 0 = __extractExpression (tail tt) at (sto+1) (ski-1) + | token_type ft == CloseParen && ski <= 0 = (take (sto-1) (drop 1 at), drop sto at) + | otherwise = __extractExpression (tail tt) at (sto+1) ski + where ft = head tt \ No newline at end of file diff --git a/haskell/Parser.o b/haskell/Parser.o new file mode 100644 index 0000000..cdba1c6 Binary files /dev/null and b/haskell/Parser.o differ diff --git a/pascal/arr.pas b/pascal/arr.pas new file mode 100644 index 0000000..dc53035 --- /dev/null +++ b/pascal/arr.pas @@ -0,0 +1,20 @@ +[ + Token {value = "halt", token_type = Literal}, + Token {value = "(", token_type = OpenParen}, + Token {value = "2", token_type = Number}, + Token {value = "+", token_type = BinaryOperator}, + Token {value = "3", token_type = Number}, + Token {value = ")", token_type = CloseParen}, + Token {value = ";", token_type = EndStatement} +] + + +[ + TreeNode {name = HaltNode, children = [ + TreeNode {name = Constant, children = [], node_val = "2"} + ], node_val = ""}, + TreeNode {name = Void, children = [ + Token {value = ")", token_type = CloseParen}, + Token {value = ";", token_type = EndStatement} + ] +[], node_val = ""}] \ No newline at end of file diff --git a/pascal/modifiers.pas b/pascal/modifiers.pas new file mode 100644 index 0000000..0be8101 --- /dev/null +++ b/pascal/modifiers.pas @@ -0,0 +1,61 @@ +absolute +abstract +alias +assembler +bitpacked +break +cdecl +continue +cppdecl +cvar +default +deprecated +dynamic +enumerator +experimental +export +external +far +far16 +forward +generic +helper +implements +index +interrupt +iocheck +local +message +name +near +nodefault +noreturn +nostackframe +oldfpccall +otherwise +overload +override +pascal +platform +private +protected +public +published +read +register +reintroduce +result +safecall +saveregisters +softfloat +specialize +static +stdcall +stored +strict +unaligned +unimplemented +varargs +virtual +winapi +write \ No newline at end of file diff --git a/pascal/reserved_words.pas b/pascal/reserved_words.pas new file mode 100644 index 0000000..604c7fc --- /dev/null +++ b/pascal/reserved_words.pas @@ -0,0 +1,53 @@ +absolute +and +array +asm +begin +case +const +constructor +destructor +div +do +downto +else +end +file +for +function +goto +if +implementation +in +inherited +inline +interface +label +mod +nil +not +object +of +operator +or +packed +procedure +program +record +reintroduce +repeat +self +set +shl +shr +string +then +to +type +unit +until +uses +var +while +with +xor \ No newline at end of file diff --git a/pascal/test.pas b/pascal/test.pas index e7f489c..40c2b3b 100644 --- a/pascal/test.pas +++ b/pascal/test.pas @@ -1 +1 @@ -halt(1); \ No newline at end of file +halt(2 + 3); \ No newline at end of file