mirror of
https://github.com/iritmaximus/ttk91-compiler.git
synced 2026-10-12 19:13:40 +03:00
implemented part of parser
This commit is contained in:
@@ -0,0 +1,3 @@
|
|||||||
|
*.hi
|
||||||
|
*.o
|
||||||
|
Main
|
||||||
+10
-2
@@ -1,6 +1,14 @@
|
|||||||
module Main (main) where
|
module Main (main) where
|
||||||
|
|
||||||
import Lib
|
import System.Environment
|
||||||
|
import Parser
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = someFunc
|
main = do args <- getArgs
|
||||||
|
putStrLn . readExpr $ args !! 0
|
||||||
|
|
||||||
|
readExpr :: String -> String
|
||||||
|
readExpr str =
|
||||||
|
case parseToken str of
|
||||||
|
Left err -> "No match: " ++ show err
|
||||||
|
Right val -> show val
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
module Parser (
|
||||||
|
parseToken,
|
||||||
|
parseRegister,
|
||||||
|
parseInstruction,
|
||||||
|
parseOperand,
|
||||||
|
Token ) where
|
||||||
|
|
||||||
|
import Data.Char
|
||||||
|
import Data.List.Split
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
parseToken :: String -> Either String Token
|
||||||
|
parseToken str = case parseOperand str of
|
||||||
|
Just token -> Right token
|
||||||
|
Nothing -> Left "No token found"
|
||||||
|
|
||||||
|
parseRegister :: String -> Maybe Token
|
||||||
|
parseRegister (x:xs)
|
||||||
|
| x == 'R' && not (null xs) && all isDigit xs = Just $ Register (read xs)
|
||||||
|
| otherwise = Nothing
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
dataInsructions = ["LOAD", "STORE", "IN", "OUT"]
|
||||||
|
arithmeticInstructions = ["ADD", "SUB", "MUL", "DIV", "MOD"]
|
||||||
|
logicInstructions = ["AND", "OR", "XOR", "NOT", "SHL", "SHR", "SHRA", "COMP"]
|
||||||
|
branchingInstructions = ["JUMP", "JNEG", "JZER", "JPOS", "JNNEG", "JNZER", "JNPOS", "JLES", "JEQU", "JGRE", "JNLES", "JNEQU", "JNGRE"]
|
||||||
|
stackInstructions = ["PUSH", "POP", "PUSHR", "POPR"]
|
||||||
|
subroutineInstructions = ["CALL", "EXIT"]
|
||||||
|
systemcallInsructions = ["SVC"]
|
||||||
|
compilerInsructions = ["EQU", "DC", "DS", "DEF"]
|
||||||
|
otherInstructions = ["NOP"]
|
||||||
|
|
||||||
|
allInsructions = dataInsructions ++ arithmeticInstructions ++ logicInstructions ++ branchingInstructions ++ stackInstructions ++ subroutineInstructions ++ systemcallInsructions ++ compilerInsructions ++ otherInstructions
|
||||||
|
|
||||||
|
parseInstruction :: String -> Maybe Token
|
||||||
|
parseInstruction str =
|
||||||
|
case str `elem` allInsructions of
|
||||||
|
True -> Just $ Instruction str
|
||||||
|
False -> Nothing
|
||||||
|
|
||||||
|
|
||||||
|
variables = [Variable "X", Variable "Y"]
|
||||||
|
|
||||||
|
parseOperand :: String -> Maybe Token
|
||||||
|
parseOperand str = Just $ Operand str immediateOperand indirectOperand indexed
|
||||||
|
where
|
||||||
|
immediateOperand = '=' `elem` str
|
||||||
|
indirectOperand = '@' `elem` str
|
||||||
|
value = if immediateOperand || indirectOperand
|
||||||
|
then tail str
|
||||||
|
else str
|
||||||
|
|
||||||
|
indexed = '(' `elem` value && ')' `elem` value
|
||||||
|
registers = if indexed
|
||||||
|
then endBy "(" $ init value
|
||||||
|
else [str]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
data Token = Register Int
|
||||||
|
| Instruction String
|
||||||
|
| Comment String
|
||||||
|
| Operand String Bool Bool Bool
|
||||||
|
| MemoryAddress String
|
||||||
|
| Value Integer
|
||||||
|
| Variable String
|
||||||
|
| Error String
|
||||||
|
deriving (Show, Eq)
|
||||||
@@ -1,3 +0,0 @@
|
|||||||
module Register
|
|
||||||
where
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user