Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Some infrastructure.

  • Loading branch information...
commit 74f3055222da3283a7eaa011901ade0ddce06d92 1 parent f4db4b3
Brian Shirai authored November 20, 2010
1  .gitignore
... ...
@@ -0,0 +1 @@
  1
+*.rbc
2  lib/syme.rb
... ...
@@ -0,0 +1,2 @@
  1
+require 'syme/bootstrap'
  2
+require 'syme/version'
4  lib/syme/bootstrap.rb
... ...
@@ -0,0 +1,4 @@
  1
+require 'syme/bootstrap/syntax'
  2
+require 'syme/bootstrap/parser'
  3
+require 'syme/bootstrap/compiler'
  4
+require 'syme/bootstrap/library'
47  lib/syme/bootstrap/parser.rb
... ...
@@ -0,0 +1,47 @@
  1
+module Syme
  2
+  class Parser
  3
+    def initialize
  4
+      @error_position = 0
  5
+    end
  6
+
  7
+    # The #parse_string method is defined in the parser C extension.
  8
+
  9
+    def parse(string)
  10
+      @string = string
  11
+      ast = parse_string string
  12
+      show_syntax_error unless ast
  13
+      ast
  14
+    end
  15
+
  16
+    def parse_file(name)
  17
+      string = IO.read name
  18
+      parse string
  19
+    end
  20
+
  21
+    # Parsing callbacks
  22
+
  23
+    def show_syntax_error
  24
+      error_line = nil
  25
+      count = 0
  26
+
  27
+      @string.each_line do |line|
  28
+        count += line.size
  29
+        if count > @error_position
  30
+          error_line = line
  31
+          break
  32
+        end
  33
+      end
  34
+
  35
+      message = <<-EOM
  36
+
  37
+#{error_line.chomp}
  38
+#{" " *(error_line.size - (count - @error_position))}^
  39
+EOM
  40
+      raise Syntax::SyntaxError, message
  41
+    end
  42
+
  43
+    def syntax_error(pos)
  44
+      @error_position = pos
  45
+    end
  46
+  end
  47
+end
4  lib/syme/bootstrap/parser/ext/.gitignore
... ...
@@ -0,0 +1,4 @@
  1
+*.bundle
  2
+*.so
  3
+*.o
  4
+parser.c
464  lib/syme/bootstrap/parser/ext/parser.c
... ...
@@ -0,0 +1,464 @@
  1
+/* A recursive-descent parser generated by greg 0.3.0 */
  2
+
  3
+#include <stdio.h>
  4
+#include <stdlib.h>
  5
+#include <string.h>
  6
+struct _GREG;
  7
+#define YYRULECOUNT 7
  8
+
  9
+
  10
+#include "ruby.h"
  11
+#include "parser.h"
  12
+
  13
+#define YY_INPUT(buf, result, max) {  \
  14
+  if (N->pos < N->size) {             \
  15
+    result = max;                     \
  16
+    if(N->pos + max > N->size)        \
  17
+      result = N->size - N->pos;      \
  18
+    memcpy(buf, N->bytes, result+1);  \
  19
+    N->pos += max;                    \
  20
+  } else {                            \
  21
+    result = 0;                       \
  22
+  }                                   \
  23
+}
  24
+
  25
+#define YYSTYPE VALUE
  26
+#define YY_XTYPE Newspeak*
  27
+#define YY_XVAR N
  28
+#define YY_NAME(N) newspeak_code_##N
  29
+
  30
+#define NS_VAL(n)         rb_funcall(N->parser, rb_intern(n), 0)
  31
+#define NS_AST(n, v)      rb_funcall(N->parser, rb_intern(n), 1, v)
  32
+#define NS_AST2(n, x, y)  rb_funcall(N->parser, rb_intern(n), 2, x, y)
  33
+#define NS_OP(n, l, r)    rb_funcall(N->parser, rb_intern(n), 2, l, r)
  34
+
  35
+
  36
+#ifndef YY_ALLOC
  37
+#define YY_ALLOC(N, D) malloc(N)
  38
+#endif
  39
+#ifndef YY_CALLOC
  40
+#define YY_CALLOC(N, S, D) calloc(N, S)
  41
+#endif
  42
+#ifndef YY_REALLOC
  43
+#define YY_REALLOC(B, N, D) realloc(B, N)
  44
+#endif
  45
+#ifndef YY_FREE
  46
+#define YY_FREE free
  47
+#endif
  48
+#ifndef YY_LOCAL
  49
+#define YY_LOCAL(T)	static T
  50
+#endif
  51
+#ifndef YY_ACTION
  52
+#define YY_ACTION(T)	static T
  53
+#endif
  54
+#ifndef YY_RULE
  55
+#define YY_RULE(T)	static T
  56
+#endif
  57
+#ifndef YY_PARSE
  58
+#define YY_PARSE(T)	T
  59
+#endif
  60
+#ifndef YY_NAME
  61
+#define YY_NAME(N) yy##N
  62
+#endif
  63
+#ifndef YY_INPUT
  64
+#define YY_INPUT(buf, result, max_size)			\
  65
+  {							\
  66
+    int yyc= getchar();					\
  67
+    result= (EOF == yyc) ? 0 : (*(buf)= yyc, 1);	\
  68
+    yyprintf((stderr, "<%c>", yyc));			\
  69
+  }
  70
+#endif
  71
+#ifndef YY_BEGIN
  72
+#define YY_BEGIN	( G->begin= G->pos, 1)
  73
+#endif
  74
+#ifndef YY_END
  75
+#define YY_END		( G->end= G->pos, 1)
  76
+#endif
  77
+#ifdef YY_DEBUG
  78
+# define yyprintf(args)	fprintf args
  79
+#else
  80
+# define yyprintf(args)
  81
+#endif
  82
+#ifndef YYSTYPE
  83
+#define YYSTYPE	int
  84
+#endif
  85
+#ifndef YY_XTYPE
  86
+#define YY_XTYPE void *
  87
+#endif
  88
+#ifndef YY_XVAR
  89
+#define YY_XVAR yydata
  90
+#endif
  91
+
  92
+#ifndef YY_PART
  93
+#define yy G->ss
  94
+
  95
+typedef void (*yyaction)(struct _GREG *G, char *yytext, int yyleng, YY_XTYPE YY_XVAR);
  96
+typedef struct _yythunk { int begin, end;  yyaction  action;  struct _yythunk *next; } yythunk;
  97
+
  98
+typedef struct _GREG {
  99
+  char *buf;
  100
+  int buflen;
  101
+  int	pos;
  102
+  int	limit;
  103
+  char *text;
  104
+  int	textlen;
  105
+  int	begin;
  106
+  int	end;
  107
+  yythunk *thunks;
  108
+  int	thunkslen;
  109
+  int thunkpos;
  110
+  YYSTYPE ss;
  111
+  YYSTYPE *val;
  112
+  YYSTYPE *vals;
  113
+  int valslen;
  114
+  YY_XTYPE data;
  115
+} GREG;
  116
+
  117
+YY_LOCAL(int) yyrefill(GREG *G)
  118
+{
  119
+  int yyn;
  120
+  YY_XTYPE YY_XVAR = (YY_XTYPE)G->data;
  121
+  while (G->buflen - G->pos < 512)
  122
+    {
  123
+      G->buflen *= 2;
  124
+      G->buf= YY_REALLOC(G->buf, G->buflen, G->data);
  125
+    }
  126
+  YY_INPUT((G->buf + G->pos), yyn, (G->buflen - G->pos));
  127
+  if (!yyn) return 0;
  128
+  G->limit += yyn;
  129
+  return 1;
  130
+}
  131
+
  132
+YY_LOCAL(int) yymatchDot(GREG *G)
  133
+{
  134
+  if (G->pos >= G->limit && !yyrefill(G)) return 0;
  135
+  ++G->pos;
  136
+  return 1;
  137
+}
  138
+
  139
+YY_LOCAL(int) yymatchChar(GREG *G, int c)
  140
+{
  141
+  if (G->pos >= G->limit && !yyrefill(G)) return 0;
  142
+  if ((unsigned char)G->buf[G->pos] == c)
  143
+    {
  144
+      ++G->pos;
  145
+      yyprintf((stderr, "  ok   yymatchChar(%c) @ %s\n", c, G->buf+G->pos));
  146
+      return 1;
  147
+    }
  148
+  yyprintf((stderr, "  fail yymatchChar(%c) @ %s\n", c, G->buf+G->pos));
  149
+  return 0;
  150
+}
  151
+
  152
+YY_LOCAL(int) yymatchString(GREG *G, char *s)
  153
+{
  154
+  int yysav= G->pos;
  155
+  while (*s)
  156
+    {
  157
+      if (G->pos >= G->limit && !yyrefill(G)) return 0;
  158
+      if (G->buf[G->pos] != *s)
  159
+        {
  160
+          G->pos= yysav;
  161
+          return 0;
  162
+        }
  163
+      ++s;
  164
+      ++G->pos;
  165
+    }
  166
+  return 1;
  167
+}
  168
+
  169
+YY_LOCAL(int) yymatchClass(GREG *G, unsigned char *bits)
  170
+{
  171
+  int c;
  172
+  if (G->pos >= G->limit && !yyrefill(G)) return 0;
  173
+  c= (unsigned char)G->buf[G->pos];
  174
+  if (bits[c >> 3] & (1 << (c & 7)))
  175
+    {
  176
+      ++G->pos;
  177
+      yyprintf((stderr, "  ok   yymatchClass @ %s\n", G->buf+G->pos));
  178
+      return 1;
  179
+    }
  180
+  yyprintf((stderr, "  fail yymatchClass @ %s\n", G->buf+G->pos));
  181
+  return 0;
  182
+}
  183
+
  184
+YY_LOCAL(void) yyDo(GREG *G, yyaction action, int begin, int end)
  185
+{
  186
+  while (G->thunkpos >= G->thunkslen)
  187
+    {
  188
+      G->thunkslen *= 2;
  189
+      G->thunks= YY_REALLOC(G->thunks, sizeof(yythunk) * G->thunkslen, G->data);
  190
+    }
  191
+  G->thunks[G->thunkpos].begin=  begin;
  192
+  G->thunks[G->thunkpos].end=    end;
  193
+  G->thunks[G->thunkpos].action= action;
  194
+  ++G->thunkpos;
  195
+}
  196
+
  197
+YY_LOCAL(int) yyText(GREG *G, int begin, int end)
  198
+{
  199
+  int yyleng= end - begin;
  200
+  if (yyleng <= 0)
  201
+    yyleng= 0;
  202
+  else
  203
+    {
  204
+      while (G->textlen < (yyleng - 1))
  205
+	{
  206
+	  G->textlen *= 2;
  207
+	  G->text= YY_REALLOC(G->text, G->textlen, G->data);
  208
+	}
  209
+      memcpy(G->text, G->buf + begin, yyleng);
  210
+    }
  211
+  G->text[yyleng]= '\0';
  212
+  return yyleng;
  213
+}
  214
+
  215
+YY_LOCAL(void) yyDone(GREG *G)
  216
+{
  217
+  int pos;
  218
+  for (pos= 0;  pos < G->thunkpos;  ++pos)
  219
+    {
  220
+      yythunk *thunk= &G->thunks[pos];
  221
+      int yyleng= thunk->end ? yyText(G, thunk->begin, thunk->end) : thunk->begin;
  222
+      yyprintf((stderr, "DO [%d] %p %s\n", pos, thunk->action, G->text));
  223
+      thunk->action(G, G->text, yyleng, G->data);
  224
+    }
  225
+  G->thunkpos= 0;
  226
+}
  227
+
  228
+YY_LOCAL(void) yyCommit(GREG *G)
  229
+{
  230
+  if ((G->limit -= G->pos))
  231
+    {
  232
+      memmove(G->buf, G->buf + G->pos, G->limit);
  233
+    }
  234
+  G->begin -= G->pos;
  235
+  G->end -= G->pos;
  236
+  G->pos= G->thunkpos= 0;
  237
+}
  238
+
  239
+YY_LOCAL(int) yyAccept(GREG *G, int tp0)
  240
+{
  241
+  if (tp0)
  242
+    {
  243
+      fprintf(stderr, "accept denied at %d\n", tp0);
  244
+      return 0;
  245
+    }
  246
+  else
  247
+    {
  248
+      yyDone(G);
  249
+      yyCommit(G);
  250
+    }
  251
+  return 1;
  252
+}
  253
+
  254
+YY_LOCAL(void) yyPush(GREG *G, char *text, int count, YY_XTYPE YY_XVAR)	{
  255
+  size_t off = (G->val - G->vals) + count;
  256
+  if (off > G->valslen) {
  257
+    while (G->valslen < off + 1)
  258
+      G->valslen *= 2;
  259
+    G->vals= YY_REALLOC((void *)G->vals, sizeof(YYSTYPE) * G->valslen, G->data);
  260
+    G->val= G->vals + off;
  261
+  } else {
  262
+    G->val += count;
  263
+  }
  264
+}
  265
+YY_LOCAL(void) yyPop(GREG *G, char *text, int count, YY_XTYPE YY_XVAR)	{ G->val -= count; }
  266
+YY_LOCAL(void) yySet(GREG *G, char *text, int count, YY_XTYPE YY_XVAR)	{ G->val[count]= G->ss; }
  267
+
  268
+#endif /* YY_PART */
  269
+
  270
+#define	YYACCEPT	yyAccept(G, yythunkpos0)
  271
+
  272
+YY_RULE(int) yy_end_of_file(GREG *G); /* 7 */
  273
+YY_RULE(int) yy_end_of_line(GREG *G); /* 6 */
  274
+YY_RULE(int) yy_comment(GREG *G); /* 5 */
  275
+YY_RULE(int) yy_space(GREG *G); /* 4 */
  276
+YY_RULE(int) yy__(GREG *G); /* 3 */
  277
+YY_RULE(int) yy___(GREG *G); /* 2 */
  278
+YY_RULE(int) yy_newspeak(GREG *G); /* 1 */
  279
+
  280
+
  281
+YY_RULE(int) yy_end_of_file(GREG *G)
  282
+{  int yypos0= G->pos, yythunkpos0= G->thunkpos;
  283
+  yyprintf((stderr, "%s\n", "end_of_file"));
  284
+  {  int yypos2= G->pos, yythunkpos2= G->thunkpos;  if (!yymatchDot(G)) goto l2;  goto l1;
  285
+  l2:;	  G->pos= yypos2; G->thunkpos= yythunkpos2;
  286
+  }
  287
+  yyprintf((stderr, "  ok   %s @ %s\n", "end_of_file", G->buf+G->pos));
  288
+  return 1;
  289
+  l1:;	  G->pos= yypos0; G->thunkpos= yythunkpos0;
  290
+  yyprintf((stderr, "  fail %s @ %s\n", "end_of_file", G->buf+G->pos));
  291
+  return 0;
  292
+}
  293
+YY_RULE(int) yy_end_of_line(GREG *G)
  294
+{  int yypos0= G->pos, yythunkpos0= G->thunkpos;
  295
+  yyprintf((stderr, "%s\n", "end_of_line"));
  296
+  {  int yypos4= G->pos, yythunkpos4= G->thunkpos;  if (!yymatchString(G, "\r\n")) goto l5;  goto l4;
  297
+  l5:;	  G->pos= yypos4; G->thunkpos= yythunkpos4;  if (!yymatchChar(G, '\n')) goto l6;  goto l4;
  298
+  l6:;	  G->pos= yypos4; G->thunkpos= yythunkpos4;  if (!yymatchChar(G, '\r')) goto l3;
  299
+  }
  300
+  l4:;	
  301
+  yyprintf((stderr, "  ok   %s @ %s\n", "end_of_line", G->buf+G->pos));
  302
+  return 1;
  303
+  l3:;	  G->pos= yypos0; G->thunkpos= yythunkpos0;
  304
+  yyprintf((stderr, "  fail %s @ %s\n", "end_of_line", G->buf+G->pos));
  305
+  return 0;
  306
+}
  307
+YY_RULE(int) yy_comment(GREG *G)
  308
+{  int yypos0= G->pos, yythunkpos0= G->thunkpos;
  309
+  yyprintf((stderr, "%s\n", "comment"));  if (!yymatchChar(G, '#')) goto l7;
  310
+  l8:;	
  311
+  {  int yypos9= G->pos, yythunkpos9= G->thunkpos;
  312
+  {  int yypos10= G->pos, yythunkpos10= G->thunkpos;  if (!yy_end_of_line(G)) goto l10;  goto l9;
  313
+  l10:;	  G->pos= yypos10; G->thunkpos= yythunkpos10;
  314
+  }  goto l8;
  315
+  l9:;	  G->pos= yypos9; G->thunkpos= yythunkpos9;
  316
+  }
  317
+  yyprintf((stderr, "  ok   %s @ %s\n", "comment", G->buf+G->pos));
  318
+  return 1;
  319
+  l7:;	  G->pos= yypos0; G->thunkpos= yythunkpos0;
  320
+  yyprintf((stderr, "  fail %s @ %s\n", "comment", G->buf+G->pos));
  321
+  return 0;
  322
+}
  323
+YY_RULE(int) yy_space(GREG *G)
  324
+{  int yypos0= G->pos, yythunkpos0= G->thunkpos;
  325
+  yyprintf((stderr, "%s\n", "space"));
  326
+  {  int yypos12= G->pos, yythunkpos12= G->thunkpos;  if (!yymatchChar(G, ' ')) goto l13;  goto l12;
  327
+  l13:;	  G->pos= yypos12; G->thunkpos= yythunkpos12;  if (!yymatchChar(G, '\f')) goto l14;  goto l12;
  328
+  l14:;	  G->pos= yypos12; G->thunkpos= yythunkpos12;  if (!yymatchChar(G, '\v')) goto l15;  goto l12;
  329
+  l15:;	  G->pos= yypos12; G->thunkpos= yythunkpos12;  if (!yymatchChar(G, '\t')) goto l11;
  330
+  }
  331
+  l12:;	
  332
+  yyprintf((stderr, "  ok   %s @ %s\n", "space", G->buf+G->pos));
  333
+  return 1;
  334
+  l11:;	  G->pos= yypos0; G->thunkpos= yythunkpos0;
  335
+  yyprintf((stderr, "  fail %s @ %s\n", "space", G->buf+G->pos));
  336
+  return 0;
  337
+}
  338
+YY_RULE(int) yy__(GREG *G)
  339
+{
  340
+  yyprintf((stderr, "%s\n", "_"));
  341
+  l17:;	
  342
+  {  int yypos18= G->pos, yythunkpos18= G->thunkpos;
  343
+  {  int yypos19= G->pos, yythunkpos19= G->thunkpos;  if (!yy_space(G)) goto l20;  goto l19;
  344
+  l20:;	  G->pos= yypos19; G->thunkpos= yythunkpos19;  if (!yy_comment(G)) goto l18;
  345
+  }
  346
+  l19:;	  goto l17;
  347
+  l18:;	  G->pos= yypos18; G->thunkpos= yythunkpos18;
  348
+  }
  349
+  yyprintf((stderr, "  ok   %s @ %s\n", "_", G->buf+G->pos));
  350
+  return 1;
  351
+}
  352
+YY_RULE(int) yy___(GREG *G)
  353
+{
  354
+  yyprintf((stderr, "%s\n", "__"));
  355
+  l22:;	
  356
+  {  int yypos23= G->pos, yythunkpos23= G->thunkpos;
  357
+  {  int yypos24= G->pos, yythunkpos24= G->thunkpos;  if (!yy_space(G)) goto l25;  goto l24;
  358
+  l25:;	  G->pos= yypos24; G->thunkpos= yythunkpos24;  if (!yy_comment(G)) goto l26;  goto l24;
  359
+  l26:;	  G->pos= yypos24; G->thunkpos= yythunkpos24;  if (!yy_end_of_line(G)) goto l23;
  360
+  }
  361
+  l24:;	  goto l22;
  362
+  l23:;	  G->pos= yypos23; G->thunkpos= yythunkpos23;
  363
+  }
  364
+  yyprintf((stderr, "  ok   %s @ %s\n", "__", G->buf+G->pos));
  365
+  return 1;
  366
+}
  367
+YY_RULE(int) yy_newspeak(GREG *G)
  368
+{  int yypos0= G->pos, yythunkpos0= G->thunkpos;
  369
+  yyprintf((stderr, "%s\n", "newspeak"));  if (!yy___(G)) goto l27;
  370
+  yyprintf((stderr, "  ok   %s @ %s\n", "newspeak", G->buf+G->pos));
  371
+  return 1;
  372
+  l27:;	  G->pos= yypos0; G->thunkpos= yythunkpos0;
  373
+  yyprintf((stderr, "  fail %s @ %s\n", "newspeak", G->buf+G->pos));
  374
+  return 0;
  375
+}
  376
+
  377
+#ifndef YY_PART
  378
+
  379
+typedef int (*yyrule)(GREG *G);
  380
+
  381
+YY_PARSE(int) YY_NAME(parse_from)(GREG *G, yyrule yystart)
  382
+{
  383
+  int yyok;
  384
+  if (!G->buflen)
  385
+    {
  386
+      G->buflen= 1024;
  387
+      G->buf= YY_ALLOC(G->buflen, G->data);
  388
+      G->textlen= 1024;
  389
+      G->text= YY_ALLOC(G->textlen, G->data);
  390
+      G->thunkslen= 32;
  391
+      G->thunks= YY_ALLOC(sizeof(yythunk) * G->thunkslen, G->data);
  392
+      G->valslen= 32;
  393
+      G->vals= YY_ALLOC(sizeof(YYSTYPE) * G->valslen, G->data);
  394
+      G->begin= G->end= G->pos= G->limit= G->thunkpos= 0;
  395
+    }
  396
+  G->begin= G->end= G->pos;
  397
+  G->thunkpos= 0;
  398
+  G->val= G->vals;
  399
+  yyok= yystart(G);
  400
+  if (yyok) yyDone(G);
  401
+  yyCommit(G);
  402
+  return yyok;
  403
+  (void)yyrefill;
  404
+  (void)yymatchDot;
  405
+  (void)yymatchChar;
  406
+  (void)yymatchString;
  407
+  (void)yymatchClass;
  408
+  (void)yyDo;
  409
+  (void)yyText;
  410
+  (void)yyDone;
  411
+  (void)yyCommit;
  412
+  (void)yyAccept;
  413
+  (void)yyPush;
  414
+  (void)yyPop;
  415
+  (void)yySet;
  416
+}
  417
+
  418
+YY_PARSE(int) YY_NAME(parse)(GREG *G)
  419
+{
  420
+  return YY_NAME(parse_from)(G, yy_newspeak);
  421
+}
  422
+
  423
+YY_PARSE(GREG *) YY_NAME(parse_new)(YY_XTYPE data)
  424
+{
  425
+  GREG *G = (GREG *)YY_CALLOC(1, sizeof(GREG), G->data);
  426
+  G->data = data;
  427
+  return G;
  428
+}
  429
+
  430
+YY_PARSE(void) YY_NAME(parse_free)(GREG *G)
  431
+{
  432
+  YY_FREE(G);
  433
+}
  434
+
  435
+#endif
  436
+
  437
+
  438
+VALUE newspeak_parse_string(VALUE self, VALUE string) {
  439
+  Newspeak N;
  440
+
  441
+  N.parser = self;
  442
+  N.pos = 0;
  443
+  N.size = RSTRING_LEN(string);
  444
+  N.bytes = RSTRING_PTR(string);
  445
+
  446
+  GREG *G = newspeak_code_parse_new(&N);
  447
+  G->pos = G->limit = 0;
  448
+
  449
+  if (!newspeak_code_parse(G)) {
  450
+    rb_funcall(N.parser, rb_intern("syntax_error"), 1, INT2FIX(G->end));
  451
+    return N.ast = Qfalse;
  452
+  }
  453
+  newspeak_code_parse_free(G);
  454
+
  455
+  return N.ast;
  456
+}
  457
+
  458
+void Init_parser(void) {
  459
+  VALUE rb_mSyme = rb_const_get(rb_cObject, rb_intern("Syme"));
  460
+  VALUE rb_cParser = rb_const_get(rb_mSyme, rb_intern("Parser"));
  461
+
  462
+  rb_define_method(rb_cParser, "parse_string", RUBY_METHOD_FUNC(newspeak_parse_string), 1);
  463
+}
  464
+
72  lib/syme/bootstrap/parser/ext/parser.g
... ...
@@ -0,0 +1,72 @@
  1
+#
  2
+# parser.g
  3
+# Newspeak grammar
  4
+#
  5
+# (c) 2010 Brian Ford
  6
+
  7
+%{
  8
+
  9
+#include "ruby.h"
  10
+#include "parser.h"
  11
+
  12
+#define YY_INPUT(buf, result, max) {  \
  13
+  if (N->pos < N->size) {             \
  14
+    result = max;                     \
  15
+    if(N->pos + max > N->size)        \
  16
+      result = N->size - N->pos;      \
  17
+    memcpy(buf, N->bytes, result+1);  \
  18
+    N->pos += max;                    \
  19
+  } else {                            \
  20
+    result = 0;                       \
  21
+  }                                   \
  22
+}
  23
+
  24
+#define YYSTYPE VALUE
  25
+#define YY_XTYPE Newspeak*
  26
+#define YY_XVAR N
  27
+#define YY_NAME(N) newspeak_code_##N
  28
+
  29
+#define NS_VAL(n)         rb_funcall(N->parser, rb_intern(n), 0)
  30
+#define NS_AST(n, v)      rb_funcall(N->parser, rb_intern(n), 1, v)
  31
+#define NS_AST2(n, x, y)  rb_funcall(N->parser, rb_intern(n), 2, x, y)
  32
+#define NS_OP(n, l, r)    rb_funcall(N->parser, rb_intern(n), 2, l, r)
  33
+
  34
+%}
  35
+
  36
+newspeak = --
  37
+
  38
+- = (space | comment)*
  39
+-- = (space | comment | end-of-line)*
  40
+comment	= '#' (!end-of-line)*
  41
+space = ' ' | '\f' | '\v' | '\t'
  42
+end-of-line = '\r\n' | '\n' | '\r'
  43
+end-of-file = !.
  44
+
  45
+%%
  46
+
  47
+VALUE newspeak_parse_string(VALUE self, VALUE string) {
  48
+  Newspeak N;
  49
+
  50
+  N.parser = self;
  51
+  N.pos = 0;
  52
+  N.size = RSTRING_LEN(string);
  53
+  N.bytes = RSTRING_PTR(string);
  54
+
  55
+  GREG *G = newspeak_code_parse_new(&N);
  56
+  G->pos = G->limit = 0;
  57
+
  58
+  if (!newspeak_code_parse(G)) {
  59
+    rb_funcall(N.parser, rb_intern("syntax_error"), 1, INT2FIX(G->end));
  60
+    return N.ast = Qfalse;
  61
+  }
  62
+  newspeak_code_parse_free(G);
  63
+
  64
+  return N.ast;
  65
+}
  66
+
  67
+void Init_parser(void) {
  68
+  VALUE rb_mSyme = rb_const_get(rb_cObject, rb_intern("Syme"));
  69
+  VALUE rb_cParser = rb_const_get(rb_mSyme, rb_intern("Parser"));
  70
+
  71
+  rb_define_method(rb_cParser, "parse_string", RUBY_METHOD_FUNC(newspeak_parse_string), 1);
  72
+}
26  lib/syme/bootstrap/parser/ext/parser.h
... ...
@@ -0,0 +1,26 @@
  1
+#ifndef SYME_PARSER_H
  2
+#define SYME_PARSER_H
  3
+
  4
+#ifdef __cplusplus
  5
+extern "C" {
  6
+#endif
  7
+
  8
+#ifndef O_BINARY
  9
+#define O_BINARY 0
  10
+#endif
  11
+
  12
+typedef struct NewspeakParserState {
  13
+  unsigned int pos;
  14
+  unsigned int size;
  15
+  char*        bytes;
  16
+  VALUE        parser;
  17
+  VALUE        ast;
  18
+} Newspeak;
  19
+
  20
+VALUE newspeak_parse(VALUE self, VALUE string);
  21
+
  22
+#ifdef __cplusplus
  23
+}
  24
+#endif
  25
+
  26
+#endif
1  lib/syme/bootstrap/syntax.rb
... ...
@@ -0,0 +1 @@
  1
+require 'syme/bootstrap/syntax/exception'
5  lib/syme/bootstrap/syntax/exception.rb
... ...
@@ -0,0 +1,5 @@
  1
+module Syme
  2
+  module Syntax
  3
+    class SyntaxError < Exception; end
  4
+  end
  5
+end
14  lib/syme/version.rb
... ...
@@ -0,0 +1,14 @@
  1
+module Syme
  2
+  module VERSION
  3
+    MAJOR = 0
  4
+    MINOR = 0
  5
+    TINY  = 1
  6
+    BUILD = nil
  7
+
  8
+    STRING = [MAJOR, MINOR, TINY, BUILD].compact.join('.')
  9
+  end
  10
+
  11
+  def VERSION.to_s
  12
+    self::STRING
  13
+  end
  14
+end
0  syme/bootstrap/compiler.rb b/lib/syme/bootstrap/compiler.rb
No changes.
0  syme/bootstrap/library.rb b/lib/syme/bootstrap/library.rb
No changes.

0 notes on commit 74f3055

Please sign in to comment.
Something went wrong with that request. Please try again.