Click here to Skip to main content
15,883,917 members
Articles / Programming Languages / C#

Building a General Purpose Interpreter, Math Engine and Parser in C#

Rate me:
Please Sign up or sign in to vote.
4.97/5 (70 votes)
13 Feb 2015GPL313 min read 84.8K   7.9K   224  
An easy to understand and use language interpreter, run time parser and calculation engine from scratch in C#.
{
/*This example shows how to fill a shape (in this case yet more spirograph curves, because we love them!!*/
removeall;: /*Warning: This can cause loss of data!*/
spirograph = function (o_plot, v_R, v_r, v_p, v_nRotations, v_thickness, s_color, s_fill)
{
    t = vectorin(0, 0.05, 2 * pi * v_nRotations);:
    x = (v_R + v_r) * cos(t) + v_p * cos((v_R + v_r) * t / v_r);:
    y = (v_R + v_r)* sin(t) + v_p * sin((v_R + v_r) * t / v_r);:
    addplot(o_plot, x, y, 0, v_thickness, s_color, 0, 0, s_fill):
}
p=createplot();:
spirograph(p, 52, -25, 36, 25, 4, "200,0,200", "80,200,120");:
showplot(p, 4, 4);:

removeall():   
spirograph = function (v_R, v_r, v_p, v_start, v_step, v_stop, o_plot, v_lineSize, s_color, v_scale, v_n, s_fill)

{
    t = vectorin(v_start, v_step, v_stop);:
    x = (v_R + v_r) * cos(t) + v_p * cos((v_R + v_r) * t / v_r);:
    y = (v_R + v_r) * sin(t) + v_p * sin((v_R + v_r) * t / v_r);:
    addplot(o_plot, x * v_scale, y * v_scale, v_n, v_lineSize, s_color, 0, 0, s_fill);:
}

colors = strings(Green, Blue, DarkOrchid, Red, Chocolate, DarkSalmon, Goldenrod, Gold);:
fills = strings(Green, Blue, DarkOrchid, Red, Chocolate, DarkSalmon, Goldenrod, Gold);:
f = array(1.5, 1.25, 1.04, 0.86, 0.71, 0.58, 0.46, 0.37);:
p = createplot();:
ring = 0;:
repeat (8)
{
    i = 0;:
    repeat(4)
    {
        j = 0;:
        repeat(8)
        {       
            spirograph(60*itemat(f, ring), -15*itemat(f, ring) , 12*itemat(f, ring) , 2*pi*(i*0.250-.055) , 0.005, 
					   2*pi*(i*0.250+.055), p, 2, stringat(colors, j),   3.0, j*(11.25)+0, stringat(fills, j));:
            j = j + 1;:
        }
        i = i + 1;:   
    }
    ring = ring + 1;:
}
showplot(p):
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


Written By
Technical Lead https://mathiversity.com
Unknown
I am a full-stack developer. My skills include JavaScript, C#/.Net, MS Azure cloud etc. I love to work on complex programming tasks requiring deep analysis, planning and use of efficient algorithms and data structures.

Comments and Discussions