From ec681eb97a5ab88c0a8e6c77d08b694629c6e119 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Sun, 18 Aug 2024 21:39:23 +0200 Subject: [PATCH] grammar and small changes --- .antlr/Wyst.interp | 2 +- .antlr/WystParser.java | 236 +++++++++++++++++++++-------------------- Wyst.g4 | 29 ++++- go.mod | 1 - src/main.go | 94 ++-------------- src/transpiler.go | 1 - test/main.wst | 3 +- 7 files changed, 158 insertions(+), 208 deletions(-) diff --git a/.antlr/Wyst.interp b/.antlr/Wyst.interp index cdc1b69..72adc43 100644 --- a/.antlr/Wyst.interp +++ b/.antlr/Wyst.interp @@ -44,4 +44,4 @@ top atn: -[4, 1, 14, 85, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 1, 0, 1, 0, 1, 0, 1, 0, 5, 0, 21, 8, 0, 10, 0, 12, 0, 24, 9, 0, 1, 0, 3, 0, 27, 8, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 5, 4, 48, 8, 4, 10, 4, 12, 4, 51, 9, 4, 1, 4, 1, 4, 1, 5, 1, 5, 1, 5, 5, 5, 58, 8, 5, 10, 5, 12, 5, 61, 9, 5, 1, 6, 1, 6, 1, 6, 1, 6, 3, 6, 67, 8, 6, 1, 6, 1, 6, 5, 6, 71, 8, 6, 10, 6, 12, 6, 74, 9, 6, 1, 6, 1, 6, 1, 7, 1, 7, 5, 7, 80, 8, 7, 10, 7, 12, 7, 83, 9, 7, 1, 7, 0, 0, 8, 0, 2, 4, 6, 8, 10, 12, 14, 0, 1, 2, 0, 10, 12, 14, 14, 85, 0, 16, 1, 0, 0, 0, 2, 30, 1, 0, 0, 0, 4, 35, 1, 0, 0, 0, 6, 38, 1, 0, 0, 0, 8, 42, 1, 0, 0, 0, 10, 54, 1, 0, 0, 0, 12, 62, 1, 0, 0, 0, 14, 81, 1, 0, 0, 0, 16, 17, 5, 1, 0, 0, 17, 22, 3, 4, 2, 0, 18, 19, 5, 2, 0, 0, 19, 21, 3, 4, 2, 0, 20, 18, 1, 0, 0, 0, 21, 24, 1, 0, 0, 0, 22, 20, 1, 0, 0, 0, 22, 23, 1, 0, 0, 0, 23, 26, 1, 0, 0, 0, 24, 22, 1, 0, 0, 0, 25, 27, 5, 2, 0, 0, 26, 25, 1, 0, 0, 0, 26, 27, 1, 0, 0, 0, 27, 28, 1, 0, 0, 0, 28, 29, 5, 3, 0, 0, 29, 1, 1, 0, 0, 0, 30, 31, 5, 12, 0, 0, 31, 32, 5, 12, 0, 0, 32, 33, 3, 0, 0, 0, 33, 34, 3, 12, 6, 0, 34, 3, 1, 0, 0, 0, 35, 36, 5, 12, 0, 0, 36, 37, 5, 12, 0, 0, 37, 5, 1, 0, 0, 0, 38, 39, 3, 4, 2, 0, 39, 40, 5, 4, 0, 0, 40, 41, 3, 10, 5, 0, 41, 7, 1, 0, 0, 0, 42, 43, 5, 5, 0, 0, 43, 44, 5, 12, 0, 0, 44, 49, 3, 10, 5, 0, 45, 46, 5, 2, 0, 0, 46, 48, 3, 10, 5, 0, 47, 45, 1, 0, 0, 0, 48, 51, 1, 0, 0, 0, 49, 47, 1, 0, 0, 0, 49, 50, 1, 0, 0, 0, 50, 52, 1, 0, 0, 0, 51, 49, 1, 0, 0, 0, 52, 53, 5, 6, 0, 0, 53, 9, 1, 0, 0, 0, 54, 59, 7, 0, 0, 0, 55, 56, 5, 13, 0, 0, 56, 58, 3, 10, 5, 0, 57, 55, 1, 0, 0, 0, 58, 61, 1, 0, 0, 0, 59, 57, 1, 0, 0, 0, 59, 60, 1, 0, 0, 0, 60, 11, 1, 0, 0, 0, 61, 59, 1, 0, 0, 0, 62, 72, 5, 7, 0, 0, 63, 67, 3, 10, 5, 0, 64, 67, 3, 4, 2, 0, 65, 67, 3, 6, 3, 0, 66, 63, 1, 0, 0, 0, 66, 64, 1, 0, 0, 0, 66, 65, 1, 0, 0, 0, 67, 68, 1, 0, 0, 0, 68, 69, 5, 6, 0, 0, 69, 71, 1, 0, 0, 0, 70, 66, 1, 0, 0, 0, 71, 74, 1, 0, 0, 0, 72, 70, 1, 0, 0, 0, 72, 73, 1, 0, 0, 0, 73, 75, 1, 0, 0, 0, 74, 72, 1, 0, 0, 0, 75, 76, 5, 8, 0, 0, 76, 13, 1, 0, 0, 0, 77, 80, 3, 8, 4, 0, 78, 80, 3, 2, 1, 0, 79, 77, 1, 0, 0, 0, 79, 78, 1, 0, 0, 0, 80, 83, 1, 0, 0, 0, 81, 79, 1, 0, 0, 0, 81, 82, 1, 0, 0, 0, 82, 15, 1, 0, 0, 0, 83, 81, 1, 0, 0, 0, 8, 22, 26, 49, 59, 66, 72, 79, 81] \ No newline at end of file +[4, 1, 14, 87, 2, 0, 7, 0, 2, 1, 7, 1, 2, 2, 7, 2, 2, 3, 7, 3, 2, 4, 7, 4, 2, 5, 7, 5, 2, 6, 7, 6, 2, 7, 7, 7, 1, 0, 1, 0, 1, 0, 1, 0, 5, 0, 21, 8, 0, 10, 0, 12, 0, 24, 9, 0, 1, 0, 3, 0, 27, 8, 0, 3, 0, 29, 8, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 1, 2, 1, 3, 1, 3, 1, 3, 1, 3, 1, 4, 1, 4, 1, 4, 1, 4, 1, 4, 5, 4, 50, 8, 4, 10, 4, 12, 4, 53, 9, 4, 1, 4, 1, 4, 1, 5, 1, 5, 1, 5, 5, 5, 60, 8, 5, 10, 5, 12, 5, 63, 9, 5, 1, 6, 1, 6, 1, 6, 1, 6, 3, 6, 69, 8, 6, 1, 6, 1, 6, 5, 6, 73, 8, 6, 10, 6, 12, 6, 76, 9, 6, 1, 6, 1, 6, 1, 7, 1, 7, 5, 7, 82, 8, 7, 10, 7, 12, 7, 85, 9, 7, 1, 7, 0, 0, 8, 0, 2, 4, 6, 8, 10, 12, 14, 0, 1, 2, 0, 10, 12, 14, 14, 88, 0, 16, 1, 0, 0, 0, 2, 32, 1, 0, 0, 0, 4, 37, 1, 0, 0, 0, 6, 40, 1, 0, 0, 0, 8, 44, 1, 0, 0, 0, 10, 56, 1, 0, 0, 0, 12, 64, 1, 0, 0, 0, 14, 83, 1, 0, 0, 0, 16, 28, 5, 1, 0, 0, 17, 22, 3, 4, 2, 0, 18, 19, 5, 2, 0, 0, 19, 21, 3, 4, 2, 0, 20, 18, 1, 0, 0, 0, 21, 24, 1, 0, 0, 0, 22, 20, 1, 0, 0, 0, 22, 23, 1, 0, 0, 0, 23, 26, 1, 0, 0, 0, 24, 22, 1, 0, 0, 0, 25, 27, 5, 2, 0, 0, 26, 25, 1, 0, 0, 0, 26, 27, 1, 0, 0, 0, 27, 29, 1, 0, 0, 0, 28, 17, 1, 0, 0, 0, 28, 29, 1, 0, 0, 0, 29, 30, 1, 0, 0, 0, 30, 31, 5, 3, 0, 0, 31, 1, 1, 0, 0, 0, 32, 33, 5, 12, 0, 0, 33, 34, 5, 12, 0, 0, 34, 35, 3, 0, 0, 0, 35, 36, 3, 12, 6, 0, 36, 3, 1, 0, 0, 0, 37, 38, 5, 12, 0, 0, 38, 39, 5, 12, 0, 0, 39, 5, 1, 0, 0, 0, 40, 41, 3, 4, 2, 0, 41, 42, 5, 4, 0, 0, 42, 43, 3, 10, 5, 0, 43, 7, 1, 0, 0, 0, 44, 45, 5, 5, 0, 0, 45, 46, 5, 12, 0, 0, 46, 51, 3, 10, 5, 0, 47, 48, 5, 2, 0, 0, 48, 50, 3, 10, 5, 0, 49, 47, 1, 0, 0, 0, 50, 53, 1, 0, 0, 0, 51, 49, 1, 0, 0, 0, 51, 52, 1, 0, 0, 0, 52, 54, 1, 0, 0, 0, 53, 51, 1, 0, 0, 0, 54, 55, 5, 6, 0, 0, 55, 9, 1, 0, 0, 0, 56, 61, 7, 0, 0, 0, 57, 58, 5, 13, 0, 0, 58, 60, 3, 10, 5, 0, 59, 57, 1, 0, 0, 0, 60, 63, 1, 0, 0, 0, 61, 59, 1, 0, 0, 0, 61, 62, 1, 0, 0, 0, 62, 11, 1, 0, 0, 0, 63, 61, 1, 0, 0, 0, 64, 74, 5, 7, 0, 0, 65, 69, 3, 10, 5, 0, 66, 69, 3, 4, 2, 0, 67, 69, 3, 6, 3, 0, 68, 65, 1, 0, 0, 0, 68, 66, 1, 0, 0, 0, 68, 67, 1, 0, 0, 0, 69, 70, 1, 0, 0, 0, 70, 71, 5, 6, 0, 0, 71, 73, 1, 0, 0, 0, 72, 68, 1, 0, 0, 0, 73, 76, 1, 0, 0, 0, 74, 72, 1, 0, 0, 0, 74, 75, 1, 0, 0, 0, 75, 77, 1, 0, 0, 0, 76, 74, 1, 0, 0, 0, 77, 78, 5, 8, 0, 0, 78, 13, 1, 0, 0, 0, 79, 82, 3, 8, 4, 0, 80, 82, 3, 2, 1, 0, 81, 79, 1, 0, 0, 0, 81, 80, 1, 0, 0, 0, 82, 85, 1, 0, 0, 0, 83, 81, 1, 0, 0, 0, 83, 84, 1, 0, 0, 0, 84, 15, 1, 0, 0, 0, 85, 83, 1, 0, 0, 0, 9, 22, 26, 28, 51, 61, 68, 74, 81, 83] \ No newline at end of file diff --git a/.antlr/WystParser.java b/.antlr/WystParser.java index 212e6fa..32f813f 100644 --- a/.antlr/WystParser.java +++ b/.antlr/WystParser.java @@ -116,37 +116,45 @@ public class WystParser extends Parser { { setState(16); match(T__0); - setState(17); - var_def(); - setState(22); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,0,_ctx); - while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { - if ( _alt==1 ) { - { - { - setState(18); - match(T__1); - setState(19); - var_def(); - } - } - } - setState(24); - _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,0,_ctx); - } - setState(26); + setState(28); _errHandler.sync(this); _la = _input.LA(1); - if (_la==T__1) { + if (_la==IDENTIFIER) { { - setState(25); - match(T__1); + setState(17); + var_def(); + setState(22); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,0,_ctx); + while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { + if ( _alt==1 ) { + { + { + setState(18); + match(T__1); + setState(19); + var_def(); + } + } + } + setState(24); + _errHandler.sync(this); + _alt = getInterpreter().adaptivePredict(_input,0,_ctx); + } + setState(26); + _errHandler.sync(this); + _la = _input.LA(1); + if (_la==T__1) { + { + setState(25); + match(T__1); + } + } + } } - setState(28); + setState(30); match(T__2); } } @@ -185,13 +193,13 @@ public class WystParser extends Parser { try { enterOuterAlt(_localctx, 1); { - setState(30); - match(IDENTIFIER); - setState(31); - match(IDENTIFIER); setState(32); - round_def(); + match(IDENTIFIER); setState(33); + match(IDENTIFIER); + setState(34); + round_def(); + setState(35); code_block(); } } @@ -224,9 +232,9 @@ public class WystParser extends Parser { try { enterOuterAlt(_localctx, 1); { - setState(35); + setState(37); match(IDENTIFIER); - setState(36); + setState(38); match(IDENTIFIER); } } @@ -261,11 +269,11 @@ public class WystParser extends Parser { try { enterOuterAlt(_localctx, 1); { - setState(38); - var_def(); - setState(39); - match(T__3); setState(40); + var_def(); + setState(41); + match(T__3); + setState(42); expr(); } } @@ -302,29 +310,29 @@ public class WystParser extends Parser { try { enterOuterAlt(_localctx, 1); { - setState(42); - match(T__4); - setState(43); - match(IDENTIFIER); setState(44); + match(T__4); + setState(45); + match(IDENTIFIER); + setState(46); expr(); - setState(49); + setState(51); _errHandler.sync(this); _la = _input.LA(1); while (_la==T__1) { { { - setState(45); + setState(47); match(T__1); - setState(46); + setState(48); expr(); } } - setState(51); + setState(53); _errHandler.sync(this); _la = _input.LA(1); } - setState(52); + setState(54); match(T__5); } } @@ -369,7 +377,7 @@ public class WystParser extends Parser { int _alt; enterOuterAlt(_localctx, 1); { - setState(54); + setState(56); _la = _input.LA(1); if ( !((((_la) & ~0x3f) == 0 && ((1L << _la) & 23552L) != 0)) ) { _errHandler.recoverInline(this); @@ -379,23 +387,23 @@ public class WystParser extends Parser { _errHandler.reportMatch(this); consume(); } - setState(59); + setState(61); _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,3,_ctx); + _alt = getInterpreter().adaptivePredict(_input,4,_ctx); while ( _alt!=2 && _alt!=org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER ) { if ( _alt==1 ) { { { - setState(55); + setState(57); match(MATH); - setState(56); + setState(58); expr(); } } } - setState(61); + setState(63); _errHandler.sync(this); - _alt = getInterpreter().adaptivePredict(_input,3,_ctx); + _alt = getInterpreter().adaptivePredict(_input,4,_ctx); } } } @@ -443,45 +451,45 @@ public class WystParser extends Parser { try { enterOuterAlt(_localctx, 1); { - setState(62); + setState(64); match(T__6); - setState(72); + setState(74); _errHandler.sync(this); _la = _input.LA(1); while ((((_la) & ~0x3f) == 0 && ((1L << _la) & 23552L) != 0)) { { { - setState(66); + setState(68); _errHandler.sync(this); - switch ( getInterpreter().adaptivePredict(_input,4,_ctx) ) { + switch ( getInterpreter().adaptivePredict(_input,5,_ctx) ) { case 1: { - setState(63); + setState(65); expr(); } break; case 2: { - setState(64); + setState(66); var_def(); } break; case 3: { - setState(65); + setState(67); var_def_set(); } break; } - setState(68); + setState(70); match(T__5); } } - setState(74); + setState(76); _errHandler.sync(this); _la = _input.LA(1); } - setState(75); + setState(77); match(T__7); } } @@ -523,23 +531,23 @@ public class WystParser extends Parser { try { enterOuterAlt(_localctx, 1); { - setState(81); + setState(83); _errHandler.sync(this); _la = _input.LA(1); while (_la==T__4 || _la==IDENTIFIER) { { - setState(79); + setState(81); _errHandler.sync(this); switch (_input.LA(1)) { case T__4: { - setState(77); + setState(79); asm(); } break; case IDENTIFIER: { - setState(78); + setState(80); func_def(); } break; @@ -547,7 +555,7 @@ public class WystParser extends Parser { throw new NoViableAltException(this); } } - setState(83); + setState(85); _errHandler.sync(this); _la = _input.LA(1); } @@ -565,55 +573,57 @@ public class WystParser extends Parser { } public static final String _serializedATN = - "\u0004\u0001\u000eU\u0002\u0000\u0007\u0000\u0002\u0001\u0007\u0001\u0002"+ + "\u0004\u0001\u000eW\u0002\u0000\u0007\u0000\u0002\u0001\u0007\u0001\u0002"+ "\u0002\u0007\u0002\u0002\u0003\u0007\u0003\u0002\u0004\u0007\u0004\u0002"+ "\u0005\u0007\u0005\u0002\u0006\u0007\u0006\u0002\u0007\u0007\u0007\u0001"+ "\u0000\u0001\u0000\u0001\u0000\u0001\u0000\u0005\u0000\u0015\b\u0000\n"+ - "\u0000\f\u0000\u0018\t\u0000\u0001\u0000\u0003\u0000\u001b\b\u0000\u0001"+ - "\u0000\u0001\u0000\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001\u0001"+ - "\u0001\u0001\u0002\u0001\u0002\u0001\u0002\u0001\u0003\u0001\u0003\u0001"+ - "\u0003\u0001\u0003\u0001\u0004\u0001\u0004\u0001\u0004\u0001\u0004\u0001"+ - "\u0004\u0005\u00040\b\u0004\n\u0004\f\u00043\t\u0004\u0001\u0004\u0001"+ - "\u0004\u0001\u0005\u0001\u0005\u0001\u0005\u0005\u0005:\b\u0005\n\u0005"+ - "\f\u0005=\t\u0005\u0001\u0006\u0001\u0006\u0001\u0006\u0001\u0006\u0003"+ - "\u0006C\b\u0006\u0001\u0006\u0001\u0006\u0005\u0006G\b\u0006\n\u0006\f"+ - "\u0006J\t\u0006\u0001\u0006\u0001\u0006\u0001\u0007\u0001\u0007\u0005"+ - "\u0007P\b\u0007\n\u0007\f\u0007S\t\u0007\u0001\u0007\u0000\u0000\b\u0000"+ - "\u0002\u0004\u0006\b\n\f\u000e\u0000\u0001\u0002\u0000\n\f\u000e\u000e"+ - "U\u0000\u0010\u0001\u0000\u0000\u0000\u0002\u001e\u0001\u0000\u0000\u0000"+ - "\u0004#\u0001\u0000\u0000\u0000\u0006&\u0001\u0000\u0000\u0000\b*\u0001"+ - "\u0000\u0000\u0000\n6\u0001\u0000\u0000\u0000\f>\u0001\u0000\u0000\u0000"+ - "\u000eQ\u0001\u0000\u0000\u0000\u0010\u0011\u0005\u0001\u0000\u0000\u0011"+ - "\u0016\u0003\u0004\u0002\u0000\u0012\u0013\u0005\u0002\u0000\u0000\u0013"+ - "\u0015\u0003\u0004\u0002\u0000\u0014\u0012\u0001\u0000\u0000\u0000\u0015"+ - "\u0018\u0001\u0000\u0000\u0000\u0016\u0014\u0001\u0000\u0000\u0000\u0016"+ - "\u0017\u0001\u0000\u0000\u0000\u0017\u001a\u0001\u0000\u0000\u0000\u0018"+ - "\u0016\u0001\u0000\u0000\u0000\u0019\u001b\u0005\u0002\u0000\u0000\u001a"+ - "\u0019\u0001\u0000\u0000\u0000\u001a\u001b\u0001\u0000\u0000\u0000\u001b"+ - "\u001c\u0001\u0000\u0000\u0000\u001c\u001d\u0005\u0003\u0000\u0000\u001d"+ - "\u0001\u0001\u0000\u0000\u0000\u001e\u001f\u0005\f\u0000\u0000\u001f "+ - "\u0005\f\u0000\u0000 !\u0003\u0000\u0000\u0000!\"\u0003\f\u0006\u0000"+ - "\"\u0003\u0001\u0000\u0000\u0000#$\u0005\f\u0000\u0000$%\u0005\f\u0000"+ - "\u0000%\u0005\u0001\u0000\u0000\u0000&\'\u0003\u0004\u0002\u0000\'(\u0005"+ - "\u0004\u0000\u0000()\u0003\n\u0005\u0000)\u0007\u0001\u0000\u0000\u0000"+ - "*+\u0005\u0005\u0000\u0000+,\u0005\f\u0000\u0000,1\u0003\n\u0005\u0000"+ - "-.\u0005\u0002\u0000\u0000.0\u0003\n\u0005\u0000/-\u0001\u0000\u0000\u0000"+ - "03\u0001\u0000\u0000\u00001/\u0001\u0000\u0000\u000012\u0001\u0000\u0000"+ - "\u000024\u0001\u0000\u0000\u000031\u0001\u0000\u0000\u000045\u0005\u0006"+ - "\u0000\u00005\t\u0001\u0000\u0000\u00006;\u0007\u0000\u0000\u000078\u0005"+ - "\r\u0000\u00008:\u0003\n\u0005\u000097\u0001\u0000\u0000\u0000:=\u0001"+ - "\u0000\u0000\u0000;9\u0001\u0000\u0000\u0000;<\u0001\u0000\u0000\u0000"+ - "<\u000b\u0001\u0000\u0000\u0000=;\u0001\u0000\u0000\u0000>H\u0005\u0007"+ - "\u0000\u0000?C\u0003\n\u0005\u0000@C\u0003\u0004\u0002\u0000AC\u0003\u0006"+ - "\u0003\u0000B?\u0001\u0000\u0000\u0000B@\u0001\u0000\u0000\u0000BA\u0001"+ - "\u0000\u0000\u0000CD\u0001\u0000\u0000\u0000DE\u0005\u0006\u0000\u0000"+ - "EG\u0001\u0000\u0000\u0000FB\u0001\u0000\u0000\u0000GJ\u0001\u0000\u0000"+ - "\u0000HF\u0001\u0000\u0000\u0000HI\u0001\u0000\u0000\u0000IK\u0001\u0000"+ - "\u0000\u0000JH\u0001\u0000\u0000\u0000KL\u0005\b\u0000\u0000L\r\u0001"+ - "\u0000\u0000\u0000MP\u0003\b\u0004\u0000NP\u0003\u0002\u0001\u0000OM\u0001"+ - "\u0000\u0000\u0000ON\u0001\u0000\u0000\u0000PS\u0001\u0000\u0000\u0000"+ - "QO\u0001\u0000\u0000\u0000QR\u0001\u0000\u0000\u0000R\u000f\u0001\u0000"+ - "\u0000\u0000SQ\u0001\u0000\u0000\u0000\b\u0016\u001a1;BHOQ"; + "\u0000\f\u0000\u0018\t\u0000\u0001\u0000\u0003\u0000\u001b\b\u0000\u0003"+ + "\u0000\u001d\b\u0000\u0001\u0000\u0001\u0000\u0001\u0001\u0001\u0001\u0001"+ + "\u0001\u0001\u0001\u0001\u0001\u0001\u0002\u0001\u0002\u0001\u0002\u0001"+ + "\u0003\u0001\u0003\u0001\u0003\u0001\u0003\u0001\u0004\u0001\u0004\u0001"+ + "\u0004\u0001\u0004\u0001\u0004\u0005\u00042\b\u0004\n\u0004\f\u00045\t"+ + "\u0004\u0001\u0004\u0001\u0004\u0001\u0005\u0001\u0005\u0001\u0005\u0005"+ + "\u0005<\b\u0005\n\u0005\f\u0005?\t\u0005\u0001\u0006\u0001\u0006\u0001"+ + "\u0006\u0001\u0006\u0003\u0006E\b\u0006\u0001\u0006\u0001\u0006\u0005"+ + "\u0006I\b\u0006\n\u0006\f\u0006L\t\u0006\u0001\u0006\u0001\u0006\u0001"+ + "\u0007\u0001\u0007\u0005\u0007R\b\u0007\n\u0007\f\u0007U\t\u0007\u0001"+ + "\u0007\u0000\u0000\b\u0000\u0002\u0004\u0006\b\n\f\u000e\u0000\u0001\u0002"+ + "\u0000\n\f\u000e\u000eX\u0000\u0010\u0001\u0000\u0000\u0000\u0002 \u0001"+ + "\u0000\u0000\u0000\u0004%\u0001\u0000\u0000\u0000\u0006(\u0001\u0000\u0000"+ + "\u0000\b,\u0001\u0000\u0000\u0000\n8\u0001\u0000\u0000\u0000\f@\u0001"+ + "\u0000\u0000\u0000\u000eS\u0001\u0000\u0000\u0000\u0010\u001c\u0005\u0001"+ + "\u0000\u0000\u0011\u0016\u0003\u0004\u0002\u0000\u0012\u0013\u0005\u0002"+ + "\u0000\u0000\u0013\u0015\u0003\u0004\u0002\u0000\u0014\u0012\u0001\u0000"+ + "\u0000\u0000\u0015\u0018\u0001\u0000\u0000\u0000\u0016\u0014\u0001\u0000"+ + "\u0000\u0000\u0016\u0017\u0001\u0000\u0000\u0000\u0017\u001a\u0001\u0000"+ + "\u0000\u0000\u0018\u0016\u0001\u0000\u0000\u0000\u0019\u001b\u0005\u0002"+ + "\u0000\u0000\u001a\u0019\u0001\u0000\u0000\u0000\u001a\u001b\u0001\u0000"+ + "\u0000\u0000\u001b\u001d\u0001\u0000\u0000\u0000\u001c\u0011\u0001\u0000"+ + "\u0000\u0000\u001c\u001d\u0001\u0000\u0000\u0000\u001d\u001e\u0001\u0000"+ + "\u0000\u0000\u001e\u001f\u0005\u0003\u0000\u0000\u001f\u0001\u0001\u0000"+ + "\u0000\u0000 !\u0005\f\u0000\u0000!\"\u0005\f\u0000\u0000\"#\u0003\u0000"+ + "\u0000\u0000#$\u0003\f\u0006\u0000$\u0003\u0001\u0000\u0000\u0000%&\u0005"+ + "\f\u0000\u0000&\'\u0005\f\u0000\u0000\'\u0005\u0001\u0000\u0000\u0000"+ + "()\u0003\u0004\u0002\u0000)*\u0005\u0004\u0000\u0000*+\u0003\n\u0005\u0000"+ + "+\u0007\u0001\u0000\u0000\u0000,-\u0005\u0005\u0000\u0000-.\u0005\f\u0000"+ + "\u0000.3\u0003\n\u0005\u0000/0\u0005\u0002\u0000\u000002\u0003\n\u0005"+ + "\u00001/\u0001\u0000\u0000\u000025\u0001\u0000\u0000\u000031\u0001\u0000"+ + "\u0000\u000034\u0001\u0000\u0000\u000046\u0001\u0000\u0000\u000053\u0001"+ + "\u0000\u0000\u000067\u0005\u0006\u0000\u00007\t\u0001\u0000\u0000\u0000"+ + "8=\u0007\u0000\u0000\u00009:\u0005\r\u0000\u0000:<\u0003\n\u0005\u0000"+ + ";9\u0001\u0000\u0000\u0000\u0001\u0000\u0000\u0000>\u000b\u0001\u0000\u0000\u0000?=\u0001"+ + "\u0000\u0000\u0000@J\u0005\u0007\u0000\u0000AE\u0003\n\u0005\u0000BE\u0003"+ + "\u0004\u0002\u0000CE\u0003\u0006\u0003\u0000DA\u0001\u0000\u0000\u0000"+ + "DB\u0001\u0000\u0000\u0000DC\u0001\u0000\u0000\u0000EF\u0001\u0000\u0000"+ + "\u0000FG\u0005\u0006\u0000\u0000GI\u0001\u0000\u0000\u0000HD\u0001\u0000"+ + "\u0000\u0000IL\u0001\u0000\u0000\u0000JH\u0001\u0000\u0000\u0000JK\u0001"+ + "\u0000\u0000\u0000KM\u0001\u0000\u0000\u0000LJ\u0001\u0000\u0000\u0000"+ + "MN\u0005\b\u0000\u0000N\r\u0001\u0000\u0000\u0000OR\u0003\b\u0004\u0000"+ + "PR\u0003\u0002\u0001\u0000QO\u0001\u0000\u0000\u0000QP\u0001\u0000\u0000"+ + "\u0000RU\u0001\u0000\u0000\u0000SQ\u0001\u0000\u0000\u0000ST\u0001\u0000"+ + "\u0000\u0000T\u000f\u0001\u0000\u0000\u0000US\u0001\u0000\u0000\u0000"+ + "\t\u0016\u001a\u001c3=DJQS"; public static final ATN _ATN = new ATNDeserializer().deserialize(_serializedATN.toCharArray()); static { diff --git a/Wyst.g4 b/Wyst.g4 index ab6f50b..9fa5ccc 100644 --- a/Wyst.g4 +++ b/Wyst.g4 @@ -1,9 +1,28 @@ grammar Wyst; -WS : [ \t\r\n]+ -> skip; +WS: [ \t\r\n]+ -> skip; +NUMBER: [1-9][0-9]* | '0'; +HEX: '0x' [a-fA-F0-9]*; +IDENTIFIER: [a-zA-Z_] ([a-zA-Z0-9] | '::')*; +MATH: '+' | '-' | '*' | '/'; +fragment ESC: '\\' ['"\\] ; +STRING: '"' (ESC | ~["\\])* '"'; +round_def: '(' (var_def (',' var_def)* ','?)? ')'; +func_def: IDENTIFIER IDENTIFIER round_def code_block; +var_def: IDENTIFIER IDENTIFIER; +var_def_set: var_def '=' expr; +asm: '%' IDENTIFIER expr (',' expr)* ';'; -expr: IDENTIFIER - ; +expr: + ( NUMBER | + HEX | + IDENTIFIER | + STRING + ) (MATH expr)*; -INT: [0-9]+; -IDENTIFIER: [a-zA-Z_] ([a-zA-Z0-9] | '::')*; \ No newline at end of file +code_block: '{' (( + expr | + var_def | + var_def_set +) ';')* '}'; +top: (asm|func_def)*; \ No newline at end of file diff --git a/go.mod b/go.mod index d14b7d4..8dbc773 100644 --- a/go.mod +++ b/go.mod @@ -7,6 +7,5 @@ toolchain go1.22.6 require github.com/antlr4-go/antlr/v4 v4.13.1 // direct require ( - github.com/antlr4-go/antlr v0.0.0-20230518091524-98b52378c522 // indirect golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect ) diff --git a/src/main.go b/src/main.go index 7def23c..6e413d9 100644 --- a/src/main.go +++ b/src/main.go @@ -2,93 +2,15 @@ package main import ( "fmt" - "strings" + "os" ) -type wmap [2]string - -var wyst_map []wmap - -func TranspileToken(node ASTNode) string { - res := "" - switch node.Rule { - - case "NUMBER": - res += node.Text - case "IDENTIFIER": - for _, i := range wyst_map { - if node.Text == i[0] { - return i[1] - } - } - res += "_0x" - for _, c := range node.Text { - if c == ':' || c == '.' { - res += string(c) - } else { - res += fmt.Sprintf("%x", c) - } - } - case "HEX": - res += node.Text - case "MATH": - res += node.Text - case "STRING": - res += node.Text - case "';'": - res += "\n " - default: - fmt.Printf("\x1b[33mDEV NOTICE:\x1b[0m Missing token case for '%s'\n", node.Rule) - +func main() { + content, err := os.ReadFile("test/main.wst") + if err != nil { + fmt.Println("Error reading file:", err) + return } - - return res -} - -func TranspileNode(node ASTNode) string { - res := "" - if node.Rule == strings.ToUpper(node.Rule) { - return TranspileToken(node) - } - switch node.Rule { - - case "func_def": - res += fmt.Sprintf("func %s%s %s %s", TranspileNode(node.Inner[1]), TranspileNode(node.Inner[2]), TranspileNode(node.Inner[0]), TranspileNode(node.Inner[3])) - case "expr": - res += TranspileNodes(node.Inner) - case "code_block": - res += "{\n " - res += strings.TrimSuffix(TranspileNodes(node.Inner), " ") - res += "}\n" - case "var_def": - res += fmt.Sprintf("var %s %s", TranspileNode(node.Inner[1]), TranspileNode(node.Inner[0])) - case "round_def": - res += "(" - res += strings.TrimSuffix(strings.ReplaceAll(TranspileNodes(node.Inner), "var ", ""), " ") - res += ")" - case "asm": - if node.Inner[0].Text == "map" && len(node.Inner) == 4 { - wyst_map = append(wyst_map, wmap{node.Inner[1].Text, node.Inner[2].Text}) - } - - default: - fmt.Printf("\x1b[33mDEV NOTICE:\x1b[0m Missing case for '%s'\n", node.Rule) - - } - return res -} - -func TranspileNodes(nodes []ASTNode) string { - res := "" - for i := 0; i < len(nodes); i++ { - res += TranspileNode(nodes[i]) - } - return res -} - -func Transpile(code string) string { - ast := Parse(code) - res := TranspileNodes(ast.Inner) - // fmt.Printf("%v", ast) - return res + code := Transpile(string(content)) + fmt.Printf("output:\n%s\n", code) } diff --git a/src/transpiler.go b/src/transpiler.go index 7def23c..6256ca5 100644 --- a/src/transpiler.go +++ b/src/transpiler.go @@ -89,6 +89,5 @@ func TranspileNodes(nodes []ASTNode) string { func Transpile(code string) string { ast := Parse(code) res := TranspileNodes(ast.Inner) - // fmt.Printf("%v", ast) return res } diff --git a/test/main.wst b/test/main.wst index a0f182e..632016f 100644 --- a/test/main.wst +++ b/test/main.wst @@ -1 +1,2 @@ -%map test, bruh \ No newline at end of file +%map test, hi; +int main(test x) {} \ No newline at end of file