mirror of
https://github.com/catseye/SixtyPical.git
synced 2025-01-25 08:30:07 +00:00
Check for duplicate declarations.
This commit is contained in:
parent
16e29c9dc9
commit
b15411c677
@ -148,3 +148,12 @@ Even in inner blocks.
|
||||
| }
|
||||
| }
|
||||
? undeclared location
|
||||
|
||||
No duplicate declarations.
|
||||
|
||||
| reserve word score
|
||||
| assign word score 4000
|
||||
| routine main {
|
||||
| nop
|
||||
| }
|
||||
? duplicate declaration
|
||||
|
@ -14,11 +14,12 @@ routineDeclared routName (Program _ routines) =
|
||||
where
|
||||
getRoutineName (Routine name _) = name
|
||||
|
||||
getDeclLocationName (Assign name _ _) = name
|
||||
getDeclLocationName (Reserve name _) = name
|
||||
|
||||
locationDeclared locName (Program decls _) =
|
||||
elem locName (map (getLocationName) decls)
|
||||
elem locName (map (getDeclLocationName) decls)
|
||||
where
|
||||
getLocationName (Assign name _ _) = name
|
||||
getLocationName (Reserve name _) = name
|
||||
|
||||
routineUsedLocations (Routine _ instrs) = blockUsedLocations instrs
|
||||
|
||||
@ -41,6 +42,21 @@ allRoutineLocationsDeclared program routine =
|
||||
allUsedLocationsDeclared p@(Program _ routines) =
|
||||
allTrue (map (allRoutineLocationsDeclared p) routines)
|
||||
|
||||
noDuplicateDecls p@(Program decls routines) =
|
||||
collectDecls decls []
|
||||
where
|
||||
collectDecls [] acc = True
|
||||
collectDecls (decl:decls) acc =
|
||||
if
|
||||
name `elem` acc
|
||||
then
|
||||
error ("duplicate declaration '" ++ name ++ "'")
|
||||
else
|
||||
collectDecls decls (name:acc)
|
||||
where
|
||||
name = getDeclLocationName decl
|
||||
|
||||
checkProgram program =
|
||||
trueOrDie "missing 'main' routine" (routineDeclared "main" program) &&
|
||||
trueOrDie "undeclared location" (allUsedLocationsDeclared program)
|
||||
trueOrDie "undeclared location" (allUsedLocationsDeclared program) &&
|
||||
noDuplicateDecls program
|
||||
|
Loading…
x
Reference in New Issue
Block a user