问题描述
我在C语言编写一个简单的基于堆栈的语言,不知道我应该如何去实现某种类型的循环结构,和/或超前符号。由于code是有点长此页(200线),我把它放在。
I'm writing a simple stack-based language in C and was wondering how I should go about implementing a loop structure of some kind, and/or lookahead symbols. Since the code is a bit long for this page (over 200 lines) I've put it in a GitHub repository.
编辑:主程序文件 stack.c
编辑:code只需要在输入字
,有点像等等。它采用 scanf函数
和作品从左到右。然后,它使用如果一系列中和
STRCMP
s到决定做什么。这真的了。
The code just takes in input in words
, kind of like FORTH. It uses scanf
and works left to right. Then it uses a series of if
s and strcmp
s to decide what to do. That's really it.
推荐答案
四议的方法是添加一个单独的回路堆栈旁边的数据堆栈。然后定义与此循环栈工作操作。例如:
The Forth approach is to add a separate loop stack alongside the data stack. You then define operations that work with this loop stack. For example:
5 0 DO I . LOOP
将打印
0 1 2 3 4
这工作方式是:
-
DO
到循环堆栈移动指数(0)和控制(5)。 -
I
拷贝循环堆栈的数据堆栈的顶部。 -
LOOP
递增指数(环堆栈的顶部)。如果该指数低于对照组(以下任一回路堆栈的顶部),然后重新运行从DO
命令回LOOP
。如果索引> =,然后将其弹出的索引和来自环路堆栈控制,并作为正常对照恢复。
DO
moves the index (0) and the control (5) over to the loop stack.I
copies the top of the loop stack to the data stack.LOOP
increments the index (top of loop stack). If the index is less than the control (one below the top of loop stack), then it reruns the commands fromDO
back toLOOP
. If the index is >=, then it pops the index and control from the loop stack, and control resumes as normal.
这篇关于如何实现回路用C语言编写的第四类语言间preTER的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!