氧化物编程语言

  • L0_791572
    了解作者
  • 78.6KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-05-19 08:20
    上传日期
氧化物编程语言解释了受 Rust 影响的语法的类 C 语言。最新发布示例程序/// recursive function calls to compute n-th/// fibonacci sequence numberfnfib (n: int) -> int {if n&lt;=1 {return n; }returnfib (n-2 )+fib (n-1 ); }let nums= vec< int> [];forletmut i=0 ; i<30 i+=1>) {forletmut i=1 ; i&lt; input.len (); i+=1 {let cur= input[i];letmut j= i-1 ;while input[j]&gt; cur {let temp= input[j+1 ]; input[j+1 ]= input[j];
tuqqu-oxide-lang.zip
内容介绍
# Oxide Programming Language Interpreted scripting language with a Rust influenced syntax. [Latest release][latest-releases] ## Example programs ```rust /// structs struct Circle { // struct declaration pub radius: float, // public field center: Point, // private field } impl Circle { // struct implementation const PI: float = 3.14159; // private associated constant pub fn new(r: float, c: Point) -> Self { // public static method return Self { radius: r, center: c, }; } } struct Point { pub x: int, pub y: int, } /// traits trait Shape { // trait declaration fn calc_area(self) -> float; // trait methods are always public } impl Shape for Circle { // trait implementation fn calc_area(self) -> float { return Self::PI * self.radius * self.radius; } } // program entry point fn main() { let a: Shape = Circle::new(200.0, Point { x: 1, y: 5 }); let area = a.calc_area(); println("circle area: " + area as str); // type casting } ``` ```rust /// enums enum Ordering { Less, Equal, Greater } impl Ordering { pub fn compare(a: int, b: int) -> Self { return match true { a < b => Self::Less, a == b => Self::Equal, a > b => Self::Greater, }; } } fn main() { let order = Ordering::compare(10, 5); // Ordering::Greater } ``` ```rust /// sorting a vector using /// insertion sort fn insertion_sort(input: vec<int>) { for i in 1..input.len() { let cur = input[i]; let mut j = i - 1; while input[j] > cur { let temp = input[j + 1]; input[j + 1] = input[j]; input[j] = temp; if j == 0 { break; } j -= 1; } } } fn main() { let input: vec<int> = vec[4, 13, 0, 3, -3, 4, 19, 1]; insertion_sort(input); dbg(input); // [vec] [-3, 0, 1, 3, 4, 4, 13, 19] } ``` [More examples][examples] ## Usage Download the [latest release][latest-releases] and put the executable in your PATH. ``` USAGE: oxide [FLAGS] [FILE] FLAGS: -h, --help Prints help -v, --version Prints version -r, --repl Run REPL -t, --allow-top-level Allow top-level instructions ARGS: <FILE> Script file to run EXAMPLE: oxide script.ox ``` ## Building from source If your architecture is not supported by the pre-built binaries you can build the interpreter from the source code yourself. Make sure you have Rust installed. ```shell git clone https://github.com/tuqqu/oxide-lang.git cd oxide-lang cargo install --path . # copies binary to /.cargo/bin/ # to uninstall run `cargo uninstall` # you can now run it with oxide script.ox ``` # Quick Overview * [Program Structure](#program-structure) * [Variables and Type System](#variables-and-type-system) * [Mutability](#mutability) * [Shadowing](#shadowing) * [Casting](#casting) * [Control Flow and Loops](#control-flow-and-loops) * [If](#if) * [Match](#match) * [While](#while) * [Loop](#loop) * [For](#for) * [Functions](#functions) * [Closures](#closures) * [Structs](#structs) * [Public and Private](#public-and-private) * [Traits](#traits) * [Enums](#enums) * [Vectors](#vectors) * [Range Expressions](#range-expressions) * [Constants](#constants) * [Operators](#operators) * [Unary](#unary) * [Binary](#binary) * [Comments](#comments) * [Standard library](#standard-library) ## Program Structure In Oxide, the entry point of a program is a function named `main`. ```rust fn main() { // code goes here } ``` On the top level only item (`const`, `fn`, `struct`, `enum`, `trait`, `impl`) declarations are possible. ```rust struct S {} trait T {} impl T for S {} enum E {} const C = 0; fn f() {} ``` ## Variables and Type System There are ten types in Oxide: `nil`, `int`, `float`, `bool`, `str`, `fn(T) -> T`, `vec<T>`, `any` and user-defined types (via [`structs`](#structs) and [`enums`](#enums)). See [type system][type-system] Variables are typed either explicitly: ```rust let x: int; // type = int let y: str = "hello world"; // type = str let nums: vec<int> = vec[1, 2]; // type = vec<int> let jane: Person = Person { name: "Jane" }; // type = Person // functions are their own type let double: fn(int) -> int = fn (x: int) -> int { return x * 2; }; //^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ // type lambda ``` or their type implicitly inferred: ```rust let x = vec["h", "i"]; // inferred as vec<str> let dog = Dog::new("Good Boy"); // inferred as Dog let ordering = Ordering::Less; // inferred as Ordering let f = fn (x: int) { ... }; // inferred as fn(int) let x; // inferred as vec<int> x = 0..=100; // the first time it is being assigned ``` ### Mutability Variables are immutable by default: ```rust let x = 100; x += 1; //! error, x is immutable ``` To be mutable they must be defined with `mut` keyword. ```rust let mut x: str = "hello"; x += " world"; // ok ``` ### Shadowing Variables can be shadowed. Each variable declaration "shadows" the previous one: ```rust let x: int = 100; let x: vec<int> = vec[1, 2]; ``` ### Casting Explicit type conversion, i.e. type casting, can be performed using the `as` keyword. Primitive types `int`, `float`, `nil`, `bool`, `str` can be cast to other primitive types. ```rust let x = 32 as str; // typeof(x) = str, x = "32" let x = "350" as int; // typeof(x) = int, x = 350 let x = 0.0 as bool; // typeof(x) = bool, x = false let x = 10; "this is x: " + x as str; // values must be cast to str for concatenation ``` Using non-primitives (vector `vec<T>`, function `fn(T) -> T`, enum, and struct types) will result in a type error. `any` type must be explicitly cast to be used in expressions: ```rust let x: any = 1; let d = 100 + x as int; // omitting cast would produce an error ``` ## Control Flow and Loops Parentheses are not needed around conditions. The statement body must be enclosed in curly braces. ### If `if` statement is rather classic. It supports `else if` and `else` branches. ```rust if x >= 100 { println("x is more than 100"); } else if x <= 100 && x > 0 { println("x is less than 100, but positive"); } else { println("x a non-positive number"); } ``` ### Match `match` expression returns the first matching arm evaluated value. Unlike other control flow statements, `match` is an expression and therefore must be terminated with a semicolon. ```rust let direction = match get_direction() { "north" => 0, "east" => 90, "south" => 180, "west" => 270, }; ``` `match true` can be used to make more generalised comparisons. ```rust let age = 40; let description: str = match true { age > 19 => "adult", age >= 13 && x <= 19 => "teenager", age < 13 => "kid", }; ``` `match` can be used with [enums](#enums) ```rust enum HttpStatus { NotFound, NotModified, Ok } impl HttpStatus { fn code(status: Self) -> int { return match status { Self::NotFound => 404, Self::NotModified => 304, Self::Ok => 200, }; } } fn main() { let status = HttpStatus::code(HttpStatus::Ok); // 200 } ``` ### While There are three loops in Oxide: `while`, `loop` and `for`. Loops support `break` and `continue` statements. `while` statement is rather usual. ```rust while
评论
    相关推荐
    • rusti:Rust编程语言的REPL
      rusti:Rust编程语言的REPL
    • learn-rust, 帮助我学习 Rust 编程语言的项目.zip
      learn-rust, 帮助我学习 Rust 编程语言的项目 通过面试问题学习 Rust这个reposity是我第一次认真尝试学习 Rust的产品。我没有一个特别的probem来处理,所以我使用了工作面试编码问题在互联网上找到。 特别是,"黑了...
    • 用于Rust编程语言的GLFW绑定和包装器。-Rust开发
      glfw-rs Rust编程语言的GLFW绑定和包装。 示例extern crate glfw; 使用glfw :: {Action,Context,Key}; fn main(){le glfw-rs Rust编程语言的GLFW绑定和包装。 示例extern crate glfw; 使用glfw :: {Action,...
    • learning-rust:通过rust编程语言pdf进行工作
      学习锈 这是处理rust文档.pdf的回购
    • rust-koans:Rust编程语言的Koans
      鲁斯特·科恩斯(Rust Koans) 结构 koans按文件划分为多个区域,arrays.rs中覆盖了数组,booleans.rs中引入了布尔值,等等。它们在path_to_enlightenment.rs文件中按顺序显示。 每个koan都会建立您对Rust的知识并...
    • 图书:Rust编程语言
      Rust编程语言 该存储库包含“ The Rust Programming Language”这本书的源代码。 。 您还可以免费在线阅读该书。 请参阅该书随附的最新, 或Rust版本。 请注意,这些版本中的问题可能已经在此存储库中修复,因为...
    • 使用rust编程语言深入研究攻击性安全-抢先体验-Rust开发
      使用锈编程语言深入研究进攻性安全-抢先体验Black Hat Rust-抢先使用Rust编程语言深入研究进攻性安全立即购买本书! 总结无论是在电影中还是在主流媒体中,黑客都经常被浪漫化:他们被描绘成黑魔法师,讨厌的罪犯,...
    • the-book:Rust编程语言代码
      通用编程概念 3.1。变量与可变性 3.2。资料类型 3.3。功能 3.4。评论 3.5。控制流 4.了解所有权 4.1。什么是所有权? 4.2。参考和借阅 4.3。切片类型 5.使用结构来构造相关数据 5.1。定义和实例化结构 5.2。使用...
    • learning-rust:此仓库用于学习rust编程语言
      learning-rust:此仓库用于学习rust编程语言
    • Rust编程语言的惯用语intify包装器。-Rust开发
      inotify-rs Rust编程语言的惯用语inotify包装器。 木箱化 inotify-rs Rust编程语言的惯用语inotify包装器。 木箱化 使用std :: env; 使用inotify :: {EventMask,WatchMask,Inotify,}; fn main(){let mut ...