Commit Graph

40 Commits

Author SHA1 Message Date
Denneisk
1c14312b14
Fix try/catch would not pop excess scopes (#3216)
* Fix try/catch would not pop excess scopes

* Set scope directly

* Add test
2024-12-16 00:10:22 -05:00
Denneisk
79f14291fa
Add new E2 hash functions (#3199)
* Add hash functions

* Add the rest of them
Why not?
Increased size limit

* Change to hard throw

* Remove return (I forgot to commit this)
2024-12-04 14:25:55 -05:00
Denneisk
6a1216128b
Rewrite E2 table clone function/partial table extension modernization (#3195)
* Rewrite table copy functions
Add shallow table copying
Add table perf function
Minor optimizations and cleanups

* E2descriptions

* Remove references to arrayCopy
It was a stub, NOT AI-generated. I swear.

* Combine these loops

* Remove unnecessary return statement

* Add test
2024-12-04 19:24:09 +00:00
Denneisk
93bb7cc4e9
Add array:indexOf (#2939)
* Update array functions
Add array:indexOf

* Remove remainder of C nots

* Update description

* Change loop
2024-01-16 22:37:47 +00:00
Vurv
b66fc4e693
Fix #2958 (#2959) 2023-12-28 04:27:56 -06:00
Vurv
4edf127222
Speed up compiler slightly, Lua tests (#2923) 2023-12-06 19:00:13 -08:00
Vurv
8fa7912254 Fix log2
Fixes #2924
2023-12-04 15:00:26 -08:00
Vurv
a5ff4ba321
Rewrite number library (#2903)
* Rewrite number library

* Fixes, more tests

* Fix using incorrect arguments in functions
* Tag more functions as explicitly `number` parameters
* More test coverage
2023-11-27 16:03:01 -08:00
Vurv
b5d10b97ea Fix #2883 2023-11-19 21:51:50 -08:00
Vurv
9add762165
Allow implicit function return types (#2866)
Resolves #2865
2023-11-18 22:00:42 -08:00
Vurv
28be0186f8 Fix changed(a)
Fixes #2876
2023-11-18 00:26:38 -08:00
Vurv
6d40b57d00 Don't allow functions to be named function
They would be useless anyway since you can't call it.
2023-11-16 18:31:00 -08:00
Vurv
7ba37430da Fix #2864 2023-11-16 18:22:49 -08:00
Vurv
f63f88fcd0
Add tests for #2860 (#2861)
Should've included these in the commit
2023-11-16 12:55:06 -08:00
Vurv
3522df956e
Add lambdas to E2 (#2829)
* Initial lambda implementation

* Add ops on creation

* New implementation

* Functions are now tables, containing parameter signature, return type and inner function
* function:getParameterTypes()
* function:getReturnType()
* Don't reset global variables on `@strict`, fixing issue with top level locals not working as upvalues since they'd be reset by the runtime. I don't think this would actually affect anyone since you shouldn't be able to use variables before they're assigned, but it's `@strict` only behavior anyway.

* Fix missing parity change

I'd already fixed this bug with functions that have return values but this was not fixed for functions without return values. Also added a test case for this.

* More tests, enforce returns at compile time

* Add tests to ensure variadic parameters, void parameters and implicit parameters aren't allowed

* Fix lambdas potentially not returning at all codepaths like functions now are expected to do. Added a test for that.

* Fix early returns

I should stop prematurely optimizing this.. This is like the third or second time I've made this mistake lol

* Fix highlighting

Actually a really easy fix. Surprising

* Fix merge regression
2023-11-12 16:16:38 -08:00
Vurv
a4e6c4f78d
Make functions a compile time construct on @strict (#2789)
* Don't allow overriding functions with `@strict`

* Add nested function warning

* Lower base op cost

5 ops on `@strict`, 8 without

* Remove extra 4 ops

* Add tests and fix methods
2023-11-12 15:43:09 -08:00
Denneisk
5316d8b41c
Extend E2 Tokenizer String Escapes (#2754)
* Use ParseEscapes in Tokenizer

* Lints

* Store sequence before escaping

* Rewrite the thing

* Handle case for \u{\u{x}}
to parse as "\u{", "\u{x}" instead of "\u{\u{x}}" or "\u{", "\u{x}}"

* Cooler error handling

* Fix oversight in hex escapes

* lol. lmao

* Add tests

* Implement Vurv's toUnicodeChar implementation
Fix unicode escape could be less than 0
Harden unicode escapes against bad input strings
2023-11-09 17:08:03 -05:00
Vurv
04d275f25e
Revert new $ behavior, deprecate $ (#2794) 2023-11-09 14:01:19 -08:00
Denneisk
e4d7aaf2e8
Fix foreach ending on tables with type mismatch (#2804)
* Fix foreach ending on tables with type mismatch

* Update test
2023-10-30 14:42:29 -04:00
Vurv
6f8480bd52 Fix foreach number foreach w/ nonsequential index 2023-10-21 22:14:42 -07:00
Vurv
58c9c707e7
Remove wire_expression2_debug, registerType [5], GLON (#2799)
* Remove wire_expression2_debug, type check field from registerType

* Undo typeSanitizers removal, add tests
2023-10-19 13:52:49 -07:00
Vurv
810a570779 Fix do while
Test wasn't adequate and I didn't pass an extra value to the node data
2023-10-14 15:51:46 -07:00
Vurv
59ca86eff7
Enforce function returns at compile time (#2788)
* Enforce function returns at compile time

* Raise cost of dynamic functions

* Revert previous change

Forgot const optimization isn't currently being used. Needs an analyzer step.

Should really not have included that code in the rewrite but it's fine being there for now.

* Add test cases

* Simplify

No need for `returned` field, can just check if scope is dead.

* Add switch case return logic

Adds logic for switch case to be the last statement in a function and be detected for return values.
2023-10-13 13:49:37 -07:00
Vurv
cd4d012b01 Fix return inside switch case
Fixes #2783
2023-10-05 17:13:10 -07:00
Vurv
ce68e90499 Fix stringcall method return 2023-10-04 10:39:07 -07:00
Vurv
d7c73b08dd
Add constant variables to E2 (#2769) 2023-10-04 09:11:43 -07:00
Vurv
44d9366a5f
E2 Compiler Rewrite (#2555)
* Initial attempt

* Sync

* Sync

* First functional prototype

* Removed ast.lua file (Largely useless)
* Removed optimizer.lua (Will be reimplemented with the new compiler as a step later.)
* Documented more parser changes
* Make tokenizer behave backwards-compatibly again
* Delete Compiler and Reimplement
* Removed syranide credit since E2 has been largely completely rewritten by me and wireteam/contributors

With just ~300 LOC the new compiler can already handle for loops, if statements and function calls. I just need to implement the rest of the functionality and make functions backward compatible.

* More changes

* Reworked preprocessor to also return multiple errors, and to use the new trace/error system.

* Added multi-error support to the editor

* All compiler steps now return errors in the form of the "Error" struct. Even E2Lib.errorHandler now will return one.

* Made wire_expression2_reload also reload the compiler and E2Lib.

* Work on backwards compatibility for registerFunction, registerOperator, registerType, etc functions. These are being deprecated for a future better extension api that won't need any preprocessing.

* Optimization: Only check for continue and break when inside a loop

* Add more compiler instruction support (Try, Function, Arithmetic, etc). Still need to deal with handling variables aot to avoid runtime scoping.

* Pass test suite

* wire_expression2_validate -> E2Lib.Validate
* removed more useless operator internals

Passing test suite doesn't really mean anything because it only tests the parsing part really well. It isn't comprehensive considering all tests pass despite me not having implemented variable scoping at runtime nor index assignment.

* Reimplement prf checks & Variadic params

* Reimplemented perf checks, probably not 1:1 but it works.

* Fixed lua error when the e2 editor was loading extensions

* Fix calling E2Lib.Compiler.Execute incorrectly in E2Lib.Validate

* Reimplemented variadic parameters

* Fix methodcall and legacy case

* If attributes not specified in `registerFunction`, automatically add and mark as legacy.

* Fix methodcall not properly taking arguments

* Make parser arguments function faster and more concise

* Many changes

* Moved ScopeManager into the e2lib
* Implement key value constructors for array and table
* Implement index get operator (no backwards compat)
* Reimplement index get operator for table and array, lower op cost (1 op for arrays, 3 for table from 5 for both.)
* Auto-mark registerOperator functions as legacy same as functions
* Add check for self.registered_events in destructor in case compilecode isn't called for whatever reason.
* Remove unused DefineLocal node variant
* Implement more helpful error message for (=) in expression.
* Reimplement includes

A lot closer to functional, still very far though.

* Fix runtime error detection

* Inject trace into state every sequence

Sad runtime performance hit, maybe in the future there could be a system for fallibility so it only injects when doing something potentially fallible

* Fix e2test runtime errors

* Too many changes

* Implemented indexing assignment
* Implemented runtime scoping
* Partially-implemented events
* Rewrote array setters
* Fix assignment to another identifier
* Fix global variable initialization on E2 start (persist/io)
* Remove unused / redundant / now unsupported operators from extpp
* Make table setter/getters cost less ops

I need to reimplement all the warnings I added, and a check for variadic syntax sugar to ensure arrays aren't passed into array varargs.

Also there's a weird parsing issue with doing X = X + 1. (X = 1 + X works)

* Runtime changes

* Replace ScopeManager with RuntimeContext class (now shared with makeContext function

* Remove now redundant assignment operators. Need to come up with a solution for special table-like types, though.

* Removed and/or/neq/not operators, since they are redundant and can be done internally using the "is" operator.

* Fixed printing of arrays to not be garbage (Doesn't break on empty arrays, and much faster using table.concat)

* Removed "delta" functionality, it's slow, useless, and I have no idea why anyone would ever want this, and probably nobody will notice it was removed.

* Rewrote a good amount of operators from the preprocessor syntax to the new registerOperator arguments. All operators will have to be rewritten, functions will keep backwards compatibility since we can't expect extension developers to rewrite everything.

* Fixed parser issue when assigning an identifier

* Fixed parser issue when using an identifier in certain cases

There's still an issue with doing X = X + 1 (wtf)

* Make for loop and while loop iterations only cost 1 / 20 ops per iteration (base cost). This is just the cost of an empty block. It's added to the cost of what's in the actual block so it's fine to be this low. It also means now E2 can run an empty for loop to ~400k numbers at base quota.

* Reimplement legacy op support, Runtime changes

* Reimplement support for legacy operators for certain operations

* Mark new numeric operators as nonlegacy

* Remove "delta" use from vector equal

* Delete unnecessary != operator from vector

* Remove unnecessary push and pop scopes on execute (Should start on scope 0 anyway.)

* Fix parse error when doing X = Y + 1

* Fixes

* Fixed hopefully final issue parsing assignments

* Change opcost of print functions (lower normal ones, raise driver/color ones)

* Port string operators to new api (and fix some functions that could be abused)

* Fixed inputs/outputs

* Mark ported angle operators as nonlegacy

* Add variadic unit test

* More fixes

* Fixed outputs not using correct wire_expression_types table
* Tidy include and preprocessor code
* Fix recursion typing
* Fix an issue from creating an error message concatenating a void returning function's return

* Parity (Awfulness)

* Disallow user functions overriding lua functions
* Disallow overriding user functions with functions that return differing types
* Fix "is" operator by marking as nonlegacy
* Fix string calls implementation (ew)
* Fix function recursion by reimplementing the old isolated scope system (ew)
* Fix error when assigning to void expression (ew)
* Only allow incrementing/decrementing number types

* Fix local declaration and increment/decrement

* Local declaration now has its proper special case so it isn't being treated as an assignment

* Increment/Decrement lookup a variable's scope rather than only looking in the current scope

* Fix case of indexing breaking parsing

Fixed issue where if you had a methodcall or stringcall after an index it'd fail to parse

* Multiple fixes

* Fix wirelinks (Rewrite operators with new system)

* Remove nodiscard from runOnTick

* Rewrite string idx operator to new system

* Fix some incomplete parsing

* Don't allow void functions to be used as an expression (Separate CompileNode into CompileExpr and CompileStmt)

* Rewrite foreach

* Removed "fea" operator for new "iter" operator, which is much simpler and doesn't require the end user to manage executing the E2.

* Rewrote foreach operations to use "iter" operator.

* Support data indexing clks

* Fix and operation (was being called incorrectly)
* Remove all assignment operators
* Support mutable data type clks
* Remove all != operators (now redundant, uses == operator internally.)
* Port wirelink index setters getters to new system

* Switch case support

Final tweaks on switch case working. This would also resolve #2049 since operators take the actual values as arguments rather than instructions.

* Fix a unit test

* Implement unary wire operators

Removed their operators (registerOperator) and are now implemented in the compiler.

* Kill `wire_expression2_delta`

Thank fuck

Also made a ton more branches for and/or operators for mixing with legacy operations.. awful..

* Fix strict and 2420 unit tests

* Fixed writing error message to wrong scope
* Fixed 2420 regression test erroring because of the e2lib emulation function pushing a scope before running the code (as E2 used to do)

* Fix function scoping at compile time

When you had a function nested somewhere since it wasn't isolating the scope (like it does at runtime) it was indexing the wrong location for the function parameters.

Also made events do this

* Fix methodcall passing function for method

* Fix indexing assignment parsing and compilation

* Replace "idx" operator with new "indexset" and "indexget" operators since I don't plan on supporting backwards compatibility for it anyway.

Addons don't really make types that use it so this shouldn't be a problem.

* Removed kvarray and kvtable operators since they're implemented in the compiler now.

* Removed operator[] and operator[T] extpp support

* Removed remaining use of `delta` in quaternion, entity, complex, bone libraries

* Ops changes + operator_is -> "is"

* Port uses of operator_is syntax to registerOperator("is"). I don't want to provide backwards compat for it but I might have to, granted it'll be through the preprocessor.

* Don't add cost of statements in loop ops (Because the block itself already adds that to the current prf.) Also made all costs 1 / 20. Foreach is the outlier which costs slightly more at 1 / 15.

* Remove sudo legacy support from not operator (pointless since legacy "is" wasn't supported anywhere else)

* Reimplement unused variable warn + Discards

* Reimplemented discards in a simpler / hackier fashion by just having them be valid identifiers that are not actually declared at compile time.

* Reimplemented unused variable warnings.

* Reimplemented error when trying to use multiple parameters with same name

* Fix cases where variables aren't marked as used

* Fix "This" unused trace

Will make it point to the function itself.

* Fix fastest case condition with meta type

When this check is done the type and name haven't been inserted into param_types and param_names, so it was doing this despite being passed a method function

* Fix operators not marked as nonlegacy

Entity equality and bone equality were trying to run with legacy parameters

* Hopefully finally fix assignment parsing

* Fix parser's `Assert` error

Was using display from old code from before I made E2 take error structs instead of strings for error handling.

* Mark indexing assignment variables as used

* Rewrite vector operators

Now using registerOperator without legacy.

* Allow variable shadowing & Fix -> operator

* Variable shadowing is apparently supposed to be allowed in E2. So I changed the error to a warning.

* Fixed oversight when changing compiler `inputs` and `outputs` to the full IODirective struct (which broke the -> operator)

* Fix error compiling void recursion

* Simplify include code

Also implemented runtime scoping. Need to optimize it later though.

* Fix include scoping

* Fix some scopings

* Make foreach only push scope once for performance gains
* Add runtime scoping to switch case as it should have had.
* Use assert where verbose erroring was used to compact compiler code
* Store scope in for loop implementation as a micro-optimization rather than constantly indexing `state.Scope`

* Check for nil in switch

* Fix userfunction vararg check

* Disallow overriding variadic builtins with the variadic sugar as it currently is in the current compiler.
* Change the unit tests to account for this (better)

* Rewrite extpp

500 sLOC => ~350 sLOC.

Mostly rewrote external preprocessor so it now supports outputting functions that use the non-legacy form of creating functions now.

My eventual plan is to deprecate the preprocessor as a whole but it's nice that having this allows for extensions to be ported over to the new system automatically.

Additionally rewrote e2functions using the varargs to use ...<name> instead.

* Actually define the parameters

Got too used to using registerOperator which actually passes the arguments to the lua function directly rather than through a table as functions do.

I think it'd be possible to do that for functions but it'd require a lot of code duplication to be efficient. Maybe it could be looked into in the future.

* Fix extpp operator implementation

Last commit ironically broke operators since it does have the opposite implementation that passes arguments directly to the lua function.

* Fix changed() functions

Still a hacky function. Luckily works since we pass the type table which is compile time (and changed just needs an identifiable compile time index).

Made it cost 5 ops from 1 since you should not be using this function often if at all and I'd really love to remove it.

* Discard optimizations

* Fixed ioGetOutput
* Added optimized compile outputs with For and Foreach loops in case the key is a discard (_).

* Fully implement stringcalls

Still no caching

* Implemented string calls for builtin functions
* Implemented string calls for methods
* Fix string call parsing error

* Some fixes

* Fix event error that would normally prevent scoping events firing with includes (which the top level is scope 1, not the global scope).

* Fix one last use of incorrect 'variadic' variable

* Fix entity indexing returning nil when not found

* Add traces to quota and internal errors

Lua errors wouldn't point to an E2 trace or anything since they didn't have a trace attached to them. The trace is stored in the context however, which the entity has access to.

So now quota errors and internal lua errors by extension developers will point you toward whichever function caused it.

* Reimplement some warnings & Tidy code

* Reimplemented "use before initialization" warning.
* Rewrote nodiscard and deprecation warnings implementation to a less hacky method.
* Make nested event a warning as to not break backwards compat for a bit. Will make it an error in the future but I don't want to break more code.

* Removed progress comments. Redundant and haven't updated them in a while.

* Initialize variable on assignment

Forgot this

Also made the editor warning/error list traces more obvious as to being a trace. ([%d:%d] -> [line %u, char %u])

* Don't trigger non-table types

Assignment to global variables was trying to have the lookup behavior which was previously restricted to table-like types for I/O on any type, which was erroring in case of an entity.

* Missed a spot

There was another location where the previous commit's issue could be reproduced.

Also fixed an internal error on using deprecated methods.

* Fix wirelink indexget

* Fix preprocessor errors

* Fix sign and include variables

* Fix sign function which used its arguments incorrectly

* Fix incorrect runtime scoping of includes and removed Context:IsolatedScope

* Rework extpp some more

* Condensed to 300 lines (shaved off 70 lines)
* Make variadic parameters not use legacy functions.
* Revert change making printed strings quoted

* Remove E2Lib.Env.Types

These changes shouldn't be included in this PR. Will be done later with reworking the api.

* Remove support for single quat/complex literals

You can no longer have `i`, `j` and `k` act as valid expressions. This is because it was causing a parsing error for methods.

This is a breaking change but I really doubt anyone used this behavior, and it is silly. Additionally I've had a warning for using them since I rewrote the tokenizer, so if anyone was (and still actively keeps up with E2), they would know to change to 1k/1j/1i.

This would fix #2544

* Fix syntax error

This popped up when fixing merge conflicts

* Fixes & New tests

* Added a bunch more integration tests for E2, this time focusing on the runtime being correct.

* Fixed switch case fallthrough not existing (probably has been broken since my PR to fix the scoping issue and nobody's noticed)

* Fix issue with for loop break bleeding into other for loops, preventing them from running

* Fix the same thing above but switch case leaking into other things

* Fix parsing test case by removing use of quaternion/complex literal syntax that has been removed.

* Remove effect "assignment" operator.

* Fix event parameter checking being messed up with the event placeholder pr.

* Slightly optimize for loops more by using compile time known number of iterations (microoptimization)

There's still a bunch of E2s that behave very strangely. Investigating this is a huge pain

* Fix void return and add more function test cases

Thanks to h2n9 reporting this on discord

* Parity and a lot more tests

Added a lot more test cases.

Now foreach defaults to strings when iterating over tables as it did prior.

* Fixes & Cleanup

* `if` statement now properly uses `operator_is` as it was always meant to. It bypasses this for number and string, however, as an optimization.

* Fix issue with calls that contained nil values that broke `ipairs` by not using ipairs (potential speedup)

* Fixes, parity & test cases

* `types` param in extpp functions -> `typeids` as it was before
* Fixed if statement logic for when to skip using operator_is (and added test case)
* Add testcase for variadic extpp functions (testing printColor and print)
* Add test case to ensure array in array won't compile.

* Fixes & Parity

* Fix issue with method calls causing internal errors
* Fix assertion ensuring void isn't used as method type
* Add some left over operators to removedoperators
* Make increment and decrement use the existing addition and subtraction operators with the type to number. So now doing V++ is a thing (where V is a vec()).
* Fix variadic parameter syntax sugar issues and not including meta type in `types` table passed to functions.
* Add check to ensure if operator_is works on strings
* Added recursion and variadic syntax sugar tests.

* Fix vararg issues

* Fix variadic udf syntax sugar with a `table` including the meta type in the table's typeids.
* Clip `typeids` to only include the variadic variables in extpp. Was not aware this was expected behavior.
* Add some more variadic function tests (format and select)

* `types` -> `typeids`

I changed the parameter output in extpp thinking nobody was using it, bad idea.

Last extpp fixes reverted that, so need this to fix changed().

* Fix exit() and reset() not resetting scope

This wasn't a problem in the past, since statements would pcall their inner contents and pop their scopes before erroring. Now that we don't do that, since exit() and reset() just trigger an error, it caused the E2 to desync between runtime and compile time scope as it'd never pop its scope.

Now the functions reset the scope to the global scope before erroring.

This got the garfield pipe E2 working.

* Fix events not compiling, Remove some legacy support

* Removed support for legacy "is" operators inside of the "or" and "and" operator since people seem to be okay with some breakages. Also I wasn't supporting it anywhere else, so.

* Fix issue with events not compiling probably due to some failed merge when pulling in the changes with event parameter names.

* Revert "is" operators now using registerOperator

They now use the e2function syntax since it supports the new operators, and will be more forwards compatible.

* Proper multiline traces

* Fix incorrect stitching

Stitched to node which caused very weird internal errors

* Ordered table keyvalue creation

Uses numeric for loops rather than pairs which is faster and will have a defined order of which keyvalues will be assigned at table creation, unlike before.

Fixes #2494

* Fix delta ($) and deprecated wirelink index method

* Fixed delta's implementation. Seems I didn't even properly implement it. Added a test for it and made tests initialize variables with defaults like a normal chip would.

* Fixed old deprecated way of indexing a wirelink ie `WL:number("Foo")` instead of `WL["Foo", number]`. Can't really test this without IO, but added a basic sanity test.

Now drunk combine works.

* Fix old dupes with dupevars missed in #2462

Didn't think about delta vars at the time. Types aren't stored with them, so any $Vector or $Angle was not being converted, and would error old E2s with them duped.

So this fix is unrelated to my changes on the compiler..

Also reverted vector and angle operations to use e2function syntax once again, and made some vector functions use Vector metamethods rather than pure lua operations (Dot/Cross/Normalized/Distance/Length)

* Add variadic string call support & Legacy support

String calls now check for legacy functions at runtime. Also supports variadic calls except for user defined syntax sugar functions (Doubt people were relying on string calls with that new feature, and hopefully string calls will be deprecated soon).

Added stringcall tests to enforce return type errors, ensure functions work through stringcalls.

* Cleaner E2Lib.compileScript

Use withStrict instead of manually defining throw function.

Add with(Inputs/Outputs/Persists/DeltaVars) methods to handle initializing variables to default values rather than doing it manually.

* Remove unused `run` param

* Slight tweaks

* Store scope before loop in foreach implementation to avoid indexing op each iteration
* Made variables and costless functions cost cheaper.

* Fix parser trace error

Seems I missed this one

* Remove assignment operator

* Fix assignment not reusing expression

Seems I got sidetracked while implementing this, and didn't properly make it re-use the final expression in assignment. This lead to simply doing `A = 2` evaluating the '2' expression twice, which is a big problem for function calls.

Added a regression test as well.

* String library fixes

* Make format use ...args syntax instead of ...
* Remove operator!=
* Make use of string.byte's index parameter rather than doing string.sub to get the character in byte foreach

* Add stringcall array restriction edge case

* Fix removed operators not preprocessing

Now it just generates a pointless lambda as to not cause syntax errors.

Additionally moved the check to the bottom of the error list since it's a warning rather than an error.

* Enforce new void representation

Void type is represented as just nil now instead of an empty string.

To enforce this, check for empty string inside of compiling exprcall.

Ideally the api just wouldn't pass an empty string now but stuff like e2doc depends on that afaik. Just easier to go about it this way for now.

* Variadic user function stringcall support

* Add stringcall method function test

* Rearrange tests & Add library tests

Now the runtime test suite is split into three parts

* Base: Testing that compiler things like if statements, while loops, etc work
* Libraries: Testing that libraries like `string` work as they are changed.
* Types: Testing that types function properly

* Temporary overriding return patch

Also added test. This code shouldn't have been uncommented, just need to handle that one case where fn.returns is expected to not be nil.

* Fix operator= causing errors

Add to RemovedOperators list and check RemovedOperators list before flagging something as an invalid function name

* Proper function override fix

* Fix incorrect error message on event error

Bad copy paste job gave a tick quota error inside of runtime errors in events

* Fix for loop continue bleeding

Also added test case to ensure this doesn't happen again. (And to explain the issue for me)

* Fix foreach not having break return and continue implemented

No idea how this didn't have any tests and wasn't found.

Also simplified for loop code

* Fix user function overriding, add override optimization

Fix user function overriding by properly looking up the function at runtime.

This would normally incur runtime overhead for all functions, but now the compiler tags functions to be `const` if they are never re-declared anywhere else. This means we can get the best of both worlds with performance with or without function overrides.

As per usual, added tests

* Fix switch case early break

Also added a bunch more test cases

* Fix nil argument stopping udf arguments

Was using ipairs internally, which would fail on a table with a gap caused by a nil argument.

* Reimplement runtime undefined function error

* Fix keyvalue calls not accepting identifiers

This is an issue that's existed for 10+ years on the old compiler but nobody has found it since it's pretty niche to even use the key value table/array constructor functionality.

Hacky fix but couldn't think of any other way. This is the fault of two things: E2 not having braces like lua does for expressions in these kv constructors, and E2 using the assignment operator inside them as well. So not much we can do about it besides this.

Added test to ensure it works correctly.

* Fix dupes

Wasn't converting the typeid back to non-type id form for the duplicator

* Fix while loop continue

While loops weren't being tagged as loop (which evaluating blocks uses to determine whether to look for continue/break/etc or not)

While also had the continue scope bleeding issue that for/foreach had patched a bit ago.

* Add stackdepth to runtimecontext builder

* notify for syntax errors again

* Fix switch fallthrough running same case twice

Fixes https://github.com/Vurv78/wire/issues/12

* Add default == operator in case it is absent

One goal of this PR is to minimize the amount of third party code needed in E2 extensions.

Now the equals operator can be omitted if the implementation intends to be a simple lua equals operation.

This not only saves on needing to implement a redundant function, relying on extension authors to maintain this, but leaves room for optimizations knowing that the operator is "simple", like compiling switch case to a lookup table of lambdas, which wouldn't be possible if the equals operator was overloaded since it'd use table indexing with the value.

Additionally added more test cases.

This behavior may also come to operator_is in the future. For now, since overloading it with valid nonsimple functions is common, it will stay necessary.

* Don't store scope in VarData

Was only using the scope field for the depth. Storing it in VarData lead to cyclic reference which couldn't be serialized in JSON, breaking dupes.

* Rewrite compound operator, decrement/increment operators

This fixes the case of compound operators alongside increment/decrement not working with types that had legacy operators.

Now these will simply act as syntax sugar for assignment and use the operators internally, now that I've removed the compound assignment operators.

It simplifies the compiler implementation of these significantly and deduplicates a lot of code regarding variable usage

* Remove redundant "is" return enforcement

Enforce that no operator is registered named "is" that returns a type other than "n", and remove redundant runtime enforcement of this in Not operator compilation

* ExtPP tweaks

Trim string before gsub, and remove extra argument passed to parseParameters

* Highlight and autocomplete user defined functions

* Fix string and byte foreach, add tests

They were cutting off one char/byte early

* Add explicit catch error typing

Adds explicit error typing to the error variable in try catch, with a warning if not present.

Added highlighting and test case for this.

* Change nontable foreach warning message

* Fix ExtPP generating trailing whitespace for aliases

Fixes #2667

* Split parsing test into several files

* Deprecate E2Lib.raiseException

Deprecate E2Lib.raiseException in favor of new function, RuntimeContext:forceThrow.

Now only in the case of non-strict, a runtime context's throw function is overridden with one that returns the default value, otherwise it inherits the function from its metatable.

forceThrow uses this inherited function.

Added coverage for early returns to unit test suite, change __return__ check position

Removed old string function implementation (completely scrapped for raw compiler implementation)

* Fix precedence of certain operators

Optimizing the parser led me to accidentally mess up the precedence of certain operators.

Specifically | should've had less precedence than &, but this was not true, they had the same precedence.

Added test cases for these operators.

Also optimized delta operator since the scope will always be the global scope.

* Remove preprocessor attribution

Incorrect and I'd rather not be connected to that

* Optimize runtime scoping

Remove SaveScopes and LoadScopes functions, inline their contents to avoid allocation of a redundant table and the function overhead.

* Fix preprocessor error causing internal error

---------

Co-authored-by: Fasteroid <29342750+Fasteroid@users.noreply.github.com>
2023-10-01 09:45:43 -07:00
Vurv
0c86173f8c
Fix discard precedence (#2526)
Constants are so incredibly niche I forgot to include them in the integration tests.
2023-01-04 14:28:22 -08:00
Vurv
f9f12eec51
E2 discard (_) parameters (#2500)
* Implement discard parameters

* Support for for, foreach, try

Additionally, the warning message is now different based on whether you can use _ to ignore the value.

* Add parse test cases
2023-01-01 13:03:17 -08:00
Vurv
6e0212bd43
Add events to Expression 2 (#2452)
* Implement editor support + Parameters

* Network events to client
* Editor highlighting
* Add fileLoaded and fileErrored events

* Custom eventname color

* Add deprecation messages and more events

* Refactor hideChat and modifyChat for event system

Code is much less complicated now. Checks if you're the author of the message before letting you set the variable at all.

And now it works with the event system.

* Cleanup

* Implement noreturn for methods
* Implement one event per-file system

* Implement event autocompletion

* More events, autocomplete colors, constructors

* Added playerSpawn, playerDeath, playerConnected, playerDisconnected, chipUsed events
* Color code autocompletions by their type.
* Add keyword autocompletion
* Add constructor/destructor system so that events can create and remove hooks as necessary per chip.

* Deprecate clk versions
2022-11-26 20:32:13 -08:00
Vurv
d78116693b
E2 Fixes (#2488)
* Fix #2481 and add regression test
* Fix #2486 by refactoring `->` and `~` operators to use GetVariableType
* Add regression test for #2465 I missed
2022-11-16 08:30:49 -08:00
Vurv
a7312c2748
Fix vector/angle multiplication (#2422)
Also added unit tests. Broke because the add/sub operators only accept angles yet the mul/div operators only accept numbers.. amazing

Additionally made the E2Lib.compileScript function return a human-friendly error in case a trace is returned from a runtime error (usual case).
2022-09-10 20:32:23 -07:00
Vurv
bdac34a112
Tokenizer Rewrite (#2406)
* Rewrite the tokenizer

This shouldn't change any common behavior for E2 users but makes the tokenizer *much* faster and simpler

The only changed behavior would be no longer allowing usage of booleans (true / false), and now allowing void to be used as a function name (same as the rest of the types, since it is no longer a keyword)

* Remove debug code

* Fix empty return case

Also added to the parsing unit test

* Add complex/quaternion literals
2022-09-07 12:02:58 -07:00
Vurv
e3c5e59719
Add unit tests for E2 (#2400)
* Add unit tests for E2

Adds basic unit tests that can be invoked by the server console using ``e2test``. This imitates an E2 chip virtually by using the world entity as it's owner, and as such doesn't act 1:1, but it should be good enough for most cases.

You won't be able to use this with the workshop wiremod which shouldn't matter as you should really just be doing unit test / debugging on the github version anyways.

Additionally added a lot more tests to the parsing "test" and fixed 1975 not compiling

All of this works on a new E2Lib function, ``runScript``, which runs an E2 script independent of an entity

* Add test types

Added the ability to specify if a file should fail to execute, or fail to compile.

So now parsing.txt functions with the input operators. Also added a switch case test to ensure there aren't any regressions from #2388

* Tidy code

Removed unused return value and trailing whitespace

* Add regression test for #2403 and fix tests

* Added a regression test for #2403, removed "switch" test in favor of a regression test for #2388

* Made unit test code logic a lot less needlessly complex

* Restrict use to superadmins

Didn't realize for some reason concommands registered on the server work for clients.. made it only allow superadmins if not invoked by the server console.

Also made "e2tests.lua" only be included on SERVER to avoid a lua error
2022-09-04 16:30:32 -07:00
Vurv
5fc05d58f9
Add try/catch stmt, @strict directive & runtime error handling to E2 (#2174)
* Add try/catch statement to E2.

Adds a try / catch statement to E2 that allows users to catch any errors that occur inside of the block, and if so, run the catch block, setting the variable name provided to a new local variable that contains the error string.

* Add not enough memory & table overflow to uncatchable

* Lot of combined changes

* Made ``exit`` skip the try catch block as a whole.
* Added ``@strict`` directive
* Refactored the directives a bit to add ``@strict``
* Added a ``throw`` function to registerFunction that either throws an error or returns the default value of the function inside using the type defined. You can override this with the second argument.
* Edited the preprocessor for this ^
* Edited the entire core/entity file to preview how this would look.
* Some copytype calls still existed and were replaced with ``E2Lib.fixDefault``

* Some missing ``throw``s

* self:throw and explicit returns

Throwing is much more explicit now. You call self:throw(msg, var) and var is returned if the chip isn't strict. Got rid of the throw hack to try and get the default type. The only problem here now is trying to get some debug info about where the error happened. Right now it's not very useful and isn't able to point to the function calling self:throw for some reason.

* Fix travis lint

Hardcoded the starting point for this line.  (#"trigger" + 2)

* Can only catch e2 errors & thrown errors

Native lua/glua error() will always error try and catch blocks for security reasons.

The only errors that will be caught are created by E2Lib.catchableError. (self.throw and e2's error)

Replaced context:throw with context.throw to be more memory efficient i guess.

* Throw compat with a lot more libraries

Added ``@strict`` compat to a lot more libraries. I think adding more would make this PR too big, it's already pretty huge.
Additionally made some code a bit more clean and optimized & used E2Lib.newE2Table more. (Notably with bone fwd up right)

* Fix compile errors

* Have compile traces be sent to runtime

* E2 instances/contexts now have a ``trace`` field, which contains the latest trace from EvaluateStatement & the ``seq`` instruction. Thrown errors will now show as something like ``Expression 2 (generic): Runtime error 'Invalid physics object!' at line 10, char 16``.

* Fixed some compiler quirks (Some functions called from the Compiler global object instead of self, which wouldn't allow for multiple compilers iirc)

* Also added Compiler:CallInstruction which does self["Instr"...] etc for you
* Localized string.upper use

* Fix udf errors

* Make stringcalls able to be caught

Forgot to re-implement this since we opted for a whitelisted error instead.

* Fix some core error

* Add strict test to unit tests, fix exit, assert

* Add a ``strict.txt`` unit test to data/expression2/tests.
* Also add a small try/catch test to ``parsing.txt``

* Fix assert() e2function not throwing a catchable error. It now provides a runtime src position like the rest of the errors

* Fix exit() not exiting the chip. The ``Skip`` table was just making exit() exit the try / catch block rather than passing it to the error handler to exit the whole chip.

* Fix #include, Runtime traces

* Include was messed up because whoever implemented it didn't think about needing the scope after erroring. It didn't load the old scope back which messed up trying to use try/catch.

* Fixed runtime traces because before I tried to make it only take from the seq instruction which would always make the error come from where the first instruction is.

* Renamed E2Lib.catchableError to raiseException to be more clear. Also has a 4th param for whether it's catchable, that is default true.

* Added E2Lib.unpackException(struct) which is a boilerplate bridge function for when you want backwards compatibility with error messages & error structs in pcalls.

* Remove debug stuff

* Squashed commit of the following:

commit 02d5dfd0ae
Author: stepa2 <sssstepa452@yandex.ru>
Date:   Thu Jul 29 20:48:37 2021 +0300

    Implemented do-while statement for E2 (issue #2191) (#2193)

    * Allow calling entity:setPos and entity:setAng on entities without collisions
    Also allow calling entity:propManipulate, but position and rotation is only updated

    * Update prop.lua

    Removed tailing whitespace, fixed luacheck warning

    * Implemented do-while statement for E2 (#2191)

    * Removed debug messages I fogot to remove, added parsing tests, disabled bugged optimization

    * Linting pass

commit 73b4dbf208
Author: thegrb93 <grbrown93@sbcglobal.net>
Date:   Sun Jul 25 00:52:11 2021 -0400

    Fix explodeRE (#2204)

commit 6a5d4a214f
Author: Derpius <49565664+Derpius@users.noreply.github.com>
Date:   Sat Jul 24 23:58:13 2021 +0100

    Add 4th option to view request bypass (#2198)

    * Add 4th option to view request bypass

    * Update comment

commit adca748833
Author: thegrb93 <grbrown93@sbcglobal.net>
Date:   Sat Jul 24 18:57:53 2021 -0400

    Fix light sprite size (#2201)

    * Fix light sprite size

    * Revert sizing code. Also simplified because you can't even see the smaller sprites

    * Made spritesize an option/input

    * Add back tube light sprites

    * Add missing clamp

commit c4d333aef3
Author: Divran <arviddivran@gmail.com>
Date:   Sat Jul 24 23:19:26 2021 +0200

    fixed crash exploit

commit 966b1e4dc9
Author: AbigailBuccaneer <AbigailBuccaneer@users.noreply.github.com>
Date:   Sat Jul 17 15:09:26 2021 +0100

    Restore expression2 ghost functionality

    Commit ec99005dd4 accidentally prevent the
    E2 tool from showing ghosts.

commit 01c36ae2aa
Merge: f85c234d 1a1ddd36
Author: Divran <arviddivran@gmail.com>
Date:   Tue Jul 13 22:54:54 2021 +0200

    Merge pull request #2197 from sammyt291/patch-1

    Light sprite colors and size

commit 1a1ddd36a4
Author: sammyt291 <samuel_canning@hotmail.co.uk>
Date:   Sun Jul 11 20:07:42 2021 +0100

    Light sprite colors and size

    Changed the smaller 3 sprites on the tubular light to match the color of the main sprite on the light
    Allowed the sprite overlay to be sized according to the size slider already present, clamped to 128 to avoid changing existing contraptions with large-size glows enabled.

commit f85c234d77
Author: stepa2 <sssstepa452@yandex.ru>
Date:   Sat Jul 3 03:35:04 2021 +0300

    Allow calling entity:setPos and entity:setAng on entities without collisions (#2188)

    * Allow calling entity:setPos and entity:setAng on entities without collisions
    Also allow calling entity:propManipulate, but position and rotation is only updated

    * Update prop.lua

    Removed tailing whitespace, fixed luacheck warning

commit 860604bbb9
Author: CoreyLee Hassell <Anticept@users.noreply.github.com>
Date:   Fri Jul 2 20:34:47 2021 -0400

    Revert "Delete json funcs until https://github.com/Facepunch/garrysmod-issues/issues/4976 is fixed (#2177)" (#2185)

    This reverts commit c6ea454243.

commit eb3841f19c
Author: thegrb93 <grbrown93@sbcglobal.net>
Date:   Fri Jun 25 19:50:34 2021 -0400

    Improve regex and fix pcall not returning (#2186)

    * Improve regex and fix pcall not returning

    * Don't use %b

commit 1ef0f63dd1
Author: thegrb93 <grbrown93@sbcglobal.net>
Date:   Thu Jun 24 02:25:28 2021 -0400

    Add level setting, also play regardless of distance/location (#2183)

commit ec99005dd4
Author: 100PXSquared <100pxsquared@gmail.com>
Date:   Wed Jun 23 08:01:26 2021 +0100

    E2 View Requests System (#2157)

    * Add E2 view requests system

    * Prevent prop protection stopping view requests

    * Perform CanTool check if chip owner is invalid

    * Refactor checks in TOOL:Think

    * Add SteamID whitelist convar to view requests

    * Fix request answer netmsg validation

    * Add EoF newline

    * Refactor E2 view requests

    * Replace bypass list with bypass mode convar

    * Implement allow always

    * Replace checks in remote updater code request

    * Fix copy paste induced bug

commit f888eb12e7
Author: thegrb93 <grbrown93@sbcglobal.net>
Date:   Wed Jun 23 02:39:23 2021 -0400

    Fix turret behavior if parented (#2184)

commit f8335f9711
Author: thegrb93 <grbrown93@sbcglobal.net>
Date:   Fri Jun 18 11:35:20 2021 -0400

    Limit regex cpu consumption (#2181)

    * Limit regex cpu consumption. Fixes: #2063 Related (https://github.com/Facepunch/garrysmod-requests/issues/1878)

    * Better error message

commit d0a84b700f
Author: Divran <arviddivran@gmail.com>
Date:   Fri Jun 18 00:44:56 2021 +0200

    sound duration is very expensive

commit f54ff0a120
Author: Divran <arviddivran@gmail.com>
Date:   Fri Jun 18 00:13:01 2021 +0200

    fixed auto indenting

    auto un-indenting after typing } was broken (triggered even if the same
    line contained a {), and did not add itself to the undo table, so could
    not be undone.
    auto indenting after typing { was also broken if the same line contained
    a }

    adjusted digi screen bandwidth
    slowed down clientside digi screen rendering to prevent fps lag

    fixed hologram queues after last update broke it

commit 4855366a2f
Author: Divran <arviddivran@gmail.com>
Date:   Wed Jun 16 18:58:31 2021 +0200

    digi screen max global bandwidth

commit 7b5ac40cb9
Author: Divran <arviddivran@gmail.com>
Date:   Wed Jun 16 02:18:54 2021 +0200

    improved digi screen performance

    added cpu time based usage checks serverside
    and coroutine processing clientside
    hopefully this is (at least v1 of) the final strip of duct tape for this
    notorious entity

commit d8cdc76bcb
Author: Divran <arviddivran@gmail.com>
Date:   Tue Jun 15 22:37:39 2021 +0200

    fixed multiple crash and lag exploits

    reset() no longer resets e2 quotas
    emergency ram shutdown now also clears gtable memory
    hologram scale/clip update rate limited to 0.1s and opcost increased to
    30
    and a few others

commit c6ea454243
Author: thegrb93 <grbrown93@sbcglobal.net>
Date:   Sat Jun 12 19:13:26 2021 -0400

    Delete json funcs until https://github.com/Facepunch/garrysmod-issues/issues/4976 is fixed (#2177)

* Revert "Squashed commit of the following:"

This reverts commit 9f0b2f0e46.

* Return keyword

* Fixed expressions being missing thanks to missing return keyword
* Removed useless local variable in var operator

* Remove unused test code

Not a bug or anything but something I randomly came across..
2021-08-14 17:38:35 -04:00
stepa2
02d5dfd0ae
Implemented do-while statement for E2 (issue #2191) (#2193)
* Allow calling entity:setPos and entity:setAng on entities without collisions
Also allow calling entity:propManipulate, but position and rotation is only updated

* Update prop.lua

Removed tailing whitespace, fixed luacheck warning

* Implemented do-while statement for E2 (#2191)

* Removed debug messages I fogot to remove, added parsing tests, disabled bugged optimization

* Linting pass
2021-07-29 13:48:37 -04:00
AbigailBuccaneer
2678c4c3ff Add some tests for E2
Currently the tests are quite minimal: a test for the E2 parser, and a
regression test for #1975. Soon there will be a test harness command
that runs all the test E2s and watches for errors.
2019-09-23 22:39:13 +01:00
AbigailBuccaneer
9ae1f5504f Fix all whitespace errors
Now nobody's commit should be flagged by Travis unless it actually
introduces whitespace errors (which we do want to check for).

This doesn't fully sort out indentation everywhere, but does fix all
whitespace that Git doesn't like. You can check the whitespace across
the entire codebase with:

    git diff-tree --check "$(git hash-object -t tree /dev/null)" HEAD
2018-02-02 00:02:28 +00:00
AbigailBuccaneer
03972803dd Normalize line endings. 2013-07-28 15:51:47 +01:00
Nebual
efe065dea8 Renamed all files to lowercase, for Workshop compatibility 2013-07-12 00:55:14 -07:00