Execute Brain****/COBOL: Difference between revisions
Content deleted Content added
Added COBOL interpreter. |
Re-factored interpreter, removing subprogram and replacing with a REDEFINES. |
||
Line 3:
<lang cobol> IDENTIFICATION DIVISION.
PROGRAM-ID. Brainfuck-Interpreter.
DATA DIVISION.
LOCAL-STORAGE SECTION.
01 Nesting-Level PIC 999.
01 Array-Area.
03 Array
01 Input-Char PIC X.
* *>> Note: This limit is mostly arbitrary.
01 Max-Program-Size CONSTANT 2048.
01 Input-Program PIC X(Max-Program-Size).
01 Program-Index USAGE
PROCEDURE DIVISION.
Main.
DISPLAY "Enter program: " WITH NO ADVANCING
ACCEPT Input-Program
PERFORM Process-Statement VARYING Program-Index FROM 1 BY 1
UNTIL Max-Program-Size < Program-Index
GOBACK
.
Process-Statement.
EVALUATE Input-Program (Program-Index:1)
WHEN ">"
SET Table-Index UP BY 1
WHEN "<"
SET Table-Index DOWN BY 1
WHEN "+"
ADD 1 TO Array-Table (Table-Index)
WHEN "-"
SUBTRACT 1 FROM Array-Table (Table-Index)
WHEN "."
DISPLAY
WHEN ","
ACCEPT
▲ USING Input-Char Array-Table (Table-Index)
WHEN "["
IF Array-Table (Table-Index) = ZERO
PERFORM Jump-To-Block-End
END-IF
WHEN "]"
IF Array-Table (Table-Index) NOT = ZERO
Line 67 ⟶ 63:
END-EVALUATE
.
* *>> Move Program-Index back to position of matching '['
Jump-To-Block-Start.
Line 78 ⟶ 74:
WHEN "["
SUBTRACT 1 FROM Nesting-Level
WHEN "]"
ADD 1 TO Nesting-Level
END-EVALUATE
END-PERFORM
PERFORM Check-Mismatched-Brackets
.
* *>> Move Program-Index forward to position of matching ']'
Jump-To-Block-End.
Line 94 ⟶ 90:
AND (Nesting-Level = 0))
OR (Input-Program (Program-Index:1) = SPACE)
EVALUATE Input-Program (Program-Index:1)
WHEN "["
ADD 1 TO Nesting-Level
WHEN "]"
SUBTRACT 1 FROM Nesting-Level
END-EVALUATE
END-PERFORM
PERFORM Check-Mismatched-Brackets
.
Check-Mismatched-Brackets.
IF (Program-Index = 0)
OR (Input-Program (Program-Index:1) = SPACE)
DISPLAY "Mismatched
GOBACK
END-IF
.</lang>
|