Improving Lexer, starting Parser

modified:   app/Lexer.hs
	modified:   app/Parser.hs
	modified:   as/test.as
This commit is contained in:
Michael Wain 2024-11-04 05:08:51 +03:00
parent 2cee932739
commit 819885b6c1
3 changed files with 12 additions and 49 deletions

View File

@ -5,7 +5,7 @@ import Data.Char (ord)
import Debug.Trace;
data TokenType = Quotes | Dot | Comma | Colon | EndStatement | EOF | QString | Numeric | Literal | Digit | Assignment | OpenParen | CloseParen | OpenCurved | CloseCurved | OpenSquared | CloseSquared | Arithmetic | Comparison | Bitwise | Empty deriving (Show, Eq)
data TokenType = Quotes | Dot | Comma | Colon | EndStatement | Numeric | Literal | Assignment | OpenParen | CloseParen | OpenCurved | CloseCurved | OpenSquared | CloseSquared | Arithmetic | Comparison | Bitwise | Empty | SoftComp | CompositeAssign deriving (Show, Eq)
data Token = Token {value :: [Char], token_type :: TokenType} deriving (Show)
-- makes token from single char
@ -61,6 +61,9 @@ reducerGuard t i
reducerItself :: [Token] -> Int -> [Token]
reducerItself t i
| h == Literal && ( g == Literal || g == Numeric ) = (Token ((value e)++(value o)) Literal):(reducerGuard t (i+2))
| h == Numeric && (g == Numeric || g == Dot) = (Token ((value e)++(value o)) Numeric):(reducerGuard t (i+2))
| h == Comparison && g == Assignment = (Token ((value e)++(value o)) SoftComp):(reducerGuard t (i+2))
| (h == Arithmetic || h == Bitwise) && g == Assignment = (Token ((value e)++(value o)) CompositeAssign):(reducerGuard t (i+2))
| otherwise = e:(reducerGuard t (succ i))
where e = t !! i
o = t !! (succ i)
@ -78,6 +81,9 @@ hasGuard t i = if length t <= (succ i) then False else hasItself t i
hasItself :: [Token] -> Int -> Bool
hasItself t i
| h == Literal && ( g == Literal || g == Numeric ) = True
| h == Numeric && ( g == Numeric || g == Dot ) = True
| h == Comparison && g == Assignment = True
| (h == Arithmetic || h == Bitwise) && g == Assignment = True
| otherwise = hasGuard t (succ i)
where e = t !! i
o = t !! (succ i)

View File

@ -1,50 +1,4 @@
module Parser (parseIntoTree, TreeNode, _extractExpression) where
module Parser 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

View File

@ -3,6 +3,9 @@ package {
import flash.display.Sprite;
public class TextHello extends Sprite {
var l: int = 0;
var l: int = 10.5;
if( l >= 4.0 ) {
l += 3;
}
}
}