Nuva

維基百科,自由的百科全書

Nuva語言是一種面向對象動態腳本語言。Nuva對應漢語的「女媧」(中國上古時代的神話傳說人物)一詞。

設計目的

設計目的是用於基於模板的代碼生成。除了用於代碼生成領域外,也能用於開發應用程式,如文本和數據處理、GUI應用程式等。

特點

  • 語法簡單靈活:採用類似虛擬碼演算法/偽碼的語法風格,結構之間可以任意嵌套,關鍵字和運算符兼容大部分現有的程式語言,非常容易學習。
  <.
    if (a = b && c == d or e <> f)
      ?? foo()
     
      function foo()
        Result = 'foo'
      end function
    end if
  .>
  • 動態的,無類型約束:採用動態類型,使用時不需聲明類型,賦值計算時自動進行類型轉換,如下:
  <.
    var a = '1'
    a ++
    ?? 'a' ~ a
    // 结果为: a2
  .>
  • 支持面向對象的編程方法:支持繼承性和多態性。
  • 支持自動垃圾回收:程式設計師不需顯式釋放其所創建的對象。
  • 模板專用的語言元素
    • 模板標記(「<.」、「.>」、「[.」、「.]」)可以混合配對使用,對于格式要求很嚴格的場合非常有用。
[.='Hello, Nuva!'.]
<.='Hello, Nuva!'.>
[.='Hello, Nuva!'.>
<.='Hello, Nuva!'.]
    • 凡「[.」之前的所有空白字符原樣輸出,「.]」之後的所有空白字符(包括換行)也原樣輸出;
    • 如果行首到「<.」之間均為空白字符,則該部分空白字符不輸出,否則原樣輸出;
    • 如果「.>」到行尾之間均為空白字符,則該部分空白字符和換行不輸出,否則也原樣輸出。
    • 特有的fileassign結構能夠非常方便的對輸出進行組合、分解,從而方便了模板的編寫。

Nuva虛擬機特點

  <.
    var text = System.File.Load('Regex_Test.nuva')
    foreach(str = text.RegexMatchs('\w+', ''))
      ?? str
    end foreach
  .>

輸出如下的結果:

  var
  text
  System
  File
  Load
  Regex_Test
  nuva
  foreach
  str
  text
  RegexMatches
  w
  str
  end
  foreach

代碼示例

Hello, Nuva!

  <.. "Hello, Nuva!" Demo ..>
  <.
  //======================================
  //	Hello, Nuva! (1)
  //======================================
  ?? 'Hello, Nuva!'
  
  /*======================================
    	Hello, Nuva! (2)
  ======================================*/
  function HelloNuva()
    ?? "Hello, Nuva!";
  end function
  
  HelloNuva();
  
  /*======================================
    	Hello, Nuva! (3)
  ======================================*/
  class Nuva()
    function Hello()
      ?? 'Hello, Nuva!';
    end function
  end class
  
  var n = Nuva();
  n.Hello();
  .>

foreach | O/R Mapping

  <.
  function Foreach_Demo()
    // Load Schema from a Xml file
    var schema = System.Data.LoadSchema(
        System.Path.ProjectPath ~ '..\Northwind\Northwind.xobject'
      );
  
    ?? '--------------------'
    ?? 'Tables Order by Name'
    ?? '--------------------'
    foreach(table = schema.Tables.OrderbyName)
      ?? table.Name
    end foreach
  
    ?? '---------------------------------'
    ?? 'Tables Filter by Name.Length < 10'
    ?? '---------------------------------'
    foreach(table = schema.Tables | table.Name.Length < 10)
      ?? table.Name
    end foreach
  end function
  .>

file | 生成文件

  <.
  function File_Demo()
    ?? \r\n ~ '--Read file and Output here--'
    file('codeexamples.nuvaproj') end file
  
    // Read file and write to 'Target', overwrite it if exist
    file('codeexamples.nuvaproj', true)
      Target = 'temp.target'
    end file
    
    ?? \r\n ~ '--Read file dynamically and Output here--'
    file('')
      FileName = System.Path.ProjectPath ~ 'output\temp.target'
    end file
    
    // Delete file
    System.File.Delete(System.Path.ProjectPath ~ 'output\temp.target')
  end function
  .>

assign | 捕獲輸出

  <.
  function Assign_Demo()
    // 'Result' assigned from the output in the assign statement
    assign(Result).]
      Generate Text ... @[.=System.Now.] ...
  <.end assign
  end function
  .>

函數 | 遞歸調用

  <.
  /*--------------------------------------------------------
  	Factorial
  --------------------------------------------------------*/
  function Factorial ( N )
    if (N <= 1)
      Result = 1;
    else
      Result = N * Factorial(N - 1); // Recursion Call
    end if;
  end function;
  .>

類 | 多態性

  <.
  function Class_Demo()
    class ClassA()
      function Do()
        this.DynDo()
      end function
      
      function DynDo()
        ?? 'ClassA'
      end function
    end class
    
    class ClassB = ClassA()
      function DynDo()
        ?? 'ClassB'
      end function
    end class
  
    var c1 = ClassA()
    var c2 = ClassB()
    c1.Do()
    c2.Do()
  end function
  .>

外部連結