Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7859555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T21:56:00+00:00 2026-06-02T21:56:00+00:00

I have been working through the tutorial at http://bkiers.blogspot.de/2011/03/creating-your-own-programming-language.html . In short, a small

  • 0

I have been working through the tutorial at http://bkiers.blogspot.de/2011/03/creating-your-own-programming-language.html. In short, a small language is developed that has a minimal set of arithmetic operations. “println” and “print” are directly mapped functions that call System.out.print().

From this I am trying to implement my own DSL by changing some properties of the language that has been developed in that article series. I want to make java.lang.Math accessible from within my DSL, but can’t think of a good way without having to map each function manually.

The language does not support imports whatsoever. I could introduce a new grammar rule to match Math.<function>(<parameters>).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T21:56:02+00:00Added an answer on June 2, 2026 at 9:56 pm

    You could do that by making Math a reserved word and make the following changes:

    grammar additions

    functionCall
      :  Identifier '(' exprList? ')'         -> ^(FUNC_CALL Identifier exprList?)
      |  Println '(' expression? ')'          -> ^(FUNC_CALL Println expression?)
      |  Print '(' expression ')'             -> ^(FUNC_CALL Print expression)
      |  Assert '(' expression ')'            -> ^(FUNC_CALL Assert expression)
      |  Size '(' expression ')'              -> ^(FUNC_CALL Size expression)
      |  Math '.' Identifier '(' exprList ')' -> ^(FUNC_CALL Math Identifier exprList) // added
      ;
    
    // ...
    
    Math : 'Math'; // added
    

    tree grammar addition

    functionCall returns [TLNode node]
      :  ^(FUNC_CALL Identifier exprList?)     {node = new FunctionCallNode($Identifier.text, $exprList.e, functions);}
      |  ^(FUNC_CALL Println expression?)      {node = new PrintlnNode($expression.node);}
      |  ^(FUNC_CALL Print expression)         {node = new PrintNode($expression.node);}
      |  ^(FUNC_CALL Assert expression)        {node = new AssertNode($expression.node);}
      |  ^(FUNC_CALL Size expression)          {node = new SizeNode($expression.node);}
      |  ^(FUNC_CALL Math Identifier exprList) {node = new MathCallNode($Identifier.text, $exprList.e);} // added
      ;
    

    new Math-node class

    package tl.tree;
    
    import tl.TLValue;
    import java.util.ArrayList;
    import java.util.List;
    
    public class MathCallNode implements TLNode {
    
        private String methodName;
        private List<TLNode> params;
    
        public MathCallNode(String nm, List<TLNode> ps) {
            methodName = nm;
            params = ps;
        }
    
        @Override
        public TLValue evaluate() {
            if(methodName.equals("sqrt")) 
                return new TLValue(Math.sqrt(params.get(0).evaluate().asDouble()));
            else if(methodName.equals("min")) 
                return new TLValue(Math.min(params.get(0).evaluate().asDouble(), params.get(1).evaluate().asDouble()));  
            // TODO implement more Math-methods
            else
                throw new RuntimeException("unknown method: Math." + methodName + "(...)");
        }
    }
    

    Now you don’t need to implement all the separate method in their own class, but will still need to check which method is invoked in the evaluate() method of MathCallNode, of course.

    If you now evaluate:

    println(Math.sqrt(9));
    println(Math.min(9, -42));
    

    the following will be printed:

    3.0
    -42.0
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working through a tutorial ( http://glacialflame.com/category/tutorial/ ) to build an Isometric
I have been working through the tutorial adapting it to a project I want
I have been working through the tutorial by Alex Young on using flash messages
I am new to asp.net and have been working through the tutorial - up
I'm starting out with wxPython and have been working my way through every tutorial
I have been working through projects involving packages that do all the web design
I have been working on Eclipse recently. I am fairly new to java programming,
I am working on a QR code encoding/decoding project. I have been read through
I have been working my way through several books on X-code, the Apple document
I am working through the Ruby-on-Rails3 Tutorial Chapter 8. I have created a form

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.