Finishing work on Lexer. 'Token reducer' was successfuly rewritten. Added Debug.Trace for logging.

modified:   app/Lexer.hs
	modified:   as/test.as
This commit is contained in:
Michael Wain 2024-11-03 02:45:11 +03:00
parent 98015b33d6
commit 2cee932739
2 changed files with 26 additions and 25 deletions

View File

@ -3,6 +3,8 @@ module Lexer (tokenize, Token (..), TokenType (..) ) where
import Data.List import Data.List
import Data.Char (ord) 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 | EOF | QString | Numeric | Literal | Digit | Assignment | OpenParen | CloseParen | OpenCurved | CloseCurved | OpenSquared | CloseSquared | Arithmetic | Comparison | Bitwise | Empty deriving (Show, Eq)
data Token = Token {value :: [Char], token_type :: TokenType} deriving (Show) data Token = Token {value :: [Char], token_type :: TokenType} deriving (Show)
@ -34,7 +36,7 @@ makeTokenFromEveryChar code = map (\c -> parseSingleToken c) code
-- entry point, which should be called to start lexer. -- entry point, which should be called to start lexer.
tokenize :: [Char] -> [Token] tokenize :: [Char] -> [Token]
tokenize sourceCode = excludeEmpty (checkFor (reduceTokens $ makeTokenFromEveryChar sourceCode)) tokenize sourceCode = excludeEmpty (checkFor (makeTokenFromEveryChar sourceCode))
excludeEmpty :: [Token] -> [Token] excludeEmpty :: [Token] -> [Token]
excludeEmpty t = filter (\c -> (token_type c) /= Empty) t excludeEmpty t = filter (\c -> (token_type c) /= Empty) t
@ -47,36 +49,41 @@ getByMod t n = filter (\c -> c `mod` 2 == n) [0..((length t)-1)]
getTokensByMod :: [Token] -> Int -> [Token] getTokensByMod :: [Token] -> Int -> [Token]
getTokensByMod t n = map (\c -> t !! c ) (getByMod t n) getTokensByMod t n = map (\c -> t !! c ) (getByMod t n)
reducerGuard :: [Token] -> [Token] -> [Token] reducerGuard :: [Token] -> Int -> [Token]
reducerGuard et ot = if length et == 0 || length ot == 0 then et++ot else reducerItself et ot --reducerGuard t i = if (length t) <= (trace ("Lol: " ++ (show (length t)) ++ " <= " ++ (show (i+1))) (i+1)) then [] else (reducerItself t i)
reducerGuard t i
| l <= i = []
| l <= succ i = (last t):[]
| otherwise = reducerItself t i
where l = length t
-- reducer itself -- reducer itself
reducerItself :: [Token] -> [Token] -> [Token] reducerItself :: [Token] -> Int -> [Token]
reducerItself et ot reducerItself t i
| h == Literal && ( g == Literal || g == Numeric ) = (Token ((value e)++(value o)) Literal):(reducerGuard (tail et) (tail ot)) | h == Literal && ( g == Literal || g == Numeric ) = (Token ((value e)++(value o)) Literal):(reducerGuard t (i+2))
| otherwise = [e,o]++(reducerGuard (tail et) (tail ot)) | otherwise = e:(reducerGuard t (succ i))
where e = head et where e = t !! i
o = head ot o = t !! (succ i)
h = token_type e h = token_type e
g = token_type o g = token_type o
-- method, which used for reducing token amout (actually to specify some tokens e.g. ! = -> !=) -- method, which used for reducing token amout (actually to specify some tokens e.g. ! = -> !=)
reduceTokens :: [Token] -> [Token] reduceTokens :: [Token] -> [Token]
reduceTokens t = reducerItself (getTokensByMod t 0) (getTokensByMod t 1) reduceTokens t = reducerItself t 0
hasGuard :: [Token] -> [Token] -> Bool hasGuard :: [Token] -> Int -> Bool
hasGuard et ot = if length et == 0 || length ot == 0 then False else hasItself et ot hasGuard t i = if length t <= (succ i) then False else hasItself t i
-- method that help checks -- method that help checks
hasItself :: [Token] -> [Token] -> Bool hasItself :: [Token] -> Int -> Bool
hasItself et ot hasItself t i
| h == Literal && ( g == Literal || g == Numeric ) = True | h == Literal && ( g == Literal || g == Numeric ) = True
| otherwise = hasGuard (tail et) (tail ot) | otherwise = hasGuard t (succ i)
where e = head et where e = t !! i
o = head ot o = t !! (succ i)
h = token_type e h = token_type e
g = token_type o g = token_type o
-- method that checks if there are equal Literals and Numerics -- method that checks if there are equal Literals and Numerics
checkFor :: [Token] -> [Token] checkFor :: [Token] -> [Token]
checkFor t = if hasItself (getTokensByMod t 0) (getTokensByMod t 1) then checkFor (reduceTokens t) else t checkFor t = if hasItself t 0 then checkFor (reduceTokens t) else t

View File

@ -3,12 +3,6 @@ package {
import flash.display.Sprite; import flash.display.Sprite;
public class TextHello extends Sprite { public class TextHello extends Sprite {
public function TextHello() { var l: int = 0;
var tf:TextField = new TextField();
tf.text = "Hello World!"
tf.x = 50;
tf.y = 40;
addChild(tf);
}
} }
} }