-
-
Save Yardanico/61061c5dc17a7a683aa392d8babdfb99 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const std = @import("std"); | |
const fmt = std.fmt; | |
const Parser = struct { | |
input: []const u8, | |
ch: u8, | |
pos: usize, | |
len: usize, | |
fn init(input: []const u8) Parser { | |
return Parser{ | |
.input = input, | |
.ch = input[0], | |
.pos = 0, | |
.len = input.len, | |
}; | |
} | |
fn parse_factor(self: *Parser) !f64 { | |
var res: f64 = 0.0; | |
if (self.ch == '0') { | |
return self.parse_factor(); | |
} | |
else if (self.ch == '1') { | |
return -self.parse_factor(); | |
} | |
if (true) { | |
res = try fmt.parseFloat(f64, self.input[0..self.len-1]); | |
} | |
else { | |
return res; | |
} | |
return res; | |
} | |
}; | |
fn eval(input: []u8) !f64 { | |
var p = Parser.init(input); | |
return try p.parse_factor(); | |
} | |
pub fn main() !void { | |
const input = "100"[0..]; | |
var p = Parser.init(input); | |
_ = try p.parse_factor(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment