anScript

anScript advanced

Example I: pass math expression to instruction

Instruction graph draws graph of passed expression.

instructionCreate(graph,exp)
    variableCreateLocal(x,0.i)
    variableCreateLocal(y,0.i)
    logicLoopF(x,500.i)
        mathExpression(y,exp)
        graphicWindowSetPoint(x,y,100.i,100.i,100.i)
    }
    variableDeleteLocal(x,y)
}
graphicWindowCreate(500.i,500.i,Graph.s)
graph(x)
graph(2.i*x)
graph((250.i-x)%50.i+50.i)

Example II: create variable with name from user input

Operator [var.s key] is used (reference to variable named as key’s value). In fact key is something like pointer to variable.

variableCreate(input,var.s)
input.consoleScan()
variableCreate([input],user<s>created<s>variable.s)
[input].consolePrint()
/To see what happened:
debugVariables() 

Example III: how instructionExecute works

instructionExecute is wrapper for calling instructions. When instruction (e.g. ins(a,b) or equivalent a.ins(b)) isn’t built-in instruction, instructionExecute is called (instructionExecute(ins,a,b) for both situations). You can also force instructionExecute to call built-in instruction (eg. instructionExecute(consolePrint,hello.s)) Try following code and see it yourself:

instructionCreate(myPrint,text)
    consolePrint(text)
}
/debugInstructions to see what happened:
debugInstructions()
consolePrint(a.s,b.s)
a.s.consolePrint(b.s)
myPrint(c.s)
instructionExecute(consolePrint,a.s,b.s)
/Even the following is correct!
consolePrint.instructionExecute(a.s,b.s)

instructionExecute has following logic:

Related to instructionExecute:

Example IV: instruction creating instructions concept

It makes creating instructions much easier.

instructionCreate(createNewPrint,insName,text)
    /Create variable named as insName
    variableCreate(insName,text)
    instructionCreate(insName)
        /Access variable named as last instruction called by instructionExecute
        [lastInstruction]. consolePrint()
        consoleNewLine()
    }
}
createNewPrint(printHello,hello.s)
createNewPrint(printGoodbye,goodbye.s)
printHello()
printGoodbye()

Example V: create fallback for instruction

This example shows how to create fallback for instruction (user might not have necessary dependencies).

<include(library/myIns.ans)>
instructionCreate(fallbackForMyIns)
    debugError(Please<s>install<s>myIns.ans.s)
}
variableCreate(myIns,fallbackForMyIns.s)
/...
/..
/If myIns instuction doesn't exist call fallbackForMyIns
myIns(1.i,abbccc.s)

Example VI: instructions with variable number of arguments

Many of built-in instructions have variable number of arguments. Here’s example showig how to achieve such effect:

instructionCreate(printFromName,name)
    [name].consolePrint()
}
instructionCreate(mySuperPrint)
    /We need to save lastArguments to temporary variable
    /because it would be overwritten by printFromName.
    variableCreateLocal(t,lastArguments)
    t.arrayForEach(printFromName)
    variableDeleteLocal(t)
}
mySuperPrint(a.s,1.i,b.s)

Example VII: anScript magic

<include(library/arrayPushBack.ans)>
variableCreate(a,var[])
a.arrayPushBack(abbbs.s)
[a.s][0.i].[consolePrint.s.s]()
[123123123.i.s].[consolePrint.s.s]()