aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/expressions.h
diff options
context:
space:
mode:
authorRobin Haberkorn <robin.haberkorn@googlemail.com>2021-05-30 02:38:43 +0200
committerRobin Haberkorn <robin.haberkorn@googlemail.com>2021-05-30 03:12:56 +0200
commit432ad24e382681f1c13b07e8486e91063dd96e2e (patch)
tree51838adac822767bd5884b9383cd4c72f29d3840 /src/expressions.h
parent524bc3960e6a6e5645ce904e20f72479e24e0a23 (diff)
downloadsciteco-432ad24e382681f1c13b07e8486e91063dd96e2e.tar.gz
THE GREAT CEEIFICATION EVENT
This is a total conversion of SciTECO to plain C (GNU C11). The chance was taken to improve a lot of internal datastructures, fix fundamental bugs and lay the foundations of future features. The GTK user interface is now in an useable state! All changes have been squashed together. The language itself has almost not changed at all, except for: * Detection of string terminators (usually Escape) now takes the string building characters into account. A string is only terminated outside of string building characters. In other words, you can now for instance write I^EQ[Hello$world]$ This removes one of the last bits of shellisms which is out of place in SciTECO where no tokenization/lexing is performed. Consequently, the current termination character can also be escaped using ^Q/^R. This is used by auto completions to make sure that strings are inserted verbatim and without unwanted sideeffects. * All strings can now safely contain null-characters (see also: 8-bit cleanliness). The null-character itself (^@) is not (yet) a valid SciTECO command, though. An incomplete list of changes: * We got rid of the BSD headers for RB trees and lists/queues. The problem with them was that they used a form of metaprogramming only to gain a bit of type safety. It also resulted in less readble code. This was a C++ desease. The new code avoids metaprogramming only to gain type safety. The BSD tree.h has been replaced by rb3ptr by Jens Stimpfle (https://github.com/jstimpfle/rb3ptr). This implementation is also more memory efficient than BSD's. The BSD list.h and queue.h has been replaced with a custom src/list.h. * Fixed crashes, performance issues and compatibility issues with the Gtk 3 User Interface. It is now more or less ready for general use. The GDK lock is no longer used to avoid using deprecated functions. On the downside, the new implementation (driving the Gtk event loop stepwise) is even slower than the old one. A few glitches remain (see TODO), but it is hoped that they will be resolved by the Scintilla update which will be performed soon. * A lot of program units have been split up, so they are shorter and easier to maintain: core-commands.c, qreg-commands.c, goto-commands.c, file-utils.h. * Parser states are simply structs of callbacks now. They still use a kind of polymorphy using a preprocessor trick. TECO_DEFINE_STATE() takes an initializer list that will be merged with the default list of field initializers. To "subclass" states, you can simply define new macros that add initializers to existing macros. * Parsers no longer have a "transitions" table but the input_cb() may use switch-case statements. There are also teco_machine_main_transition_t now which can be used to implement simple transitions. Additionally, you can specify functions to execute during transitions. This largely avoids long switch-case-statements. * Parsers are embeddable/reusable now, at least in parse-only mode. This does not currently bring any advantages but may later be used to write a Scintilla lexer for TECO syntax highlighting. Once parsers are fully embeddable, it will also be possible to run TECO macros in a kind of coroutine which would allow them to process string arguments in real time. * undo.[ch] still uses metaprogramming extensively but via the C preprocessor of course. On the downside, most undo token generators must be initiated explicitly (theoretically we could have used embedded functions / trampolines to instantiate automatically but this has turned out to be dangereous). There is a TECO_DEFINE_UNDO_CALL() to generate closures for arbitrary functions now (ie. to call an arbitrary function at undo-time). This simplified a lot of code and is much shorter than manually pushing undo tokens in many cases. * Instead of the ridiculous C++ Curiously Recurring Template Pattern to achieve static polymorphy for user interface implementations, we now simply declare all functions to implement in interface.h and link in the implementations. This is possible since we no longer hace to define interface subclasses (all state is static variables in the interface's *.c files). * Headers are now significantly shorter than in C++ since we can often hide more of our "class" implementations. * Memory counting is based on dlmalloc for most platforms now. Unfortunately, there is no malloc implementation that provides an efficient constant-time memory counter that is guaranteed to decrease when freeing memory. But since we use a defined malloc implementation now, malloc_usable_size() can be used safely for tracking memory use. malloc() replacement is very tricky on Windows, so we use a poll thread on Windows. This can also be enabled on other supported platforms using --disable-malloc-replacement. All in all, I'm still not pleased with the state of memory limiting. It is a mess. * Error handling uses GError now. This has the advantage that the GError codes can be reused once we support error catching in the SciTECO language. * Added a few more test suite cases. * Haiku is no longer supported as builds are instable and I did not manage to debug them - quite possibly Haiku bugs were responsible. * Glib v2.44 or later are now required. The GTK UI requires Gtk+ v3.12 or later now. The GtkFlowBox fallback and sciteco-wrapper workaround are no longer required. * We now extensively use the GCC/Clang-specific g_auto feature (automatic deallocations when leaving the current code block). * Updated copyright to 2021. SciTECO has been in continuous development, even though there have been no commits since 2018. * Since these changes are so significant, the target release has been set to v2.0. It is planned that beginning with v3.0, the language will be kept stable.
Diffstat (limited to 'src/expressions.h')
-rw-r--r--src/expressions.h399
1 files changed, 124 insertions, 275 deletions
diff --git a/src/expressions.h b/src/expressions.h
index bdd683c..6ff8af4 100644
--- a/src/expressions.h
+++ b/src/expressions.h
@@ -1,5 +1,5 @@
/*
- * Copyright (C) 2012-2017 Robin Haberkorn
+ * Copyright (C) 2012-2021 Robin Haberkorn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -14,300 +14,149 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-
-#ifndef __EXPRESSIONS_H
-#define __EXPRESSIONS_H
+#pragma once
#include <glib.h>
-#include "memory.h"
+#include "sciteco.h"
#include "undo.h"
-#include "error.h"
-
-namespace SciTECO {
-
-template <typename Type>
-class ValueStack : public Object {
- /*
- * NOTE: Since value stacks are usually singleton,
- * we pass them as a template parameter, saving space
- * in the undo token.
- */
- template <ValueStack<Type> &stack>
- class UndoTokenPush : public UndoToken {
- Type value;
- guint index;
-
- public:
- UndoTokenPush(Type _value, guint _index = 0)
- : value(_value), index(_index) {}
-
- void
- run(void)
- {
- stack.push(value, index);
- }
- };
-
- template <ValueStack<Type> &stack>
- class UndoTokenPop : public UndoToken {
- guint index;
-
- public:
- UndoTokenPop(guint _index = 0)
- : index(_index) {}
-
- void
- run(void)
- {
- stack.pop(index);
- }
- };
-
- /** Beginning of stack area */
- Type *stack;
- /** End of stack area */
- Type *stack_top;
-
- /** Pointer to top element on stack */
- Type *sp;
-
-public:
- ValueStack(gsize size = 1024)
- {
- stack = new Type[size];
- /* stack grows to smaller addresses */
- sp = stack_top = stack+size;
- }
-
- ~ValueStack()
- {
- delete[] stack;
- }
-
- inline guint
- items(void)
- {
- return stack_top - sp;
- }
-
- inline Type &
- push(Type value, guint index = 0)
- {
- if (G_UNLIKELY(sp == stack))
- throw Error("Stack overflow");
-
- /* reserve space for new element */
- sp--;
- g_assert(items() > index);
-
- /* move away elements after index (index > 0) */
- for (guint i = 0; i < index; i++)
- sp[i] = sp[i+1];
-
- return sp[index] = value;
- }
-
- template <ValueStack<Type> &stack>
- static inline void
- undo_push(Type value, guint index = 0)
- {
- undo.push<UndoTokenPush<stack>>(value, index);
- }
-
- inline Type
- pop(guint index = 0)
- {
- /* peek() already asserts */
- Type v = peek(index);
-
- /* elements after index are moved to index (index > 0) */
- while (index--)
- sp[index+1] = sp[index];
-
- /* free space of element to pop */
- sp++;
-
- return v;
- }
-
- template <ValueStack<Type> &stack>
- static inline void
- undo_pop(guint index = 0)
- {
- undo.push<UndoTokenPop<stack>>(index);
- }
-
- inline Type &
- peek(guint index = 0)
- {
- g_assert(items() > index);
-
- return sp[index];
- }
-
- /** Clear all but `keep_items` items. */
- inline void
- clear(guint keep_items = 0)
- {
- g_assert(keep_items <= items());
-
- sp = stack_top - keep_items;
- }
-};
/**
- * Arithmetic expression stacks
+ * Defines a function undo__insert_val__ARRAY() to insert a value into
+ * a fixed GArray.
+ *
+ * @note
+ * This optimizes undo token memory consumption under the assumption
+ * that ARRAY is a global object that does not have to be stored in
+ * the undo tokens.
+ * Otherwise, you could simply undo__g_array_insert_val(...).
+ *
+ * @fixme
+ * If we only ever use INDEX == ARRAY->len, we might simplify this
+ * to undo__append_val__ARRAY().
*/
-extern class Expressions : public Object {
-public:
- /**
- * Operator type.
- * The enumeration value divided by 16 represents
- * its precedence (small values mean low precedence).
- * In other words, the value's lower nibble is
- * reserved for enumerating operators of the
- * same precedence.
- */
- enum Operator {
- /*
- * Pseudo operators
- */
- OP_NIL = 0x00,
- OP_NEW,
- OP_BRACE,
- OP_NUMBER,
- /*
- * Real operators
- */
- OP_POW = 0x60, // ^*
- OP_MOD = 0x50, // ^/
- OP_DIV, // /
- OP_MUL, // *
- OP_SUB = 0x40, // -
- OP_ADD, // +
- OP_AND = 0x30, // &
- OP_XOR = 0x20, // ^#
- OP_OR = 0x10 // #
- };
+#define TECO_DEFINE_ARRAY_UNDO_INSERT_VAL(ARRAY, TYPE) \
+ static inline void \
+ insert_val__##ARRAY(guint index, TYPE value) \
+ { \
+ g_array_insert_val(ARRAY, index, value); \
+ } \
+ TECO_DEFINE_UNDO_CALL(insert_val__##ARRAY, guint, TYPE)
-private:
- /** Get operator precedence */
- inline gint
- precedence(Operator op)
- {
- return op >> 4;
- }
+/**
+ * Defines a function undo__remove_index__ARRAY() to remove a value from
+ * a fixed GArray.
+ *
+ * @note
+ * See TECO_DEFINE_ARRAY_UNDO_INSERT_VAL().
+ * undo__g_array_remove_index(...) would also be possible.
+ *
+ * @fixme
+ * If we only ever use INDEX == ARRAY->len-1, we might simplify this
+ * to undo__pop__ARRAY().
+ */
+#define TECO_DEFINE_ARRAY_UNDO_REMOVE_INDEX(ARRAY) \
+ static inline void \
+ remove_index__##ARRAY(guint index) \
+ { \
+ g_array_remove_index(ARRAY, index); \
+ } \
+ TECO_DEFINE_UNDO_CALL(remove_index__##ARRAY, guint)
+/**
+ * Operator type.
+ * The enumeration value divided by 16 represents
+ * its precedence (small values mean low precedence).
+ * In other words, the value's lower nibble is
+ * reserved for enumerating operators of the
+ * same precedence.
+ */
+typedef enum {
/*
- * Number and operator stacks are static, so
- * they can be passed to the undo token constructors.
- * This is OK since Expression is singleton.
+ * Pseudo operators
*/
- typedef ValueStack<tecoInt> NumberStack;
- static NumberStack numbers;
-
- typedef ValueStack<Operator> OperatorStack;
- static OperatorStack operators;
-
-public:
- Expressions() : num_sign(1), radix(10), brace_level(0) {}
-
- gint num_sign;
- inline void
- set_num_sign(gint sign)
- {
- undo.push_var(num_sign) = sign;
- }
-
- gint radix;
- inline void
- set_radix(gint r)
- {
- undo.push_var(radix) = r;
- }
-
- tecoInt push(tecoInt number);
-
- /**
- * Push characters of a C-string.
- * Could be overloaded on push(tecoInt)
- * but this confuses GCC.
+ TECO_OP_NIL = 0x00,
+ TECO_OP_NEW,
+ TECO_OP_BRACE,
+ TECO_OP_NUMBER,
+ /*
+ * Real operators
*/
- inline void
- push_str(const gchar *str)
- {
- while (*str)
- push(*str++);
- }
-
- inline tecoInt
- peek_num(guint index = 0)
- {
- return numbers.peek(index);
- }
- tecoInt pop_num(guint index = 0);
- tecoInt pop_num_calc(guint index, tecoInt imply);
- inline tecoInt
- pop_num_calc(guint index = 0)
- {
- return pop_num_calc(index, num_sign);
- }
+ TECO_OP_POW = 0x60, // ^*
+ TECO_OP_MOD = 0x50, // ^/
+ TECO_OP_DIV, // /
+ TECO_OP_MUL, // *
+ TECO_OP_SUB = 0x40, // -
+ TECO_OP_ADD, // +
+ TECO_OP_AND = 0x30, // &
+ TECO_OP_XOR = 0x20, // ^#
+ TECO_OP_OR = 0x10 // #
+} teco_operator_t;
+
+extern gint teco_num_sign;
+
+static inline void
+teco_set_num_sign(gint sign)
+{
+ teco_undo_gint(teco_num_sign) = sign;
+}
+
+extern gint teco_radix;
+
+static inline void
+teco_set_radix(gint r)
+{
+ teco_undo_gint(teco_radix) = r;
+}
+
+void teco_expressions_push_int(teco_int_t number);
+
+/** Push characters of a C-string. */
+static inline void
+teco_expressions_push_str(const gchar *str)
+{
+ while (*str)
+ teco_expressions_push_int(*str++);
+}
+
+teco_int_t teco_expressions_peek_num(guint index);
+teco_int_t teco_expressions_pop_num(guint index);
+gboolean teco_expressions_pop_num_calc(teco_int_t *ret, teco_int_t imply, GError **error);
+
+void teco_expressions_add_digit(gchar digit);
+
+void teco_expressions_push_op(teco_operator_t op);
+gboolean teco_expressions_push_calc(teco_operator_t op, GError **error);
- tecoInt add_digit(gchar digit);
-
- Operator push(Operator op);
- Operator push_calc(Operator op);
- inline Operator
- peek_op(guint index = 0)
- {
- return operators.peek(index);
- }
- Operator pop_op(guint index = 0);
-
- void eval(bool pop_brace = false);
-
- guint args(void);
-
- void discard_args(void);
+/*
+ * FIXME: Does not work for TECO_OP_* constants as they are treated like int.
+ */
+#define teco_expressions_push(X) \
+ (_Generic((X), default : teco_expressions_push_int, \
+ char * : teco_expressions_push_str, \
+ const char * : teco_expressions_push_str)(X))
- /** The nesting level of braces */
- guint brace_level;
+teco_operator_t teco_expressions_peek_op(guint index);
+teco_operator_t teco_expressions_pop_op(guint index);
- inline void
- brace_open(void)
- {
- push(OP_BRACE);
- undo.push_var(brace_level)++;
- }
+gboolean teco_expressions_eval(gboolean pop_brace, GError **error);
- void brace_return(guint keep_braces, guint args = 0);
+guint teco_expressions_args(void);
- inline void
- brace_close(void)
- {
- if (!brace_level)
- throw Error("Missing opening brace");
- undo.push_var(brace_level)--;
- eval(true);
- }
+gint teco_expressions_first_op(void);
- inline void
- clear(void)
- {
- numbers.clear();
- operators.clear();
- brace_level = 0;
- }
+gboolean teco_expressions_discard_args(GError **error);
- const gchar *format(tecoInt number);
+extern guint teco_brace_level;
-private:
- void calc(void);
+void teco_expressions_brace_open(void);
+gboolean teco_expressions_brace_return(guint keep_braces, guint args, GError **error);
+gboolean teco_expressions_brace_close(GError **error);
- gint first_op(void);
-} expressions;
+void teco_expressions_clear(void);
-} /* namespace SciTECO */
+/** Maximum size required to format a number if teco_radix == 2 */
+#define TECO_EXPRESSIONS_FORMAT_LEN \
+ (1 + sizeof(teco_int_t)*8 + 1)
-#endif
+gchar *teco_expressions_format(gchar *buffer, teco_int_t number);