1

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

HamedFathi
  • 3,667
  • 5
  • 32
  • 72
  • If you don't know much about languages, you should start with an interpreter. Don't try to make the language work on the JVM - just try to make it work at all. – user253751 Feb 09 '14 at 10:54
  • I do not hurry I'm trying to slowly acquire necessary information But It is very important to run on the JVM, I need it for university project – HamedFathi Feb 09 '14 at 11:01

2 Answers2

0

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

TwoThe
  • 13,879
  • 6
  • 30
  • 54
  • Ok you right but I know assembly (I am master student in computer engineering) I dont know how convert Antlr sources to java Bytecode, I need a simple sample for starting but thank you – HamedFathi Feb 09 '14 at 11:32
  • I added some links for the specification – TwoThe Feb 09 '14 at 14:33
0

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

How to emit and execute Java bytecode at runtime?

Community
  • 1
  • 1
MrCodeMnky
  • 835
  • 1
  • 6
  • 18