Browse Source

Updates to namespace support

master 0.2.0
sloum 2 months ago
parent
commit
9088fde2c1
  1. 2
      README.md
  2. 68
      main.slo
  3. 4
      module.json

2
README.md

@ -15,7 +15,7 @@ The `infix-calc` procedure is used to calculate the given problem. It has the fo
``` scheme
(load-mod infix-math)
(define problem "1 + -2 * ($X + 1)")
(infix-calc problem [["$X" 5]])
(infix-math::calc problem [["$X" 5]])
; => -11
(infix-calc [1 '+ -2 '* "(" 5 '+ 1 ")"])

68
main.slo

@ -4,12 +4,12 @@
;;; Version: 0.1.0
;;;
(define infix-math-is-keyword? (lambda (val) (or (infix-math-is-op? val) (member? ["(" ")"] val))))
(define infix-math-is-op? (lambda (val) (member? ["-" "+" "*" "/" "<" ">" "="] val)))
(define infix-math-is-digit? (lambda (val) (and (>= (string->rune val) 48) (<= (string->rune val) 57))))
(define infix-math-op-associativity (lambda (op) (if (member? ["-" "+" "*" "/"] op) -1 1)))
(define is-keyword? (lambda (val) (or (is-op? val) (member? ["(" ")"] val))))
(define is-op? (lambda (val) (member? ["-" "+" "*" "/" "<" ">" "="] val)))
(define is-digit? (lambda (val) (and (>= (string->rune val) 48) (<= (string->rune val) 57))))
(define op-associativity (lambda (op) (if (member? ["-" "+" "*" "/"] op) -1 1)))
(define infix-math-op-precedence
(define op-precedence
(lambda (op)
(cond
((member? ["=" "<" ">"] op) 1)
@ -18,58 +18,58 @@
((equal? "^" op) 4)
(else 0))))
(define infix-math-op-actions (lambda (stmt stack)
(define tok-prec (infix-math-op-precedence (car stmt)))
(define tok-assoc (infix-math-op-associativity (car stmt)))
(define op-actions (lambda (stmt stack)
(define tok-prec (op-precedence (car stmt)))
(define tok-assoc (op-associativity (car stmt)))
(define stack-op (if (not (null? stack)) (car stack) []))
(define stack-prec (if (not (null? stack-op)) (infix-math-op-precedence stack-op) 0))
(define stack-prec (if (not (null? stack-op)) (op-precedence stack-op) 0))
(if
(or
(and (negative? tok-assoc) (<= tok-prec stack-prec))
(and (positive? tok-assoc) (< tok-prec stack-prec)))
(cons stack-op (infix-math-shunting-yard stmt (cdr stack)))
(infix-math-shunting-yard (cdr stmt) (cons (car stmt) stack)))))
(cons stack-op (shunting-yard stmt (cdr stack)))
(shunting-yard (cdr stmt) (cons (car stmt) stack)))))
(define infix-math-stack-ops (lambda (stack)
(define stack-ops (lambda (stack)
(cond
((and (not (null? stack)) (equal? (car stack) "("))
(! "Unbalanced parens"))
((null? stack) stack)
(else (cons (car stack) (infix-math-shunting-yard [] (cdr stack)))))))
(else (cons (car stack) (shunting-yard [] (cdr stack)))))))
(define infix-math-shunting-yard (lambda (stmt stack)
(define shunting-yard (lambda (stmt stack)
(cond
((null? stmt)
(infix-math-stack-ops stack))
(stack-ops stack))
((string->number (car stmt))
(cons (car stmt) (infix-math-shunting-yard (cdr stmt) stack)))
((infix-math-is-op? (car stmt))
(infix-math-op-actions stmt stack))
(cons (car stmt) (shunting-yard (cdr stmt) stack)))
((is-op? (car stmt))
(op-actions stmt stack))
((equal? (car stmt) "(")
(infix-math-shunting-yard (cdr stmt) (cons (car stmt) stack)))
(shunting-yard (cdr stmt) (cons (car stmt) stack)))
((equal? (car stmt) ")")
(if (and (pair? stack) (equal? "(" (car stack)))
(infix-math-shunting-yard (cdr stmt) (cdr stack))
(cons (car stack) (infix-math-shunting-yard stmt (cdr stack))))))))
(shunting-yard (cdr stmt) (cdr stack))
(cons (car stack) (shunting-yard stmt (cdr stack))))))))
(define infix-math-prefix-calc (lambda (arr)
(define prefix-calc (lambda (arr)
(define s [])
(if (null? arr)
0
(begin
(for-each
(lambda (v)
(if (infix-math-is-op? v)
(if (is-op? v)
(cond
((equal? v "+") (set! s (cons (+ (car (cdr s)) (car s)) (cdr (cdr s)))))
((equal? v "-") (set! s (cons (- (car (cdr s)) (car s)) (cdr (cdr s)))))
((equal? v "*") (set! s (cons (* (car (cdr s)) (car s)) (cdr (cdr s)))))
((equal? v "/") (set! s (cons (/ (car (cdr s)) (car s)) (cdr (cdr s))))))
(set! s (cons (string->number v) s))))
(infix-math-shunting-yard arr []))
(shunting-yard arr []))
(if (not (equal? (length s) 1)) (! "Invalid prefix expression") (car s))))))
(define infix-math-parse-string (lambda (input)
(define parse-string (lambda (input)
(set! input (append "" input))
(define prev "")
(reverse
@ -78,16 +78,16 @@
(begin0
(cond
((member? [" " "\t" "\n" "\r"] val) acc)
((and (infix-math-is-digit? val) (not (null? acc)) (equal? prev "-"))
((and (is-digit? val) (not (null? acc)) (equal? prev "-"))
(cons (append (car acc) val) (cdr acc)))
((infix-math-is-keyword? val) (cons val acc))
((and (not (null? acc)) (infix-math-is-keyword? (car acc))) (cons val acc))
((is-keyword? val) (cons val acc))
((and (not (null? acc)) (is-keyword? (car acc))) (cons val acc))
(else (if (null? acc) (cons val acc) (cons (append (car acc) val) (cdr acc)))))
(set! prev val)))
[]
(string->list input)))))
(define infix-math-replace-vars (lambda (ex en)
(define replace-vars (lambda (ex en)
(define panic? (exception-mode-panic?))
(if panic? (exception-mode-pass))
(begin0
@ -101,13 +101,13 @@
ex))
(if panic? (exception-mode-panic)))))
(define infix-calc (lambda (exp ...)
(define calc (lambda (exp ...)
(if (list? exp)
(set! exp (map (lambda (v) (append "" v)) exp))
(set! exp (infix-math-parse-string exp)))
(set! exp (parse-string exp)))
(if (and (not (null? ...)) (assoc? (car ...)))
(set! exp (infix-math-replace-vars exp (car ...))))
(infix-math-prefix-calc exp)))
(set! exp (replace-vars exp (car ...))))
(prefix-calc exp)))
(define _USAGE [
["infix-calc" "(infix-calc [problem: string|list] [[vars: assoc]]) => number\n\nThe available operations for a problem are:\n\n\t+, -, *, /, (, and )\n\nPositive, negative, floating point, and integer numbers are all supported. If using subtraction there must be a space between the operator and the operand so-as to prevent confusion between a negative number and a subtraction operation. Variables should be given as an associative list of key value pairs. The key should be a string as it would appear in the problem and the value will replace it in the actual calculation"]])
["calc" "(infix-math::calc [problem: string|list] [[vars: assoc]]) => number\n\nThe available operations for a problem are:\n\n\t+, -, *, /, (, and )\n\nPositive, negative, floating point, and integer numbers are all supported. If using subtraction there must be a space between the operator and the operand so-as to prevent confusion between a negative number and a subtraction operation. Variables should be given as an associative list of key value pairs. The key should be a string as it would appear in the problem and the value will replace it in the actual calculation"]])

4
module.json

@ -2,9 +2,9 @@
"title": "infix-math",
"description": "Infix math calculations",
"author": "sloum",
"version": "0.1.0",
"version": "0.2.0",
"homepage": "",
"repository": "https://git.rawtext.club/sloum/request",
"tag": "0.1.0",
"tag": "0.2.0",
"dependencies": []
}

Loading…
Cancel
Save