Permalink
Browse files

first commit

  • Loading branch information...
0 parents commit f8a76a450e0e1267e9847f70d274cc3fc17dfd4c @peryaudo committed with peryaudo Aug 26, 2013
Showing with 37,657 additions and 0 deletions.
  1. +21 −0 LICENSE
  2. +14 −0 Makefile
  3. +18 −0 Makefile.in
  4. +103 −0 README.md
  5. +7 −0 runtime/Makefile
  6. +3 −0 runtime/peryandefs
  7. +172 −0 runtime/runtime.c
  8. +431 −0 src/AST.h
  9. +394 −0 src/ASTPrinter.h
  10. +20 −0 src/CodeGen.h
  11. +50 −0 src/FileSourceReader.cc
  12. +32 −0 src/FileSourceReader.h
  13. +1,827 −0 src/LLVMCodeGen.cc
  14. +25 −0 src/LLVMCodeGen.h
  15. +438 −0 src/Lexer.cc
  16. +112 −0 src/Lexer.h
  17. +124 −0 src/Main.cc
  18. +25 −0 src/Makefile
  19. +22 −0 src/Options.h
  20. +1,248 −0 src/Parser.cc
  21. +153 −0 src/Parser.h
  22. +18 −0 src/SourceReader.h
  23. +39 −0 src/StringSourceReader.h
  24. +217 −0 src/SymbolRegister.cc
  25. +33 −0 src/SymbolRegister.h
  26. +491 −0 src/SymbolResolver.cc
  27. +53 −0 src/SymbolResolver.h
  28. +418 −0 src/SymbolTable.h
  29. +44 −0 src/Token.cc
  30. +103 −0 src/Token.h
  31. +107 −0 src/Tokens.def
  32. +918 −0 src/TypeResolver.cc
  33. +103 −0 src/TypeResolver.h
  34. +14 −0 src/WarningPrinter.cc
  35. +28 −0 src/WarningPrinter.h
  36. +13 −0 test/Makefile
  37. +10 −0 test/integration/Makefile
  38. +1 −0 test/integration/cases/AdvString.pr
  39. +8 −0 test/integration/cases/BreakStatement.pr
  40. +10 −0 test/integration/cases/FunctionDeclaration.pr
  41. +15 −0 test/integration/cases/IfStatement.pr
  42. +9 −0 test/integration/cases/InfiniteLoop.pr
  43. +13 −0 test/integration/cases/LotOfReturns.pr
  44. +1 −0 test/integration/cases/MoreAdvStr.pr
  45. +5 −0 test/integration/cases/NestedComp.pr
  46. +6 −0 test/integration/cases/NestedRepeat.pr
  47. +11 −0 test/integration/cases/Reference.pr
  48. +3 −0 test/integration/cases/RepeatLoop.pr
  49. +24 −0 test/integration/cases/Sieve.pr
  50. +8 −0 test/integration/cases/SimpleArray.pr
  51. +6 −0 test/integration/cases/SimpleBreak.pr
  52. +4 −0 test/integration/cases/SimpleIf.pr
  53. +5 −0 test/integration/cases/String.pr
  54. +4 −0 test/integration/cases/TwoDimAry.pr
  55. +3 −0 test/integration/cases/VariableDeclaration.pr
  56. +7 −0 test/integration/cases/VariableScope.pr
  57. BIN test/integration/compiled/.FunctionDeclaration.ll.swo
  58. +1 −0 test/integration/expected/AdvString.txt
  59. +12 −0 test/integration/expected/BreakStatement.txt
  60. +1 −0 test/integration/expected/FunctionDeclaration.txt
  61. +1 −0 test/integration/expected/IfStatement.txt
  62. +5 −0 test/integration/expected/InfiniteLoop.txt
  63. +1 −0 test/integration/expected/LotOfReturns.txt
  64. +1 −0 test/integration/expected/MoreAdvStr.txt
  65. +1 −0 test/integration/expected/NestedComp.txt
  66. +12 −0 test/integration/expected/NestedRepeat.txt
  67. +1 −0 test/integration/expected/Reference.txt
  68. +5 −0 test/integration/expected/RepeatLoop.txt
  69. +169 −0 test/integration/expected/Sieve.txt
  70. +6 −0 test/integration/expected/SimpleArray.txt
  71. +2 −0 test/integration/expected/SimpleBreak.txt
  72. +2 −0 test/integration/expected/SimpleIf.txt
  73. +1 −0 test/integration/expected/String.txt
  74. +1 −0 test/integration/expected/TwoDimAry.txt
  75. +1 −0 test/integration/expected/VariableDeclaration.txt
  76. +1 −0 test/integration/expected/VariableScope.txt
  77. +54 −0 test/integration/run_tester.rb
  78. +26 −0 test/unit/ASTPrinterTest.cc
  79. +150 −0 test/unit/LexerTest.cc
  80. +23 −0 test/unit/Makefile
  81. +305 −0 test/unit/ParserTest.cc
  82. +160 −0 test/unit/SemanticsTest.cc
  83. +28 −0 test/unit/gtest/COPYING
  84. +9,118 −0 test/unit/gtest/gtest-all.cc
  85. +19,537 −0 test/unit/gtest/gtest.h
  86. +39 −0 test/unit/gtest/gtest_main.cc
  87. +2 −0 test/unit/test.pr
@@ -0,0 +1,21 @@
+The MIT License (MIT)
+
+Copyright (c) 2013 peryaudo
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,14 @@
+all:
+ cd src; make
+ cd test; make
+ cd runtime; make
+
+clean:
+ cd src; make clean
+ cd test; make clean
+ cd runtime; make clean
+
+test: all
+ cd test; make test
+
+.PHONY: all test clean
@@ -0,0 +1,18 @@
+CXX = clang++
+
+CXXFLAGS = -Wall -O3
+CPPFLAGS =
+LDFLAGS = `llvm-config --ldflags`
+LIBS = `llvm-config --libs`
+
+all : $(TARGET)
+
+%.o: %.cc
+ $(CXX) $(CXXFLAGS) $(CPPFLAGS) -c -o $@ $<
+
+$(TARGET): $(OBJS)
+ $(CXX) -o $(TARGET) $(OBJS) $(LIBS) $(LDFLAGS) $(CXXFLAGS) $(CPPFLAGS)
+
+clean:
+ $(RM) $(TARGET) $(OBJS)
+
@@ -0,0 +1,103 @@
+# プログラミング言語 Peryan
+
+## Peryanについて
+
+Peryanによって書かれたエラトステネスのふるい(1000より小さい素数を列挙)
+
+ var length = 1000
+
+ var table = [Bool](length, true)
+
+ table[0] = false
+ table[1] = false
+
+ repeat length
+ if table[cnt] == false : continue
+
+ var cnt_ = cnt
+ var cur = cnt_ + cnt_
+
+ repeat
+ cur += cnt_
+ if !(cur < length) : break
+
+ table[cur] = false
+ loop
+ loop
+
+ repeat length
+ if table[cnt] : mes String(cnt)
+ loop
+
+Peryanは以下のような目標を持つプログラミング言語です。
+
+* 強い静的型付けとLLVMを用いた静的コンパイル
+* オブジェクト指向を含む現代的な言語機能
+* HSPとの互換性
+
+Peryanは、これらの目標を達成することにより、高い実用性を持った言語となる事を目指しています。
+
+## 処理系のコンパイル
+
+処理系はSTLのみを利用するC++で書かれており、コンパイルにはLLVMが必要です。
+ただし、Peryanによって実行ファイルを出力する場合にはClangも必要です。
+また、統合テストを実行するにはRubyも必要です。
+
+ make
+ make test
+
+## PeryanによるPeryanプログラムのコンパイル
+
+ cd src
+ make
+ PERYAN_RUNTIME_PATH=../runtime ./peryan ../test/integration/cases/Sieve.pr sieve
+ ./sieve
+
+## 文法概観
+
+### 関数定義
+
+ func thisIsFunction (arg1 :: Int, arg2 :: Double, arg3 :: String) :: Int {
+ thisIsStatement
+ return 0
+ }
+
+### 変数定義
+
+ var thisIsVariable :: Int = 123
+
+ただし型推論の有効な範囲内で型名を省略することができます。
+
+## 今後の展望
+
+Peryanではオブジェクト指向など多くの機能が未だ実装されずに残っています。
+今後実装される予定の物には以下のものがあります。
+また、詳細なドキュメントも今後用意される予定です。
+
+* 名前空間
+* 本格的な型推論器
+* オブジェクト指向
+* variant型
+* パターンマッチ
+* クロージャ
+* 部分適用
+* ガード
+* 各種HSP互換機能
+* ランタイムライブラリ
+* コルーチン
+
+## 処理系のコードについて
+
+処理系のコードはメモリをまともに解放しない、頻繁にSegmentation Faultで落ちるなど、ひどい出来です。
+アドバイス等ございましたら、@peryaudoまでお伝え頂けると非常に助かります。
+
+## ライセンス
+
+Peryanの処理系はMITライセンスの下に提供されます。詳しくはLISENCEを御覧ください。
+また、test/unit/gtest/下のGoogle Testは新BSDライセンスの下に提供されます。詳しくはtest/unit/gtest/COPYINGを御覧ください。
+
+## 参考文献
+
+* 柏木 餅子, 風薬 『きつねさんでもわかるLLVM 〜コンパイラを自作するためのガイドブック〜』 2013年
+* (著) Terence Parr, (監訳) 中田 育男, (訳) 伊藤 真浩 『言語実装パターン』 2011年
+
@@ -0,0 +1,7 @@
+all: runtime.ll
+
+runtime.ll: runtime.c
+ clang -O0 -S -emit-llvm runtime.c -o runtime.ll
+
+clean:
+ rm -rf runtime.ll
@@ -0,0 +1,3 @@
+extern mes :: String -> Void
+// in case of debugging
+extern printNum :: Int -> Void
@@ -0,0 +1,172 @@
+/* peryan runtime */
+#include "stdio.h"
+#include "stdlib.h"
+#include "string.h"
+
+extern void PeryanMain();
+
+/* Main function (will be separated to each platform) */
+int main(int argc, char *argv[])
+{
+ PeryanMain();
+ return 0;
+}
+
+/* In case of debugging */
+ void printNum(int num) {
+ printf("%d\n", num);
+ return;
+}
+
+/* Begin implementation of built-in String */
+
+struct String {
+ int length;
+ int capacity;
+ char *str;
+};
+
+void *PRMalloc(unsigned int size)
+{
+ return malloc(size);
+}
+
+void *PRFree(void *ptr)
+{
+ return PRFree(ptr);
+}
+
+void *PRRealloc(void *ptr, int size)
+{
+ return realloc(ptr, size);
+}
+
+struct String *PRStringConstructorCStr(char *cStr)
+{
+ struct String *res = NULL;
+
+ res = PRMalloc(sizeof(struct String));
+
+ res->length = strlen(cStr);
+
+ res->capacity = res->length + 1;
+
+ res->str = PRMalloc(res->capacity);
+ strcpy(res->str, cStr);
+
+ return res;
+}
+
+struct String *PRStringConstructorInt(int num)
+{
+ char *str[20];
+ sprintf((char *)str, "%d", num);
+ return PRStringConstructorCStr((char *)str);
+}
+
+struct String *PRStringConcatenate(struct String *lhs, struct String *rhs)
+{
+ struct String *res = NULL;
+
+ res = PRMalloc(sizeof(struct String));
+
+ res->length = lhs->length + rhs->length;
+ res->capacity = res->length + 1;
+
+ res->str = PRMalloc(res->capacity);
+ strcpy(res->str, lhs->str);
+ strcpy(res->str + lhs->length, rhs->str);
+
+ return res;
+}
+
+void PRStringDestructor(struct String *str)
+{
+ PRFree(str->str);
+ PRFree(str);
+}
+
+int strlen__(struct String *str)
+{
+ return str->length;
+}
+
+/* End implementation of built-in String */
+
+/*
+ * Array is Peryan's sole polymorphic type so that far closer to type system itself,
+ * so it should be implemented in the code generator
+ *
+ */
+
+void mes(struct String *str)
+{
+ printf("%s\n", str->str);
+ return;
+}
+
+struct String *strmid(struct String *str, int start, int length)
+{
+ int i = 0;
+ struct String *res = NULL;
+
+ res = PRMalloc(sizeof(struct String));
+
+ res->length = length;
+ res->capacity = res->length + 1;
+
+ res->str = PRMalloc(res->capacity);
+ for (i = 0; i < length; ++i) {
+ res->str[i] = str->str[start + i];
+ }
+ res->str[length] = 0;
+
+ return res;
+}
+
+/* extern instr :: String -> Int -> String -> Int */
+int instr(struct String *haystack, int start, struct String *needle)
+{
+ /* TODO: there's famous faster algorithm called KMP */
+
+ int i = 0, j = 0, res = -1;
+ for (i = start; i < haystack->length; ++i) {
+ res = i;
+
+ for (j = 0; j < needle->length; ++j) {
+ if (i + j >= haystack->length)
+ break;
+
+ if (haystack->str[i + j] != needle->str[j]) {
+ res = -1;
+ break;
+ }
+ }
+
+ if (res != -1) {
+ return res;
+ }
+ }
+
+ return -1;
+}
+
+int rnd(int maxRange) {
+ static unsigned int x = 123456789;
+ static unsigned int y = 362436069;
+ static unsigned int z = 521288629;
+ static unsigned int w = 88675123;
+ unsigned int t;
+
+ t = x ^ (x << 11);
+ x = y; y = z; z = w;
+ w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
+ return w % maxRange;
+}
+
+int PRIntConstructor(struct String *str)
+{
+ int res;
+ sscanf(str->str, "%d", &res);
+ return res;
+}
Oops, something went wrong.

0 comments on commit f8a76a4

Please sign in to comment.