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

Dashboard
Notifications
Mark all as read
Q&A

Adding support on mathematical expressions

+0
−3

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. Just copy this code into the Code section, then write a program on the Input section:

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:

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.

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:
→3+5; ` Full program
→     ` Output function
 3+5  ` Add 3 and 5
    ; ` Trigger to start a new line

Backticks are supposed to be the syntax for comments, which will be implemented soon.

  1. Calculate variables:
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
  1. Access the values of variables in functions
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?

Why does this post require moderator attention?
You might want to add some details to your flag.
Why should this post be closed?

3 comment threads

Side note: perhaps you're not doing it right (or at least, not the best way) (2 comments)
Precedence (2 comments)
Expression trees or perhaps it's better for you to stick with RPN (5 comments)

0 answers

Sign up to answer this question »