Pergunta

I want to create own programming language on JVM

for first step I try to write very simple statement (Adding e.g 3+7)

so I Created a lexer and a parser with Antlr

grammar gr;
formula : Digit Add Digit Equal;
Digit   
  :  '0'..'9'  
  ;  
Add     : '+';
Equal   : '=';    
WS : [\t\r\n]+ -> skip ;

then

Antlr generated Lexer,Parser and Listener

I used them

import java.io.IOException;
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;

public class gr {
    public static void main(String[] args) throws IOException {

        String str = "7+8=";        
        ANTLRInputStream input = new ANTLRInputStream(str);     
        grLexer lexer = new grLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        grParser parser = new grParser(tokens);
        ParseTree tree = parser.formula();
        System.out.println(tree.toStringTree(parser));  
    }
}

Result :

(formula 7 + 8 =)

now my problem is Here !!!

How I can execute this line (7 + 8 =) on JVM ??? (get Result 15)

seems I must use ASM library ? Yes ???

If your answer is yes please help me for very simple sample

please guide me how use JVM for create my own language

Foi útil?

Solução

From your text I assume that you are not very familiar with how the CPU executes statements, which is the basic knowledge necessary to write your own programming language.

To get that you should first spend some time with a real assembler language, which you can do online or offline. I strongly recommend to read some tutorials on this as this is anything but trivial. Learning Assembler will give you a much better understanding what the CPU actually does when it has to execute some code.

You don't have to become an Assembler expert, but once you have some understanding of it, it becomes suddenly very clear what to do to get the JVM - essentially a CPU emulator - to execute your code: you need to compile it into a form that the JVM can execute, then simply tell it to do that.

Edit: Seems I guessed wrong. ;)

Java bytecode instruction listings: Wikipedia
Devailed VM spec: Oracle

Outras dicas

Have a look at this other question dealing with emitting and running Java bytecode.

How to emit and execute Java bytecode at runtime?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top