Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

Post History

22%
+0 −5
Q&A Adding support on mathematical expressions

MarkFuncs has really gotten huge progress, at least in my eyes, and now, I want to make math possible in the language. Background MarkFuncs is a programming language that I have been working on f...

0 answers  ·  posted 2y ago by General Sebast1an‭  ·  edited 2y ago by Lundin‭

#2: Post edited by user avatar Lundin‭ · 2021-09-27T10:03:46Z (over 2 years ago)
Making personal tags which is used by your own code base only has no value to anyone else. If/when your library is used by thousands of people, that's another story...
#1: Initial revision by user avatar General Sebast1an‭ · 2021-09-26T08:20:06Z (over 2 years ago)
Adding support on mathematical expressions
[MarkFuncs](https://github.com/JoyrenceMatthewAgas/MarkFuncs/) has really gotten huge progress, at least in my eyes, and now, I want to make math possible in the language.

# Background

**MarkFuncs** is a programming language that I have been working on for a while, which is basically a golflang that works like an object-oriented proglang (the summary for the final product). Currently, the language doesn't have a site to run the language, but it does have an interpreter that can run in [TIO](https://tio.run/#python3). Just copy this code into the Code section, then write a program on the Input section:
```python
from ast import arg
from inspect import signature

BAD_RESULT = "err"

def bottles_of_beer(num):
	for i in range(num, 0, -1):
		if i == 1:
			if num == 1:
				print(f"1 bottle of beer on the wall. 1 bottle of beer.\nLet it be to sit in peace. {num} bottle of beer on the wall.")
			else:
				print(f"1 bottle of beer on the wall. 1 bottle of beer.\nGo to the store and buy some more. {num} bottles of beer on the wall.")
		elif i == 2:
			print(f"{i} bottles of beer on the wall. {i} bottles of beer.\nTake one down and pass it around. {i - 1} bottle of beer on the wall.\n")
		else:
			print(f"{i} bottles of beer on the wall. {i} bottles of beer.\nTake one down and pass it around. {i - 1} bottles of beer on the wall.\n")

def str_replicator(str, num):
	for chr in str:
		print(end = chr * num)

functions = {
	"bottle": bottles_of_beer,
	"repl": str_replicator
}

variables = {}

"""
Helper method
returns (string, new index)
"""
def getString(code, i):
	assert(code[i] == '"')
	i += 1
	str = ""
	while code[i] != '"':
		str += code[i]
		i += 1
	i += 1
	return str, i

def getNum(code, i):
	assert(code[i].isdigit() or code[i] == '.')
	float_check = 0
	num = ""
	while code[i].isdigit() or code[i] == '.':
		if code[i] == '.':
			float_check += 1
		num += code[i]; i += 1
		if float_check > 1:
			print("Invalid syntax → Reuse of decimal points", file = sys.stderr)
			return BAD_RESULT, i
	if float_check == 0:
		return int(num), i
	else:
		return float(num), i

def program(code):
	i = 0
	while i < len(code):
		if code[i] == '→':
			if code[i+1] == '"':
				i += 2
				str = ""
				while code[i] != '"':
					str += code[i]; i += 1
				print(str)
				i += 1
			elif code[i+1].isdigit():
				i += 1
				equation = ""
				while code[i] != ';':
					equation += code[i]; i += 1
				i += 1
			elif code[i+1].isalpha():
				i += 1
				var = ""
				while code[i] != ';':
					var += code[i]; i += 1
				print(variables[var])
				i += 1
			else:
				print(f"Invalid syntax → {code[i]} is not a variable", file = sys.stderr)
		elif code[i].isalpha():
			funcname = ""
			while code[i].isalpha() or code[i].isdigit():
				funcname += code[i]; i += 1
			if code[i] == '(':
				args = []
				i += 1
				while (code[i] != ')'):
					if (code[i] == '"'):
						arg, i = getString(code, i)
					elif (code[i].isdigit() or code[i] == '.'):
						arg, i = getNum(code, i)
					else:
						print(f"Invalid argument → {code[i]} can't be an argument", file = sys.stderr)
					args.append(arg)
					if (code[i] == ','):
						i += 1
					else:
						assert(code[i] == ')')
				i += 1
				if functions[funcname]:
					func = functions[funcname]
					num_req_args = len(signature(func).parameters)
					if (num_req_args != len(args)):
						print(f"Not enough arguments → {funcname} requires {num_req_args} but only {len(args)} were given", file = sys.stderr)
					else:
						func(*args)
				else:
					print(f"Invalid function → {funcname}() does not exist", file = sys.stderr)
			elif code[i] == '=':
				i += 1
				if (code[i] == '"'):
					val, i = getString(code, i)
				elif (code[i].isdigit() or code[i] == '.'):
					val, i = getNum(code, i)
				i += 1
				variables[funcname] = val
		else:
			print(f"Invalid syntax → Character {code[i]} undefined", file = sys.stderr)

while True:
	try:
		s = input()
		program(s)
	except:
		break
```

An example program you can run is:
```none
var1="Hello, ";
→var1;var2="World!";
→var2;
```

The language is obviously incomplete, and I'm using Q&A so I can add more stuff, not just to rely on the [Discord server](https://discord.gg/wwh8XZ9CWS).

# Main Deal

I currently want to implement the mathematical expressions in programming. There are a few things to deal with:

1. Calculate with integers and floating-point numbers:
```none
→3+5; ` Full program
→     ` Output function
 3+5  ` Add 3 and 5
    ; ` Trigger to start a new line
```
<sup>Backticks are supposed to be the syntax for comments, which will be implemented soon.</sup>

2. Calculate variables:
```none
x=10;y=15;→x+=y; ` Full program
x=10;            ` Assign x with 10
     y=15;       ` Assign y with 15
          →x+=y; ` Output x+y, assign x with sum of x and y
```
3. Access the values of variables in functions
```none
i=100;bottle(i-1); ` Full program
i=100;             ` Assign i with 100
      bottle(i-1); ` Run "N Bottles of Beer" using i+1 (see tutorial for more info)
```

I currently have no idea how to implement this properly, especially with the syntax I have involved:
```
+ plus
- minus
* times
/ divided by
% modulo
^ exponentiate
⊕ XOR

Use GOEMDAS when doing math:
- Grouped equations first
- Functions such as modulo and XOR go first
- Exponents are next
- MD: if multiplication goes first in equation, do all multiplication, same goes for division
- AS: if addition goes first in equation, do all addition, same goes for subtraction
```

So how can I properly implement this system under MarkFuncs syntax?