From 819885b6c191c8d89c2c23c80429d8a2aba19185 Mon Sep 17 00:00:00 2001 From: alterdekim Date: Mon, 4 Nov 2024 05:08:51 +0300 Subject: [PATCH] Improving Lexer, starting Parser modified: app/Lexer.hs modified: app/Parser.hs modified: as/test.as --- app/Lexer.hs | 8 +++++++- app/Parser.hs | 48 +----------------------------------------------- as/test.as | 5 ++++- 3 files changed, 12 insertions(+), 49 deletions(-) diff --git a/app/Lexer.hs b/app/Lexer.hs index 8061458..8ed6521 100644 --- a/app/Lexer.hs +++ b/app/Lexer.hs @@ -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) diff --git a/app/Parser.hs b/app/Parser.hs index 74102ac..d359341 100644 --- a/app/Parser.hs +++ b/app/Parser.hs @@ -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 \ No newline at end of file diff --git a/as/test.as b/as/test.as index 0651d4e..ef4120b 100644 --- a/as/test.as +++ b/as/test.as @@ -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; + } } } \ No newline at end of file