Smalltalk-可以用Smalltalk编写Java程序吗?

Smalltalk-可以用Smalltalk编写Java程序吗?,java,interpreter,smalltalk,executor,Java,Interpreter,Smalltalk,Executor,我正在尝试将我为编译器编写的Java解释器转换为Smalltalk解释器。可能吗?如果是的话,什么是一个很好的资源?我对Smalltalk完全陌生,现在看起来很奇怪。谢谢你的帮助。关于该程序的更多信息:输入将是一个中间代码文件,其示例如下 计算数字阶乘的中间代码文件: read store x push x store i push 1.0 store fact push i push 1.0 greater testfgoto 21 push fact push i multiply

我正在尝试将我为编译器编写的Java解释器转换为Smalltalk解释器。可能吗?如果是的话,什么是一个很好的资源?我对Smalltalk完全陌生,现在看起来很奇怪。谢谢你的帮助。关于该程序的更多信息:输入将是一个中间代码文件,其示例如下

计算数字阶乘的中间代码文件:

read
store x
push x
store i
push   1.0
store fact
push i
push   1.0
greater
testfgoto 21
push fact
push i
multiply
store fact
push i
push   1.0
minus
store i
push 1.0
testtgoto 7
push fact
print
end
JavaExecutor.java程序:

public void executor(){
    String operation = null;
    StringTokenizer tokens = null;
    String key,value = null;
    Double tempVar = null;
    // Traverse the Arraylist to get the operations
    for(int i=0; i < operations.size(); i++){
        operation = (String)operations.get(i);
        System.out.println("Operation -------"+ operation);
        tokens = new StringTokenizer(operation);
        while(tokens.hasMoreTokens()){
            key = tokens.nextToken();
            //System.out.println("KEY "+ key);
            if(key.toUpperCase().equals("PUSH")){
                //System.out.println("Push Operation");
                value = (String)tokens.nextToken();
                //System.out.println("value "+ value);
                if(checkforVaribaleName(value)){
                    stack.push((Double)assignments.get(value));
                    System.out.println("PUSH"+ (Double)assignments.get(value));
                }
                else if(value != null){
                    stack.push(Double.parseDouble(value));
                    System.out.println("PUSH" + value);
                } else{
                    stack.push(0.0);
                    System.out.println("PUSH" + 0.0);
                }
                break;

            }else if(key.toUpperCase().equals("POP")){
                //NO LOGIC
                break;

            }else if(key.toUpperCase().equals("READ")){
                //System.out.println("Read Operation");
                tempVar = readDoubleFromConsole();
                stack.push(tempVar);
                System.out.println("PUSHP :"+tempVar);
                break;

            }else if(key.toUpperCase().equals("WRITE")){
                break;
            }else if(key.toUpperCase().equals("MULTIPLY")){
                //System.out.println("MULTIPLY operation");
                Double first = (Double)stack.pop();
                Double second = (Double)stack.pop();
                stack.push(first*second);
                System.out.println("PUSH "+ first*second);
                break;

            }else if(key.toUpperCase().equals("DIVIDE")){
                Double first = (Double)stack.pop();
                Double second = (Double)stack.pop();
                stack.push(second/first);
                break;
            }else if(key.toUpperCase().equals("PLUS")){
                Double first = (Double)stack.pop();
                Double second = (Double)stack.pop();
                stack.push(second+first);
                break;
            }else if(key.toUpperCase().equals("MINUS")){
                Double first = (Double)stack.pop();
                Double second = (Double)stack.pop();
                stack.push(second-first);
                System.out.println("PUSH "+ (second-first));
                break;
            }else if(key.toUpperCase().equals("GREATER")){
                //System.out.println("GREATER operation");
                Double first = (Double)stack.pop();
                System.out.println("POP :"+first);
                Double second = (Double)stack.pop();
                System.out.println("POP "+ second);
                if(second>first){
                    stack.push(1.0);
                }
                else{
                    stack.push(0.0);
                }
                break;
            }else if(key.toUpperCase().equals("LESS")){
                Double first = (Double)stack.pop();
                Double second = (Double)stack.pop();
                if(second<first){
                    stack.push(1.0);
                }
                else{
                    stack.push(0.0);
                }
                break;
            }else if(key.toUpperCase().equals("EQUAL")){
                Double first = (Double)stack.pop();
                Double second = (Double)stack.pop();
                if(second==first){
                    stack.push(1.0);
                }
                else{
                    stack.push(0.0);
                }
                break;
            }else if(key.toUpperCase().equals("STORE")){
                //System.out.println("Store operation");
                value = (String)tokens.nextToken();

                assignments.put(value, (Double)stack.pop());
                System.out.println("POP :"+assignments.get(value));
                break;
            }else if(key.toUpperCase().equals("TESTFGOTO")){
                value = (String)tokens.nextToken();
                if((Double)stack.pop() == 0.0){
                    System.out.println("POP " +0.0);
                    i = Integer.parseInt(value)-2;
                }
                break;
            }else if(key.toUpperCase().equals("TESTTGOTO")){
                value = (String)tokens.nextToken();
                if((Double)stack.pop() == 1.0){
                    System.out.println("POP " +1.0);
                    i = (Integer.parseInt(value)-2);
                    System.out.println(i);
                }
                break;
            }else if(key.toUpperCase().equals("PRINT")){
                //System.out.println("PRINT operation");
                System.out.println("Result "+stack.pop());
                break;
            }else if(key.toUpperCase().equals("END")){
                System.out.println("Execution Completed");
                System.exit(0);
            }

        }
    }
}


public static void main(String args[])
{
    String fileName = null;
    if(args.length > 0)
    {
        fileName = args[0];
    }
    else{
        System.out.println("Usage : JavaExecutor fileName.inp");
        System.exit(0);
    }
    JavaExecutor j = new JavaExecutor(fileName);
    j.readFromFile();
    j.executor();


}

}

从理论上讲,编程语言之间的自动翻译是可能的,只在少数情况下可行,在其中很少情况下有效。即使是虚拟机字节码的自动翻译通常也不高效,JVM相当于Java专用的;向Smalltalk字节码转换或从Smalltalk字节码转换可能不太顺利。

在Smalltalk中为中间代码实现解释器应该一点也不难,但您至少需要了解Smalltalk的基本知识:集合类、流类,以及让自己适应IDE


下面是VisualWorks Smalltalk版本的解释器:

'From VisualWorks, 7.8 of March 30, 2011 on May 1, 2012 at 9:47:31 PM'!


CodeComponent create: #package named: 'IntermediateInterpreter'!"Package IntermediateInterpreter*"!


CodeComponent create: #package named: 'IntermediateInterpreter'!

Smalltalk defineClass: #IntermediateInterpreter
    superclass: #{Core.Object}
    indexedType: #none
    private: false
    instanceVariableNames: 'lines currentLineNumber variables stack commands '
    classInstanceVariableNames: ''
    imports: ''
    category: ''!

!IntermediateInterpreter class methodsFor: 'instance creation'!

new
    ^super new initialize
! !


!IntermediateInterpreter methodsFor: 'initialize-release'!

initialize

    stack := OrderedCollection new.
    variables := Dictionary new.
    lines := OrderedCollection new.
    self initializeCommands
!

initializeCommands

    commands := Dictionary new.
    commands
        at: 'READ' put: [:values | stack add: (self tokenize: (Dialog request: 'value?' initialAnswer: ''))];
        at: 'STORE' put: [:values | variables at: values second put: stack removeLast];
        at: 'PUSH' put: [:values | stack add: ((self isVariableName: values second) ifTrue: [variables at: values second] ifFalse: [values second])];
        at: 'GREATER' put: [:values | | a b | b := stack removeLast. a := stack removeLast. stack add: (a > b ifTrue: [1.0] ifFalse: [0.0])];
        at: 'MULTIPLY' put: [:values | | a b | b := stack removeLast. a := stack removeLast. stack add: a * b ];
        at: 'MINUS' put: [:values | | a b | b := stack removeLast. a := stack removeLast. stack add: a - b ];
        at: 'TESTFGOTO' put: [:values | (stack removeLast - 0.0) abs < 0.01 ifTrue: [currentLineNumber := values second]];
        at: 'TESTTGOTO' put: [:values | (stack removeLast - 1.0) abs < 0.01 ifTrue: [currentLineNumber := values second]];
        at: 'PRINT' put: [:values | Transcript show: stack removeLast printString; cr];
        at: 'END' put: [:values | currentLineNumber := 0]! !

!IntermediateInterpreter methodsFor: 'testing'!

isVariableName: aString

    (aString isKindOf: String) ifFalse: [^false].
    ^aString first isAlphabetic! !

!IntermediateInterpreter methodsFor: 'interpreting'!

interpret: aString

    self createLinesFrom: aString.
    currentLineNumber := 1.
    self interpret!

interpret

    | tokens |
    [currentLineNumber = 0] whileFalse: 
            [tokens := lines at: currentLineNumber.
            Transcript show: tokens printString; cr.
            currentLineNumber := currentLineNumber + 1.
            (commands at: tokens first asUppercase
                ifAbsent: [self error: 'Unknown command']) value: tokens]!

tokenizeLine: aString

    ^(((aString tokensBasedOn: Character space)
        reject: [:each | each isEmpty]) collect: [:each | self tokenize: each]) asArray!

tokenizeCurrentLine

    ^(((lines at: currentLineNumber) tokensBasedOn: Character space)
        reject: [:each | each isEmpty]) collect: [:each | self tokenize: each]!

createLinesFrom: aString

    | stream |
    stream := aString readStream.
    [stream atEnd] whileFalse: [lines add: (self tokenizeLine: (stream upTo: Character cr))]!

tokenize: each

    ^(self isVariableName: each)
        ifTrue: [each]
        ifFalse: [Number readFrom: each readStream]! !

你的问题已经表明你可能想从更简单的事情开始。。。smalltalk是一种面向对象的语言。哦,我为我缺乏知识而道歉,我把动态和函数混淆了,并做了更正thanks@Yaba大卫·辛伯伦把密码给了你。请接受答案或询问更多信息。那么,对于散列图、文件读取器和写入器之类的东西,是否会有smalltalk替代方案呢;这更多的是一个库的问题,这给翻译增加了更多的复杂性,因为相应的Smalltalk和Java库并不总是提供兼容的接口。很抱歉,我刚刚开始学习Smalltalk语法,很难理解Smalltalk语法。如果我能找到类似的东西,你能给我一些建议吗,我用objective-c/c编写了编译器,并使用flex和bison生成了中间代码。Java executor工作得很好,但是smalltalk…\n我想在这里我建议您参考第一条全局注释。在有效使用环境之前,您需要先了解环境。Dave,为什么不使用isVariableName:aString aString isKindOf:String和:[aString first isAlphabetic]?当然。使用和似乎阅读效果更好。好建议。