Prompt for AI

 

SimpleMath Script

SimpleMath is a programming script with a syntax similar to JavaScript, but extremely simplified to support only number and boolean types. SimpleMath is to be used only for Function Calculator. Here are the key features and rules:

Variables

  1. Declare variables using the 'var' keyword:
    var a; var b = 3;
  2. Assign values to variables:
    a = b; a = 3 + b;
  3. Variables follow strict scope rules similar to java.
  4. Variable should be declared first using 'var' keyword. Without decalaration it can not be used.

Data Types

  • Only two data types: number and boolean
  • Types are inferred, not specified
  • Number literals: Any numeric value
  • Boolean literals: true or false
  • Collection data structures such as array or list are not supported. Only single data type number and boolean are supported.
  • SimpleMath does not have "null" or "undefined" unlike javascript.
  • When null or undefined value needs to be returned in a function you can return very unlikely number such as -515151.515151 as a kind of pseudo null.

Operators

SimpleMath supports following operators:
+, -, *, /, ^ (exponentiation),
= (assignment), ==, !=, <, >, <=, >= || (logical OR), && (logical AND)
SimpleMath does not support following operators:
++, --, +=, *=,
/=, ^=, -=, !(when used alone)

The precedence and associativity rules between operators
are the same as in Java.

Control Statements

All control statements must use curly braces {} for their body:
ex) if(1<2){return 1;} -> OK
ex) if(1<2) return 1; -> Syntax Error

Followings are supported control statememts.
  • if, if else, else
  • while
  • for
  • switch case
  • do while
"break", "continue" and "return" statements are supported.


Functions

Define functions using the 'def' keyword. Three ways to define functions:
  1. Single-line definition:
    def functionName(params) = expression;
  2. Simple recursion with base case:
    def functionName(params) = recursiveExpression; functionName(baseCase) = baseValue;
  3. Multi-line function with logic:
    def functionName(params) { var result; // function body with logic return result; }
  4. Only global scope function definition is allowed. So function definition inside function not allowed.

Naming Conventions

  • Identifiers can use any language letters
  • Numbers in identifiers must come after letters
  • Underscores are not allowed in identifiers
Valid: bigBoy123
Invalid: big_boy, big1boy

Code Termination

Every code line should end with a semicolon (;).

SimpleMath provides math functions such as sin, cos, tan, toRadians, toDegrees, abs, floor, ceil, round, log(a,b), log10, ln(x) for log(e,x), sqrt, cbrt etc. 
But SimpleMath does not have type integer. Only number type. So when integer value is necessary make sure that floor or ceil or round should be applied to the value. Otherwise infinite loop or endless recursion might happen.

Comment

No comment is allowed. So no "//","#","/**/" comment syntax allowed.

System providing functions

  • sin(x)
  • cos(x)
  • tan(x)
  • asin(x)
  • acos(x)
  • atan(x)
  • sinh(x)
  • cosh(x)
  • tanh(x)
  • ln(x)
  • log(x,y)
  • log10(x)
  • floor(x)
  • round(x)
  • ceil(x)
  • abs(x)
  • pow(x,y)
  • sqrt(x)
  • qbrt(x)
  • toRadians(x)
  • toDegrees(x)

System providing constant

  • pi = 3.14159...
  • e = 2.71828...
  warning:  system providing constant name e and pi can not be used as a variable or parameter name.

Any number is number type

SimpleMath has no specific integer type. Any number is treated as just number type. So in some situation programmer should use integer converting functions such as "floor", "round", or "ceil" to prevent logic error.

SimpleMath code examples

def catalan(n){
  if(n<=1){
    return 1;
  }
  var result=0;
  for(var i=0;i<n;i=i+1){
    var a=catalan(i);
    var b=catalan(n−i−1);
    result=result+a*b;
  }
  return result;
}

def fibonacci(n)=fibonacci(n−1)+fibonacci(n−2);
fibonacci(1)=1;
fibonacci(2)=1;

def 리만합(a,b,c,start,end,n){
  var Sn=0;
  var delta=(end−start)/n;
  var k=1;
  while(k<=n){
    var x=start+delta*(k−0.5);
    var h=a*x*x+b*x+c;
    var s=delta*abs(h);
    Sn=Sn+s;
    k=k+1;
  }
  return Sn;
}

def factorial(n){
  if(n<=0){
    return 1;
  }
  return n*factorial(n−1);
}

def isPrime(n){
  if(n<=1){
    return false;
  }
  var i=2;
  while(i*i<=n){
    if(n%i==0){
      return false;
    }
    i=i+1;
  }
  return true;
}


def nthPrime(n){
  var count=0;
  var num=2;
  while(count<n){
    if(isPrime(num)){
      count=count+1;
    }
    if(count==n){
      return num;
    }
    num=num+1;
  }
  return num−1;
}

def monthlyPayment(loan, rate, years)
 =(rate*loan)/(1-(1+rate)^-(12*years));

Invalid code examples

var a = "hello";  (String type not supported)

var k = [1,2,3];   ( Collection data structure not supported)

k[0]=2; (list or array type not supported)

a += 3; ('+=' operator not supported)

var test12test=4; (number should come at the end of identifier)

if(1<2) return 1; (statement body should be enclosed in curly braces)

Comments

Popular posts from this blog

How Deep Is the Well?

Function Examples