Post Reply 
monic part 6: the mC programming language and compiler
03-18-2022, 04:59 AM (This post was last modified: 05-29-2022 06:08 AM by F-73P.)
Post: #8
RE: monic part 6: the mC programming language and compiler
I've completed the first stage of the compiler for the grammar in post #4. It's a stripped down version of C with support for:
  • global variables only (no arrays)
  • subroutines (i.e. functions without arguments)
  • if-then-else, while, do-while and for statements
If anyone would like to try it:

1) Go to github and download the files
2) Install Keil uVision5 (MDK-ARM)
3) Create a new project "Project -> New uVision Project"
4) Select "ARM Cortex M7 -> ARMCM7", press "OK" twice
5) Click "Target 1", right-click "Source Group 1 -> Add Existing Files to Group 'Source Group 1'..."
6) Add the files downloaded in step 1

Select one of the example programs in "moniC_programs.h" or enter your own (see the example below). Then on lines 31 and 32 in "simulator.c" enter the values to be pushed onto the stack. Place a breakpoint at line 89, where you can step through the parser and virtual machine. Place another breakpoint at line 97, where you can pop the returned value off the stack to confirm the program executed correctly.

e.g. how to enter a program to find the zero of x-e^(-0.5x^2) in [a,b] with an error tolerance tol using the bisection method:

1) Type the program in Notepad++

Code:
/*bisection method*/

func()
{ return(x-exp(-0.5*x*x));
}

main()
{ read tol;
  read b;
  read a;
  x=a;
  fa=func();
  x=b;
  fb=func();
  m=(a+b)/2;
  while(b-m>=tol)
  { x=m;
    fm=func();
    if(fm*fb<0) a=m;    
    else
    { b=m;
      fb=fm;
    }
    m=(a+b)/2;
  }
  write m;
}

2) Convert the program to a single line by highlighting it and going to Edit->Line Operations->Join Lines

3) Copy the joined lines and paste them in the "char testSource[]" array in "moniC_programs.h".

Because the free version of uVision5 is code limited to 32kB, I was only able to include the transcendental functions ln, exp, sin, cos and tan from the C math library (taking the size to 29.5kB). But the actual calculator has plenty of memory so this is not an issue.
Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: monic part 6: the mC programming language and compiler - F-73P - 03-18-2022 04:59 AM



User(s) browsing this thread: 1 Guest(s)