Changes to be committed:

new file:   .gitignore
	new file:   docs/grammar.md
	new file:   haskell/Lexer.hi
	renamed:    haskell/main.hs -> haskell/Lexer.hs
	new file:   haskell/Lexer.o
	new file:   haskell/Main
	new file:   haskell/Main.hi
	new file:   haskell/Main.hs
	new file:   haskell/Main.o
	new file:   haskell/NewParser.hs
	new file:   haskell/Parser.hi
	new file:   haskell/Parser.hs
	new file:   haskell/Parser.o
	new file:   pascal/arr.pas
	new file:   pascal/modifiers.pas
	new file:   pascal/reserved_words.pas
	modified:   pascal/test.pas
This commit is contained in:
Michael Wain 2024-10-01 01:08:39 +03:00
parent 1d44a8bf13
commit 1bfba526de
17 changed files with 231 additions and 16 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
.vscode/launch.json
.vscode/tasks.json

5
docs/grammar.md Normal file
View File

@ -0,0 +1,5 @@
$$
[\text{halt}] \to halt([\text{expression}]);
\\
[\text{expression}] \to \text{Number}
$$

BIN
haskell/Lexer.hi Normal file

Binary file not shown.

View File

@ -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
where tt = token_type $ t !! i

BIN
haskell/Lexer.o Normal file

Binary file not shown.

BIN
haskell/Main Executable file

Binary file not shown.

BIN
haskell/Main.hi Normal file

Binary file not shown.

23
haskell/Main.hs Normal file
View File

@ -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

BIN
haskell/Main.o Normal file

Binary file not shown.

11
haskell/NewParser.hs Normal file
View File

@ -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 = []

BIN
haskell/Parser.hi Normal file

Binary file not shown.

50
haskell/Parser.hs Normal file
View File

@ -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

BIN
haskell/Parser.o Normal file

Binary file not shown.

20
pascal/arr.pas Normal file
View File

@ -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 = ""}]

61
pascal/modifiers.pas Normal file
View File

@ -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

53
pascal/reserved_words.pas Normal file
View File

@ -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

View File

@ -1 +1 @@
halt(1);
halt(2 + 3);