Use text for member access instead of Expr

This commit is contained in:
2025-09-02 10:22:55 -04:00
parent d5c7e2826f
commit 1c5cadd263
3 changed files with 11 additions and 5 deletions

View File

@@ -60,7 +60,7 @@ data Expr
| UnOp UnOp Expr | UnOp UnOp Expr
| Call Expr [Expr] | Call Expr [Expr]
| Index Expr Expr | Index Expr Expr
| Member Expr Expr | Member Expr Text
| Cast Type Expr | Cast Type Expr
| Sizeof Type | Sizeof Type
| StructInit Text [(Text, Expr)] | StructInit Text [(Text, Expr)]

View File

@@ -108,7 +108,7 @@ codegenLVal (Id name) = do
Nothing -> error $ "Variable " ++ show name ++ " not found" Nothing -> error $ "Variable " ++ show name ++ " not found"
-- TODO support members of members -- TODO support members of members
codegenLVal (Member (Id sName) (Id field)) = do codegenLVal (Member (Id sName) field) = do
ctx <- get ctx <- get
case lookup sName (operands ctx) of case lookup sName (operands ctx) of
Just ((Just (StructType op_type)), struct) -> do Just ((Just (StructType op_type)), struct) -> do
@@ -202,7 +202,7 @@ codegenExpr (Call (Id f) args) = do
codegenExpr (Index arr idx) = undefined -- TODO arrays codegenExpr (Index arr idx) = undefined -- TODO arrays
-- Get the address of the struct field and load it -- Get the address of the struct field and load it
codegenExpr (Member (Id sVarName) (Id field)) = do codegenExpr (Member (Id sVarName) field) = do
ctx <- get ctx <- get
case lookup sVarName (operands ctx) of case lookup sVarName (operands ctx) of
Just ((Just (StructType op_type)), struct) -> do Just ((Just (StructType op_type)), struct) -> do

View File

@@ -9,8 +9,14 @@ import Windows12.Lexer
opTable :: [[Operator Parser Expr]] opTable :: [[Operator Parser Expr]]
opTable = opTable =
[ [ InfixL $ Member <$ symbol ".", [ [ Postfix $ do
InfixL $ (\l r -> Member (UnOp Deref l) r) <$ symbol "->" _ <- symbol "."
field <- identifier
pure (\expr -> Member expr field),
Postfix $ do
_ <- symbol "->"
field <- identifier
pure (\expr -> Member (UnOp Deref expr) field)
], ],
[ unary (UnOp Neg) "-", [ unary (UnOp Neg) "-",
unary (UnOp Not) "!", unary (UnOp Not) "!",