mirror of
https://github.com/KevinMidboe/linguist.git
synced 2025-10-29 17:50:22 +00:00
Merge master
This commit is contained in:
@@ -63,6 +63,8 @@ https://github.com/Varriount/NimLime:
|
||||
- source.nimrodcfg
|
||||
https://github.com/angryant0007/VBDotNetSyntax:
|
||||
- source.vbnet
|
||||
https://github.com/anunayk/cool-tmbundle:
|
||||
- source.cool
|
||||
https://github.com/aroben/ada.tmbundle/raw/c45eed4d5f98fe3bcbbffbb9e436601ab5bbde4b/Syntaxes/Ada.plist:
|
||||
- source.ada
|
||||
https://github.com/aroben/ruby.tmbundle@4636a3023153c3034eb6ffc613899ba9cf33b41f:
|
||||
@@ -137,6 +139,8 @@ https://github.com/fsharp/fsharpbinding:
|
||||
- source.fsharp
|
||||
https://github.com/gingerbeardman/monkey.tmbundle:
|
||||
- source.monkey
|
||||
https://github.com/alkemist/gradle.tmbundle:
|
||||
- source.groovy.gradle
|
||||
https://github.com/guillermooo/dart-sublime-bundle/raw/master/Dart.tmLanguage:
|
||||
- source.dart
|
||||
https://github.com/harrism/sublimetext-cuda-cpp/raw/master/cuda-c%2B%2B.tmLanguage:
|
||||
@@ -408,3 +412,7 @@ https://github.com/vmg/zephir-sublime:
|
||||
- source.php.zephir
|
||||
https://github.com/whitequark/llvm.tmbundle:
|
||||
- source.llvm
|
||||
https://github.com/lsf37/Isabelle.tmbundle:
|
||||
- source.isabelle.theory
|
||||
https://github.com/eregon/oz-tmbundle:
|
||||
- source.oz
|
||||
|
||||
@@ -4,4 +4,5 @@ require 'linguist/heuristics'
|
||||
require 'linguist/language'
|
||||
require 'linguist/repository'
|
||||
require 'linguist/samples'
|
||||
require 'linguist/shebang'
|
||||
require 'linguist/version'
|
||||
|
||||
@@ -3,6 +3,25 @@ require 'linguist/tokenizer'
|
||||
module Linguist
|
||||
# Language bayesian classifier.
|
||||
class Classifier
|
||||
# Public: Use the classifier to detect language of the blob.
|
||||
#
|
||||
# blob - An object that quacks like a blob.
|
||||
# possible_languages - Array of Language objects
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# Classifier.call(FileBlob.new("path/to/file"), [
|
||||
# Language["Ruby"], Language["Python"]
|
||||
# ])
|
||||
#
|
||||
# Returns an Array of Language objects, most probable first.
|
||||
def self.call(blob, possible_languages)
|
||||
language_names = possible_languages.map(&:name)
|
||||
classify(Samples.cache, blob.data, language_names).map do |name, _|
|
||||
Language[name] # Return the actual Language objects
|
||||
end
|
||||
end
|
||||
|
||||
# Public: Train classifier that data is a certain language.
|
||||
#
|
||||
# db - Hash classifier database object
|
||||
|
||||
@@ -1,173 +1,149 @@
|
||||
module Linguist
|
||||
# A collection of simple heuristics that can be used to better analyze languages.
|
||||
class Heuristics
|
||||
ACTIVE = true
|
||||
|
||||
# Public: Given an array of String language names,
|
||||
# apply heuristics against the given data and return an array
|
||||
# of matching languages, or nil.
|
||||
# Public: Use heuristics to detect language of the blob.
|
||||
#
|
||||
# data - Array of tokens or String data to analyze.
|
||||
# languages - Array of language name Strings to restrict to.
|
||||
# blob - An object that quacks like a blob.
|
||||
# possible_languages - Array of Language objects
|
||||
#
|
||||
# Returns an array of Languages or []
|
||||
def self.find_by_heuristics(data, languages)
|
||||
if active?
|
||||
result = []
|
||||
# Examples
|
||||
#
|
||||
# Heuristics.call(FileBlob.new("path/to/file"), [
|
||||
# Language["Ruby"], Language["Python"]
|
||||
# ])
|
||||
#
|
||||
# Returns an Array of languages, or empty if none matched or were inconclusive.
|
||||
def self.call(blob, languages)
|
||||
data = blob.data
|
||||
|
||||
if languages.all? { |l| ["Perl", "Prolog"].include?(l) }
|
||||
result = disambiguate_pl(data)
|
||||
end
|
||||
if languages.all? { |l| ["ECL", "Prolog"].include?(l) }
|
||||
result = disambiguate_ecl(data)
|
||||
end
|
||||
if languages.all? { |l| ["IDL", "Prolog"].include?(l) }
|
||||
result = disambiguate_pro(data)
|
||||
end
|
||||
if languages.all? { |l| ["Common Lisp", "OpenCL"].include?(l) }
|
||||
result = disambiguate_cl(data)
|
||||
end
|
||||
if languages.all? { |l| ["Hack", "PHP"].include?(l) }
|
||||
result = disambiguate_hack(data)
|
||||
end
|
||||
if languages.all? { |l| ["Scala", "SuperCollider"].include?(l) }
|
||||
result = disambiguate_sc(data)
|
||||
end
|
||||
if languages.all? { |l| ["AsciiDoc", "AGS Script"].include?(l) }
|
||||
result = disambiguate_asc(data)
|
||||
end
|
||||
if languages.all? { |l| ["FORTRAN", "Forth"].include?(l) }
|
||||
result = disambiguate_f(data)
|
||||
end
|
||||
if languages.all? { |l| ["F#", "Forth", "GLSL"].include?(l) }
|
||||
result = disambiguate_fs(data)
|
||||
end
|
||||
return result
|
||||
@heuristics.each do |heuristic|
|
||||
return Array(heuristic.call(data)) if heuristic.matches?(languages)
|
||||
end
|
||||
|
||||
[] # No heuristics matched
|
||||
end
|
||||
|
||||
# Internal: Define a new heuristic.
|
||||
#
|
||||
# languages - String names of languages to disambiguate.
|
||||
# heuristic - Block which takes data as an argument and returns a Language or nil.
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# disambiguate "Perl", "Prolog" do |data|
|
||||
# if data.include?("use strict")
|
||||
# Language["Perl"]
|
||||
# elsif data.include?(":-")
|
||||
# Language["Prolog"]
|
||||
# end
|
||||
# end
|
||||
#
|
||||
def self.disambiguate(*languages, &heuristic)
|
||||
@heuristics << new(languages, &heuristic)
|
||||
end
|
||||
|
||||
# Internal: Array of defined heuristics
|
||||
@heuristics = []
|
||||
|
||||
# Internal
|
||||
def initialize(languages, &heuristic)
|
||||
@languages = languages
|
||||
@heuristic = heuristic
|
||||
end
|
||||
|
||||
# Internal: Check if this heuristic matches the candidate languages.
|
||||
def matches?(candidates)
|
||||
candidates.all? { |l| @languages.include?(l.name) }
|
||||
end
|
||||
|
||||
# Internal: Perform the heuristic
|
||||
def call(data)
|
||||
@heuristic.call(data)
|
||||
end
|
||||
|
||||
disambiguate "Objective-C", "C++", "C" do |data|
|
||||
if (/@(interface|class|protocol|property|end|synchronised|selector|implementation)\b/.match(data))
|
||||
Language["Objective-C"]
|
||||
elsif (/^\s*#\s*include <(cstdint|string|vector|map|list|array|bitset|queue|stack|forward_list|unordered_map|unordered_set|(i|o|io)stream)>/.match(data) ||
|
||||
/^\s*template\s*</.match(data) || /^[^@]class\s+\w+/.match(data) || /^[^@](private|public|protected):$/.match(data) || /std::.+$/.match(data))
|
||||
Language["C++"]
|
||||
end
|
||||
end
|
||||
|
||||
# .h extensions are ambiguous between C, C++, and Objective-C.
|
||||
# We want to shortcut look for Objective-C _and_ now C++ too!
|
||||
#
|
||||
# Returns an array of Languages or []
|
||||
def self.disambiguate_c(data)
|
||||
matches = []
|
||||
if data.include?("@interface")
|
||||
matches << Language["Objective-C"]
|
||||
elsif data.include?("#include <cstdint>")
|
||||
matches << Language["C++"]
|
||||
end
|
||||
matches
|
||||
end
|
||||
|
||||
def self.disambiguate_pl(data)
|
||||
matches = []
|
||||
disambiguate "Perl", "Prolog" do |data|
|
||||
if data.include?("use strict")
|
||||
matches << Language["Perl"]
|
||||
Language["Perl"]
|
||||
elsif data.include?(":-")
|
||||
matches << Language["Prolog"]
|
||||
Language["Prolog"]
|
||||
end
|
||||
matches
|
||||
end
|
||||
|
||||
def self.disambiguate_ecl(data)
|
||||
matches = []
|
||||
disambiguate "ECL", "Prolog" do |data|
|
||||
if data.include?(":-")
|
||||
matches << Language["Prolog"]
|
||||
Language["Prolog"]
|
||||
elsif data.include?(":=")
|
||||
matches << Language["ECL"]
|
||||
Language["ECL"]
|
||||
end
|
||||
matches
|
||||
end
|
||||
|
||||
def self.disambiguate_pro(data)
|
||||
matches = []
|
||||
if (data.include?(":-"))
|
||||
matches << Language["Prolog"]
|
||||
disambiguate "IDL", "Prolog" do |data|
|
||||
if data.include?(":-")
|
||||
Language["Prolog"]
|
||||
else
|
||||
matches << Language["IDL"]
|
||||
Language["IDL"]
|
||||
end
|
||||
matches
|
||||
end
|
||||
|
||||
def self.disambiguate_ts(data)
|
||||
matches = []
|
||||
if (data.include?("</translation>"))
|
||||
matches << Language["XML"]
|
||||
else
|
||||
matches << Language["TypeScript"]
|
||||
end
|
||||
matches
|
||||
end
|
||||
|
||||
def self.disambiguate_cl(data)
|
||||
matches = []
|
||||
disambiguate "Common Lisp", "OpenCL", "Cool" do |data|
|
||||
if data.include?("(defun ")
|
||||
matches << Language["Common Lisp"]
|
||||
Language["Common Lisp"]
|
||||
elsif /^class/x.match(data)
|
||||
Language["Cool"]
|
||||
elsif /\/\* |\/\/ |^\}/.match(data)
|
||||
matches << Language["OpenCL"]
|
||||
Language["OpenCL"]
|
||||
end
|
||||
matches
|
||||
end
|
||||
|
||||
def self.disambiguate_r(data)
|
||||
matches = []
|
||||
matches << Language["Rebol"] if /\bRebol\b/i.match(data)
|
||||
matches << Language["R"] if data.include?("<-")
|
||||
matches
|
||||
end
|
||||
|
||||
def self.disambiguate_hack(data)
|
||||
matches = []
|
||||
disambiguate "Hack", "PHP" do |data|
|
||||
if data.include?("<?hh")
|
||||
matches << Language["Hack"]
|
||||
Language["Hack"]
|
||||
elsif /<?[^h]/.match(data)
|
||||
matches << Language["PHP"]
|
||||
Language["PHP"]
|
||||
end
|
||||
matches
|
||||
end
|
||||
|
||||
def self.disambiguate_sc(data)
|
||||
matches = []
|
||||
if (/\^(this|super)\./.match(data) || /^\s*(\+|\*)\s*\w+\s*{/.match(data) || /^\s*~\w+\s*=\./.match(data))
|
||||
matches << Language["SuperCollider"]
|
||||
disambiguate "Scala", "SuperCollider" do |data|
|
||||
if /\^(this|super)\./.match(data) || /^\s*(\+|\*)\s*\w+\s*{/.match(data) || /^\s*~\w+\s*=\./.match(data)
|
||||
Language["SuperCollider"]
|
||||
elsif /^\s*import (scala|java)\./.match(data) || /^\s*val\s+\w+\s*=/.match(data) || /^\s*class\b/.match(data)
|
||||
Language["Scala"]
|
||||
end
|
||||
if (/^\s*import (scala|java)\./.match(data) || /^\s*val\s+\w+\s*=/.match(data) || /^\s*class\b/.match(data))
|
||||
matches << Language["Scala"]
|
||||
end
|
||||
matches
|
||||
end
|
||||
|
||||
def self.disambiguate_asc(data)
|
||||
matches = []
|
||||
matches << Language["AsciiDoc"] if /^=+(\s|\n)/.match(data)
|
||||
matches
|
||||
disambiguate "AsciiDoc", "AGS Script" do |data|
|
||||
Language["AsciiDoc"] if /^=+(\s|\n)/.match(data)
|
||||
end
|
||||
|
||||
def self.disambiguate_f(data)
|
||||
matches = []
|
||||
disambiguate "FORTRAN", "Forth" do |data|
|
||||
if /^: /.match(data)
|
||||
matches << Language["Forth"]
|
||||
Language["Forth"]
|
||||
elsif /^([c*][^a-z]| subroutine\s)/i.match(data)
|
||||
matches << Language["FORTRAN"]
|
||||
Language["FORTRAN"]
|
||||
end
|
||||
matches
|
||||
end
|
||||
|
||||
def self.disambiguate_fs(data)
|
||||
matches = []
|
||||
disambiguate "F#", "Forth", "GLSL" do |data|
|
||||
if /^(: |new-device)/.match(data)
|
||||
matches << Language["Forth"]
|
||||
Language["Forth"]
|
||||
elsif /^(#light|import|let|module|namespace|open|type)/.match(data)
|
||||
matches << Language["F#"]
|
||||
Language["F#"]
|
||||
elsif /^(#include|#pragma|precision|uniform|varying|void)/.match(data)
|
||||
matches << Language["GLSL"]
|
||||
Language["GLSL"]
|
||||
end
|
||||
matches
|
||||
end
|
||||
|
||||
def self.active?
|
||||
!!ACTIVE
|
||||
end
|
||||
disambiguate "Gosu", "JavaScript" do |data|
|
||||
Language["Gosu"] if /^uses java\./.match(data)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,6 +10,8 @@ require 'linguist/heuristics'
|
||||
require 'linguist/samples'
|
||||
require 'linguist/file_blob'
|
||||
require 'linguist/blob_helper'
|
||||
require 'linguist/strategy/filename'
|
||||
require 'linguist/shebang'
|
||||
|
||||
module Linguist
|
||||
# Language names that are recognizable by GitHub. Defined languages
|
||||
@@ -91,6 +93,13 @@ module Linguist
|
||||
language
|
||||
end
|
||||
|
||||
STRATEGIES = [
|
||||
Linguist::Strategy::Filename,
|
||||
Linguist::Shebang,
|
||||
Linguist::Heuristics,
|
||||
Linguist::Classifier
|
||||
]
|
||||
|
||||
# Public: Detects the Language of the blob.
|
||||
#
|
||||
# blob - an object that includes the Linguist `BlobHelper` interface;
|
||||
@@ -98,61 +107,22 @@ module Linguist
|
||||
#
|
||||
# Returns Language or nil.
|
||||
def self.detect(blob)
|
||||
name = blob.name.to_s
|
||||
|
||||
# Bail early if the blob is binary or empty.
|
||||
return nil if blob.likely_binary? || blob.binary? || blob.empty?
|
||||
|
||||
# A bit of an elegant hack. If the file is executable but extensionless,
|
||||
# append a "magic" extension so it can be classified with other
|
||||
# languages that have shebang scripts.
|
||||
extensions = FileBlob.new(name).extensions
|
||||
if extensions.empty? && blob.mode && (blob.mode.to_i(8) & 05) == 05
|
||||
name += ".script!"
|
||||
end
|
||||
|
||||
# Find languages that match based on filename.
|
||||
possible_languages = find_by_filename(name)
|
||||
|
||||
if possible_languages.length == 1
|
||||
# Simplest and most common case, we can just return the one match based
|
||||
# on extension
|
||||
possible_languages.first
|
||||
|
||||
# If there is more than one possible language with that extension (or no
|
||||
# extension at all, in the case of extensionless scripts), we need to
|
||||
# continue our detection work
|
||||
else
|
||||
# Matches possible_languages.length == 0 || possible_languages.length > 0
|
||||
data = blob.data
|
||||
|
||||
# Check if there's a shebang line and use that as authoritative
|
||||
if (result = find_by_shebang(data)) && !result.empty?
|
||||
return result.first
|
||||
|
||||
# More than one language with that extension. We need to make a choice.
|
||||
elsif possible_languages.length > 1
|
||||
|
||||
# First try heuristics
|
||||
|
||||
possible_language_names = possible_languages.map(&:name)
|
||||
heuristic_languages = Heuristics.find_by_heuristics(data, possible_language_names)
|
||||
|
||||
# If there are multiple possible languages returned from heuristics
|
||||
# then reduce language candidates for Bayesian classifier here.
|
||||
if heuristic_languages.size > 1
|
||||
possible_language_names = heuristic_languages.map(&:name)
|
||||
end
|
||||
|
||||
if heuristic_languages.size == 1
|
||||
return heuristic_languages.first
|
||||
# Lastly, fall back to the probabilistic classifier.
|
||||
elsif classified = Classifier.classify(Samples.cache, data, possible_language_names).first
|
||||
# Return the actual Language object based of the string language name (i.e., first element of `#classify`)
|
||||
return Language[classified[0]]
|
||||
end
|
||||
# Call each strategy until one candidate is returned.
|
||||
STRATEGIES.reduce([]) do |languages, strategy|
|
||||
candidates = strategy.call(blob, languages)
|
||||
if candidates.size == 1
|
||||
return candidates.first
|
||||
elsif candidates.size > 1
|
||||
# More than one candidate was found, pass them to the next strategy.
|
||||
candidates
|
||||
else
|
||||
# No candiates were found, pass on languages from the previous strategy.
|
||||
languages
|
||||
end
|
||||
end
|
||||
end.first
|
||||
end
|
||||
|
||||
# Public: Get all Languages
|
||||
@@ -229,20 +199,26 @@ module Linguist
|
||||
@extension_index[extname]
|
||||
end
|
||||
|
||||
# Public: Look up Languages by shebang line.
|
||||
# DEPRECATED
|
||||
def self.find_by_shebang(data)
|
||||
@interpreter_index[Shebang.interpreter(data)]
|
||||
end
|
||||
|
||||
# Public: Look up Languages by interpreter.
|
||||
#
|
||||
# data - Array of tokens or String data to analyze.
|
||||
# interpreter - String of interpreter name
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# Language.find_by_shebang("#!/bin/bash\ndate;")
|
||||
# Language.find_by_interpreter("bash")
|
||||
# # => [#<Language name="Bash">]
|
||||
#
|
||||
# Returns the matching Language
|
||||
def self.find_by_shebang(data)
|
||||
@interpreter_index[Linguist.interpreter_from_shebang(data)]
|
||||
def self.find_by_interpreter(interpreter)
|
||||
@interpreter_index[interpreter]
|
||||
end
|
||||
|
||||
|
||||
# Public: Look up Language by its name or lexer.
|
||||
#
|
||||
# name - The String name of the Language
|
||||
|
||||
@@ -341,6 +341,8 @@ C:
|
||||
color: "#555"
|
||||
extensions:
|
||||
- .c
|
||||
- .C
|
||||
- .H
|
||||
- .cats
|
||||
- .h
|
||||
- .idc
|
||||
@@ -369,9 +371,6 @@ C++:
|
||||
- cpp
|
||||
extensions:
|
||||
- .cpp
|
||||
- .C
|
||||
- .CPP
|
||||
- .H
|
||||
- .c++
|
||||
- .cc
|
||||
- .cxx
|
||||
@@ -574,6 +573,12 @@ Component Pascal:
|
||||
- objectpascal
|
||||
ace_mode: pascal
|
||||
|
||||
Cool:
|
||||
type: programming
|
||||
extensions:
|
||||
- .cl
|
||||
tm_scope: source.cool
|
||||
|
||||
Coq:
|
||||
type: programming
|
||||
extensions:
|
||||
@@ -1066,6 +1071,12 @@ Grace:
|
||||
tm_scope: none
|
||||
ace_mode: none
|
||||
|
||||
Gradle:
|
||||
type: data
|
||||
extensions:
|
||||
- .gradle
|
||||
tm_scope: source.groovy.gradle
|
||||
|
||||
Grammatical Framework:
|
||||
type: programming
|
||||
aliases:
|
||||
@@ -1331,7 +1342,7 @@ Isabelle:
|
||||
color: "#fdcd00"
|
||||
extensions:
|
||||
- .thy
|
||||
tm_scope: none
|
||||
tm_scope: source.isabelle.theory
|
||||
ace_mode: none
|
||||
|
||||
J:
|
||||
@@ -1422,6 +1433,7 @@ JavaScript:
|
||||
- .bones
|
||||
- .es6
|
||||
- .frag
|
||||
- .gs
|
||||
- .jake
|
||||
- .jsb
|
||||
- .jsfl
|
||||
@@ -1754,6 +1766,8 @@ Mercury:
|
||||
type: programming
|
||||
color: "#abcdef"
|
||||
ace_mode: prolog
|
||||
interpreters:
|
||||
- mmi
|
||||
extensions:
|
||||
- .m
|
||||
- .moo
|
||||
@@ -2024,6 +2038,13 @@ Oxygene:
|
||||
tm_scope: none
|
||||
ace_mode: none
|
||||
|
||||
Oz:
|
||||
type: programming
|
||||
color: "#fcaf3e"
|
||||
extensions:
|
||||
- .oz
|
||||
tm_scope: source.oz
|
||||
|
||||
PAWN:
|
||||
type: programming
|
||||
color: "#dbb284"
|
||||
@@ -2334,6 +2355,15 @@ R:
|
||||
- Rscript
|
||||
ace_mode: r
|
||||
|
||||
RAML:
|
||||
type: data
|
||||
lexer: YAML
|
||||
ace_mode: yaml
|
||||
tm_scope: source.yaml
|
||||
color: "#77d9fb"
|
||||
extensions:
|
||||
- .raml
|
||||
|
||||
RDoc:
|
||||
type: prose
|
||||
ace_mode: rdoc
|
||||
@@ -3000,6 +3030,7 @@ XML:
|
||||
- .jelly
|
||||
- .kml
|
||||
- .launch
|
||||
- .mm
|
||||
- .mxml
|
||||
- .nproj
|
||||
- .nuspec
|
||||
|
||||
@@ -6,6 +6,7 @@ end
|
||||
|
||||
require 'linguist/md5'
|
||||
require 'linguist/classifier'
|
||||
require 'linguist/shebang'
|
||||
|
||||
module Linguist
|
||||
# Model for accessing classifier training data.
|
||||
@@ -61,7 +62,7 @@ module Linguist
|
||||
yield({
|
||||
:path => path,
|
||||
:language => category,
|
||||
:interpreter => Linguist.interpreter_from_shebang(File.read(path)),
|
||||
:interpreter => Shebang.interpreter(File.read(path)),
|
||||
:extname => File.extname(filename)
|
||||
})
|
||||
end
|
||||
@@ -114,41 +115,4 @@ module Linguist
|
||||
db
|
||||
end
|
||||
end
|
||||
|
||||
# Used to retrieve the interpreter from the shebang line of a file's
|
||||
# data.
|
||||
def self.interpreter_from_shebang(data)
|
||||
lines = data.lines.to_a
|
||||
|
||||
if lines.any? && (match = lines[0].match(/(.+)\n?/)) && (bang = match[0]) =~ /^#!/
|
||||
bang.sub!(/^#! /, '#!')
|
||||
tokens = bang.split(' ')
|
||||
pieces = tokens.first.split('/')
|
||||
|
||||
if pieces.size > 1
|
||||
script = pieces.last
|
||||
else
|
||||
script = pieces.first.sub('#!', '')
|
||||
end
|
||||
|
||||
script = script == 'env' ? tokens[1] : script
|
||||
|
||||
# If script has an invalid shebang, we might get here
|
||||
return unless script
|
||||
|
||||
# "python2.6" -> "python2"
|
||||
script.sub! $1, '' if script =~ /(\.\d+)$/
|
||||
|
||||
# Check for multiline shebang hacks that call `exec`
|
||||
if script == 'sh' &&
|
||||
lines[0...5].any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
|
||||
script = $1
|
||||
end
|
||||
|
||||
File.basename(script)
|
||||
else
|
||||
nil
|
||||
end
|
||||
end
|
||||
|
||||
end
|
||||
|
||||
44
lib/linguist/shebang.rb
Normal file
44
lib/linguist/shebang.rb
Normal file
@@ -0,0 +1,44 @@
|
||||
module Linguist
|
||||
class Shebang
|
||||
# Public: Use shebang to detect language of the blob.
|
||||
#
|
||||
# blob - An object that quacks like a blob.
|
||||
#
|
||||
# Examples
|
||||
#
|
||||
# Shebang.call(FileBlob.new("path/to/file"))
|
||||
#
|
||||
# Returns an Array with one Language if the blob has a shebang with a valid
|
||||
# interpreter, or empty if there is no shebang.
|
||||
def self.call(blob, _ = nil)
|
||||
Language.find_by_interpreter interpreter(blob.data)
|
||||
end
|
||||
|
||||
# Public: Get the interpreter from the shebang
|
||||
#
|
||||
# Returns a String or nil
|
||||
def self.interpreter(data)
|
||||
lines = data.lines
|
||||
return unless match = /^#! ?(.*)$/.match(lines.first)
|
||||
|
||||
tokens = match[1].split(' ')
|
||||
script = tokens.first.split('/').last
|
||||
|
||||
script = tokens[1] if script == 'env'
|
||||
|
||||
# If script has an invalid shebang, we might get here
|
||||
return unless script
|
||||
|
||||
# "python2.6" -> "python2"
|
||||
script.sub! $1, '' if script =~ /(\.\d+)$/
|
||||
|
||||
# Check for multiline shebang hacks that call `exec`
|
||||
if script == 'sh' &&
|
||||
lines.first(5).any? { |l| l.match(/exec (\w+).+\$0.+\$@/) }
|
||||
script = $1
|
||||
end
|
||||
|
||||
File.basename(script)
|
||||
end
|
||||
end
|
||||
end
|
||||
20
lib/linguist/strategy/filename.rb
Normal file
20
lib/linguist/strategy/filename.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
module Linguist
|
||||
module Strategy
|
||||
# Detects language based on filename and/or extension
|
||||
class Filename
|
||||
def self.call(blob, _)
|
||||
name = blob.name.to_s
|
||||
|
||||
# A bit of an elegant hack. If the file is executable but extensionless,
|
||||
# append a "magic" extension so it can be classified with other
|
||||
# languages that have shebang scripts.
|
||||
extensions = FileBlob.new(name).extensions
|
||||
if extensions.empty? && blob.mode && (blob.mode.to_i(8) & 05) == 05
|
||||
name += ".script!"
|
||||
end
|
||||
|
||||
Language.find_by_filename(name)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -232,9 +232,6 @@
|
||||
# .DS_Store's
|
||||
- .[Dd][Ss]_[Ss]tore$
|
||||
|
||||
# Mercury --use-subdirs
|
||||
- Mercury/
|
||||
|
||||
# R packages
|
||||
- ^vignettes/
|
||||
- ^inst/extdata/
|
||||
|
||||
86
samples/C++/16F88.h
Normal file
86
samples/C++/16F88.h
Normal file
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* This file is part of PIC
|
||||
* Copyright © 2012 Rachel Mant (dx-mon@users.sourceforge.net)
|
||||
*
|
||||
* PIC is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PIC is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
enum PIC16F88Instruction
|
||||
{
|
||||
ADDWF,
|
||||
ANDWF,
|
||||
CLRF,
|
||||
CLRW,
|
||||
COMF,
|
||||
DECF,
|
||||
DECFSZ,
|
||||
INCF,
|
||||
INCFSZ,
|
||||
IORWF,
|
||||
MOVF,
|
||||
MOVWF,
|
||||
NOP,
|
||||
RLF,
|
||||
RRF,
|
||||
SUBWF,
|
||||
SWAPF,
|
||||
XORWF,
|
||||
BCF,
|
||||
BSF,
|
||||
BTFSC,
|
||||
BTFSS,
|
||||
ADDLW,
|
||||
ANDLW,
|
||||
CALL,
|
||||
CLRWDT,
|
||||
GOTO,
|
||||
IORLW,
|
||||
MOVLW,
|
||||
RETFIE,
|
||||
RETLW,
|
||||
RETURN,
|
||||
SLEEP,
|
||||
SUBLW,
|
||||
XORLW
|
||||
};
|
||||
|
||||
class PIC16F88
|
||||
{
|
||||
public:
|
||||
PIC16F88(ROM *ProgramMemory);
|
||||
void Step();
|
||||
|
||||
private:
|
||||
uint8_t q;
|
||||
bool nextIsNop, trapped;
|
||||
Memory *memory;
|
||||
ROM *program;
|
||||
Stack<uint16_t, 8> *CallStack;
|
||||
Register<uint16_t> *PC;
|
||||
Register<> *WREG, *PCL, *STATUS, *PCLATCH;
|
||||
PIC16F88Instruction inst;
|
||||
uint16_t instrWord;
|
||||
|
||||
private:
|
||||
void DecodeInstruction();
|
||||
void ProcessInstruction();
|
||||
|
||||
uint8_t GetBank();
|
||||
uint8_t GetMemoryContents(uint8_t partialAddress);
|
||||
void SetMemoryContents(uint8_t partialAddress, uint8_t newVal);
|
||||
void CheckZero(uint8_t value);
|
||||
void StoreValue(uint8_t value, bool updateZero);
|
||||
uint8_t SetCarry(bool val);
|
||||
uint16_t GetPCHFinalBits();
|
||||
};
|
||||
32
samples/C++/Memory16F88.h
Normal file
32
samples/C++/Memory16F88.h
Normal file
@@ -0,0 +1,32 @@
|
||||
/*
|
||||
* This file is part of PIC
|
||||
* Copyright © 2012 Rachel Mant (dx-mon@users.sourceforge.net)
|
||||
*
|
||||
* PIC is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* PIC is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "Memory.h"
|
||||
|
||||
class Memory16F88 : public Memory
|
||||
{
|
||||
private:
|
||||
uint8_t memory[512];
|
||||
std::map<uint32_t, MemoryLocation *> memoryMap;
|
||||
|
||||
public:
|
||||
Memory16F88();
|
||||
uint8_t Dereference(uint8_t bank, uint8_t partialAddress);
|
||||
uint8_t *Reference(uint8_t bank, uint8_t partialAddress);
|
||||
uint8_t *operator [](uint32_t ref);
|
||||
};
|
||||
76
samples/C++/ThreadedQueue.h
Normal file
76
samples/C++/ThreadedQueue.h
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
* This file is part of IRCBot
|
||||
* Copyright © 2014 Rachel Mant (dx-mon@users.sourceforge.net)
|
||||
*
|
||||
* IRCBot is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* IRCBot is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __THREADED_QUEUE_H__
|
||||
#define __THREADED_QUEUE_H__
|
||||
|
||||
#include <pthread.h>
|
||||
#include <queue>
|
||||
|
||||
template<class T>
|
||||
class ThreadedQueue : public std::queue<T>
|
||||
{
|
||||
private:
|
||||
pthread_mutex_t queueMutex;
|
||||
pthread_cond_t queueCond;
|
||||
|
||||
public:
|
||||
ThreadedQueue()
|
||||
{
|
||||
pthread_mutexattr_t mutexAttrs;
|
||||
pthread_condattr_t condAttrs;
|
||||
|
||||
pthread_mutexattr_init(&mutexAttrs);
|
||||
pthread_mutexattr_settype(&mutexAttrs, PTHREAD_MUTEX_ERRORCHECK);
|
||||
pthread_mutex_init(&queueMutex, &mutexAttrs);
|
||||
pthread_mutexattr_destroy(&mutexAttrs);
|
||||
|
||||
pthread_condattr_init(&condAttrs);
|
||||
pthread_condattr_setpshared(&condAttrs, PTHREAD_PROCESS_PRIVATE);
|
||||
pthread_cond_init(&queueCond, &condAttrs);
|
||||
pthread_condattr_destroy(&condAttrs);
|
||||
}
|
||||
|
||||
~ThreadedQueue()
|
||||
{
|
||||
pthread_cond_destroy(&queueCond);
|
||||
pthread_mutex_destroy(&queueMutex);
|
||||
}
|
||||
|
||||
void waitItems()
|
||||
{
|
||||
pthread_mutex_lock(&queueMutex);
|
||||
pthread_cond_wait(&queueCond, &queueMutex);
|
||||
pthread_mutex_unlock(&queueMutex);
|
||||
}
|
||||
|
||||
void signalItems()
|
||||
{
|
||||
pthread_mutex_lock(&queueMutex);
|
||||
pthread_cond_broadcast(&queueCond);
|
||||
pthread_mutex_unlock(&queueMutex);
|
||||
}
|
||||
|
||||
void push(T item)
|
||||
{
|
||||
std::queue<T>::push(item);
|
||||
signalItems();
|
||||
}
|
||||
};
|
||||
|
||||
#endif /*__THREADED_QUEUE_H__*/
|
||||
145
samples/C/2D.C
Normal file
145
samples/C/2D.C
Normal file
@@ -0,0 +1,145 @@
|
||||
#include "2D.h"
|
||||
#include <math.h>
|
||||
|
||||
void set_vgabasemem(void)
|
||||
{
|
||||
ULONG vgabase;
|
||||
SELECTOR tmp;
|
||||
asm mov [tmp], ds
|
||||
dpmi_get_sel_base(&vgabase, tmp);
|
||||
vgabasemem = (char *)(-vgabase + 0xa0000);
|
||||
}
|
||||
|
||||
void drw_chdis(int mode) // change the display!
|
||||
{
|
||||
regs.b.ah = 0x00; // seet theh display moode
|
||||
regs.b.al = mode; // change it to the mode like innit
|
||||
regs.h.flags = 0x72;// Set the dingoes kidneys out of FLAGS eh?
|
||||
regs.h.ss = 0; // Like, totally set the stack segment
|
||||
regs.h.sp = 0; // Set tha stack pointaaaaahhhhh!!!
|
||||
dpmi_simulate_real_interrupt(0x10, ®s);
|
||||
}
|
||||
|
||||
void drw_pix(int x, int y, enum COLORS col)
|
||||
{
|
||||
*VGAPIX(x, y) = col;
|
||||
}
|
||||
|
||||
void drw_line(int x0, int y0, int x1, int y1, enum COLORS col)
|
||||
{
|
||||
// Going for the optimized version of bresenham's line algo.
|
||||
int stp = (abs(y0 - y1) > abs(x0 - x1));
|
||||
int tmp, dx, dy, err, yi, i, j; // yi = y excrement
|
||||
if (stp) {
|
||||
// swappity swap
|
||||
tmp = y0;
|
||||
y0 = x0;
|
||||
x0 = tmp;
|
||||
|
||||
tmp = y1;
|
||||
y1 = x1;
|
||||
x1 = tmp;
|
||||
}
|
||||
// AAAAND NOW WE MUST DO ZEES AGAIN :(
|
||||
// I'm sure there was a func somewhere that does this? :P
|
||||
if (x0 > x1) {
|
||||
tmp = x0;
|
||||
x0 = x1;
|
||||
x1 = tmp;
|
||||
|
||||
tmp = y0;
|
||||
y0 = y1;
|
||||
y1 = tmp;
|
||||
}
|
||||
dx = (x1 - x0);
|
||||
dy = (abs(y1 - y0));
|
||||
err = (dx / 2);
|
||||
|
||||
if (y0 < y1)
|
||||
yi = 1;
|
||||
else
|
||||
yi = -1;
|
||||
j = y0;
|
||||
for (i = x0; i < x1; i++)
|
||||
{
|
||||
if (stp)
|
||||
*VGAPIX(j, i) = col;
|
||||
else
|
||||
*VGAPIX(i, j) = col;
|
||||
|
||||
err -= dy;
|
||||
if (err < 0) {
|
||||
j += yi;
|
||||
err += dx;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void drw_rectl(int x, int y, int w, int h, enum COLORS col)
|
||||
{
|
||||
drw_line(x, y, x+w, y, col);
|
||||
drw_line(x+w, y, x+w, y+h, col);
|
||||
|
||||
drw_line(x, y, x, y+h, col);
|
||||
drw_line(x, y+h, x+w+1, y+h, col);
|
||||
}
|
||||
|
||||
void drw_rectf(int x, int y, int w, int h, enum COLORS col)
|
||||
{
|
||||
int i, j;
|
||||
for (j = y; j < x+h; j++) {
|
||||
for (i = x; i < y+w; i++) {
|
||||
*VGAPIX(i, j) = col;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void drw_circl(int x, int y, int rad, enum COLORS col)
|
||||
{
|
||||
int mang, i; // max angle, haha
|
||||
int px, py;
|
||||
mang = 360; // Yeah yeah I'll switch to rad later
|
||||
for (i = 0; i <= mang; i++)
|
||||
{
|
||||
px = cos(i)*rad + x; // + px; // causes some really cools effects! :D
|
||||
py = sin(i)*rad + y; // + py;
|
||||
*VGAPIX(px, py) = col;
|
||||
}
|
||||
}
|
||||
|
||||
void drw_tex(int x, int y, int w, int h, enum COLORS tex[])
|
||||
{ // i*w+j
|
||||
int i, j;
|
||||
for (i = 0; i < w; i++)
|
||||
{
|
||||
for (j = 0; j < h; j++)
|
||||
{
|
||||
*VGAPIX(x+i, y+j) = tex[j*w+i];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void 2D_init(void)
|
||||
{
|
||||
set_vgabasemem();
|
||||
drw_chdis(0x13);
|
||||
}
|
||||
|
||||
void 2D_exit(void)
|
||||
{
|
||||
drw_chdis(3);
|
||||
}
|
||||
/*
|
||||
int main()
|
||||
{
|
||||
set_vgabasemem();
|
||||
drw_chdis(0x13);
|
||||
|
||||
while(!kbhit()) {
|
||||
if ((getch()) == 0x1b) // escape
|
||||
break;
|
||||
}
|
||||
drw_chdis(3);
|
||||
return 0;
|
||||
}
|
||||
*/
|
||||
29
samples/C/2D.H
Normal file
29
samples/C/2D.H
Normal file
@@ -0,0 +1,29 @@
|
||||
#ifndef __2DGFX
|
||||
#define __2DGFX
|
||||
// Includes
|
||||
#include <stdio.h>
|
||||
#include <math.h>
|
||||
#include <conio.h>
|
||||
#include <dpmi.h>
|
||||
|
||||
// Defines
|
||||
#define VGAPIX(x,y) (vgabasemem + (x) + (y) * 320)
|
||||
|
||||
// Variables
|
||||
char * vgabasemem;
|
||||
DPMI_REGS regs;
|
||||
|
||||
// Drawing functions:
|
||||
//void setvgabasemem(void);
|
||||
void drw_chdis(int mode); // draw_func_change_display
|
||||
void drw_pix(int x, int y, enum COLORS col);
|
||||
void drw_line(int x0, int y0, int x1, int y1, enum COLORS col);
|
||||
void drw_rectl(int x, int y, int w, int h, enum COLORS col);
|
||||
void drw_rectf(int x, int y, int w, int h, enum COLORS col);
|
||||
void drw_cirl(int x, int y, int rad, enum COLORS col);
|
||||
void drw_tex(int x, int y, int w, int h, enum COLORS tex[]);
|
||||
void 2D_init(void);
|
||||
void 2D_exit(void);
|
||||
|
||||
|
||||
#endif
|
||||
93
samples/C/ArrowLeft.h
Normal file
93
samples/C/ArrowLeft.h
Normal file
@@ -0,0 +1,93 @@
|
||||
/*
|
||||
* This file is part of GTK++ (libGTK++)
|
||||
* Copyright © 2012 Rachel Mant (dx-mon@users.sourceforge.net)
|
||||
*
|
||||
* GTK++ is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* GTK++ is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
/* GdkPixbuf RGBA C-Source image dump */
|
||||
|
||||
#ifdef __SUNPRO_C
|
||||
#pragma align 4 (ArrowLeft)
|
||||
#endif
|
||||
#ifdef __GNUC__
|
||||
static const uint8_t ArrowLeft[] __attribute__ ((__aligned__ (4))) =
|
||||
#else
|
||||
static const uint8_t ArrowLeft[] =
|
||||
#endif
|
||||
{ ""
|
||||
/* Pixbuf magic (0x47646b50) */
|
||||
"GdkP"
|
||||
/* length: header (24) + pixel_data (1600) */
|
||||
"\0\0\6X"
|
||||
/* pixdata_type (0x1010002) */
|
||||
"\1\1\0\2"
|
||||
/* rowstride (80) */
|
||||
"\0\0\0P"
|
||||
/* width (20) */
|
||||
"\0\0\0\24"
|
||||
/* height (20) */
|
||||
"\0\0\0\24"
|
||||
/* pixel_data: */
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\377\0\0\0\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0\377\0\0\0\377"
|
||||
"\0\0\0\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377"
|
||||
"\0\0\0\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0\377\0\0\0\377\0"
|
||||
"\0\0\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\377\0\0\0\377\0\0\0\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\377\0\0\0\377\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\377\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0"
|
||||
"\0\0"};
|
||||
|
||||
|
||||
903
samples/C/GLKMatrix4.h
Normal file
903
samples/C/GLKMatrix4.h
Normal file
@@ -0,0 +1,903 @@
|
||||
//
|
||||
// GLKMatrix4.h
|
||||
// GLKit
|
||||
//
|
||||
// Copyright (c) 2011, Apple Inc. All rights reserved.
|
||||
//
|
||||
|
||||
#ifndef __GLK_MATRIX_4_H
|
||||
#define __GLK_MATRIX_4_H
|
||||
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <math.h>
|
||||
|
||||
#if defined(__ARM_NEON__)
|
||||
#include <arm_neon.h>
|
||||
#endif
|
||||
|
||||
#include <GLKit/GLKMathTypes.h>
|
||||
#include <GLKit/GLKVector3.h>
|
||||
#include <GLKit/GLKVector4.h>
|
||||
#include <GLKit/GLKQuaternion.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Prototypes
|
||||
#pragma mark -
|
||||
|
||||
extern const GLKMatrix4 GLKMatrix4Identity;
|
||||
|
||||
/*
|
||||
m30, m31, and m32 correspond to the translation values tx, ty, tz, respectively.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Make(float m00, float m01, float m02, float m03,
|
||||
float m10, float m11, float m12, float m13,
|
||||
float m20, float m21, float m22, float m23,
|
||||
float m30, float m31, float m32, float m33);
|
||||
|
||||
/*
|
||||
m03, m13, and m23 correspond to the translation values tx, ty, tz, respectively.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeAndTranspose(float m00, float m01, float m02, float m03,
|
||||
float m10, float m11, float m12, float m13,
|
||||
float m20, float m21, float m22, float m23,
|
||||
float m30, float m31, float m32, float m33);
|
||||
|
||||
/*
|
||||
m[12], m[13], and m[14] correspond to the translation values tx, ty, and tz, respectively.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeWithArray(float values[16]);
|
||||
|
||||
/*
|
||||
m[3], m[7], and m[11] correspond to the translation values tx, ty, and tz, respectively.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeWithArrayAndTranspose(float values[16]);
|
||||
|
||||
/*
|
||||
row0, row1, and row2's last component should correspond to the translation values tx, ty, and tz, respectively.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeWithRows(GLKVector4 row0,
|
||||
GLKVector4 row1,
|
||||
GLKVector4 row2,
|
||||
GLKVector4 row3);
|
||||
|
||||
/*
|
||||
column3's first three components should correspond to the translation values tx, ty, and tz.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeWithColumns(GLKVector4 column0,
|
||||
GLKVector4 column1,
|
||||
GLKVector4 column2,
|
||||
GLKVector4 column3);
|
||||
|
||||
/*
|
||||
The quaternion will be normalized before conversion.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeWithQuaternion(GLKQuaternion quaternion);
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeTranslation(float tx, float ty, float tz);
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeScale(float sx, float sy, float sz);
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeRotation(float radians, float x, float y, float z);
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeXRotation(float radians);
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeYRotation(float radians);
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeZRotation(float radians);
|
||||
|
||||
/*
|
||||
Equivalent to gluPerspective.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakePerspective(float fovyRadians, float aspect, float nearZ, float farZ);
|
||||
|
||||
/*
|
||||
Equivalent to glFrustum.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeFrustum(float left, float right,
|
||||
float bottom, float top,
|
||||
float nearZ, float farZ);
|
||||
|
||||
/*
|
||||
Equivalent to glOrtho.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeOrtho(float left, float right,
|
||||
float bottom, float top,
|
||||
float nearZ, float farZ);
|
||||
|
||||
/*
|
||||
Equivalent to gluLookAt.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeLookAt(float eyeX, float eyeY, float eyeZ,
|
||||
float centerX, float centerY, float centerZ,
|
||||
float upX, float upY, float upZ);
|
||||
|
||||
/*
|
||||
Returns the upper left 3x3 portion of the 4x4 matrix.
|
||||
*/
|
||||
static __inline__ GLKMatrix3 GLKMatrix4GetMatrix3(GLKMatrix4 matrix);
|
||||
/*
|
||||
Returns the upper left 2x2 portion of the 4x4 matrix.
|
||||
*/
|
||||
static __inline__ GLKMatrix2 GLKMatrix4GetMatrix2(GLKMatrix4 matrix);
|
||||
|
||||
/*
|
||||
GLKMatrix4GetRow returns vectors for rows 0, 1, and 2 whose last component will be the translation value tx, ty, and tz, respectively.
|
||||
Valid row values range from 0 to 3, inclusive.
|
||||
*/
|
||||
static __inline__ GLKVector4 GLKMatrix4GetRow(GLKMatrix4 matrix, int row);
|
||||
/*
|
||||
GLKMatrix4GetColumn returns a vector for column 3 whose first three components will be the translation values tx, ty, and tz.
|
||||
Valid column values range from 0 to 3, inclusive.
|
||||
*/
|
||||
static __inline__ GLKVector4 GLKMatrix4GetColumn(GLKMatrix4 matrix, int column);
|
||||
|
||||
/*
|
||||
GLKMatrix4SetRow expects that the vector for row 0, 1, and 2 will have a translation value as its last component.
|
||||
Valid row values range from 0 to 3, inclusive.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4SetRow(GLKMatrix4 matrix, int row, GLKVector4 vector);
|
||||
/*
|
||||
GLKMatrix4SetColumn expects that the vector for column 3 will contain the translation values tx, ty, and tz as its first three components, respectively.
|
||||
Valid column values range from 0 to 3, inclusive.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4SetColumn(GLKMatrix4 matrix, int column, GLKVector4 vector);
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Transpose(GLKMatrix4 matrix);
|
||||
|
||||
GLKMatrix4 GLKMatrix4Invert(GLKMatrix4 matrix, bool *isInvertible);
|
||||
GLKMatrix4 GLKMatrix4InvertAndTranspose(GLKMatrix4 matrix, bool *isInvertible);
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Multiply(GLKMatrix4 matrixLeft, GLKMatrix4 matrixRight);
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Add(GLKMatrix4 matrixLeft, GLKMatrix4 matrixRight);
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Subtract(GLKMatrix4 matrixLeft, GLKMatrix4 matrixRight);
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Translate(GLKMatrix4 matrix, float tx, float ty, float tz);
|
||||
static __inline__ GLKMatrix4 GLKMatrix4TranslateWithVector3(GLKMatrix4 matrix, GLKVector3 translationVector);
|
||||
/*
|
||||
The last component of the GLKVector4, translationVector, is ignored.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4TranslateWithVector4(GLKMatrix4 matrix, GLKVector4 translationVector);
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Scale(GLKMatrix4 matrix, float sx, float sy, float sz);
|
||||
static __inline__ GLKMatrix4 GLKMatrix4ScaleWithVector3(GLKMatrix4 matrix, GLKVector3 scaleVector);
|
||||
/*
|
||||
The last component of the GLKVector4, scaleVector, is ignored.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4ScaleWithVector4(GLKMatrix4 matrix, GLKVector4 scaleVector);
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Rotate(GLKMatrix4 matrix, float radians, float x, float y, float z);
|
||||
static __inline__ GLKMatrix4 GLKMatrix4RotateWithVector3(GLKMatrix4 matrix, float radians, GLKVector3 axisVector);
|
||||
/*
|
||||
The last component of the GLKVector4, axisVector, is ignored.
|
||||
*/
|
||||
static __inline__ GLKMatrix4 GLKMatrix4RotateWithVector4(GLKMatrix4 matrix, float radians, GLKVector4 axisVector);
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4RotateX(GLKMatrix4 matrix, float radians);
|
||||
static __inline__ GLKMatrix4 GLKMatrix4RotateY(GLKMatrix4 matrix, float radians);
|
||||
static __inline__ GLKMatrix4 GLKMatrix4RotateZ(GLKMatrix4 matrix, float radians);
|
||||
|
||||
/*
|
||||
Assumes 0 in the w component.
|
||||
*/
|
||||
static __inline__ GLKVector3 GLKMatrix4MultiplyVector3(GLKMatrix4 matrixLeft, GLKVector3 vectorRight);
|
||||
/*
|
||||
Assumes 1 in the w component.
|
||||
*/
|
||||
static __inline__ GLKVector3 GLKMatrix4MultiplyVector3WithTranslation(GLKMatrix4 matrixLeft, GLKVector3 vectorRight);
|
||||
/*
|
||||
Assumes 1 in the w component and divides the resulting vector by w before returning.
|
||||
*/
|
||||
static __inline__ GLKVector3 GLKMatrix4MultiplyAndProjectVector3(GLKMatrix4 matrixLeft, GLKVector3 vectorRight);
|
||||
|
||||
/*
|
||||
Assumes 0 in the w component.
|
||||
*/
|
||||
static __inline__ void GLKMatrix4MultiplyVector3Array(GLKMatrix4 matrix, GLKVector3 *vectors, size_t vectorCount);
|
||||
/*
|
||||
Assumes 1 in the w component.
|
||||
*/
|
||||
static __inline__ void GLKMatrix4MultiplyVector3ArrayWithTranslation(GLKMatrix4 matrix, GLKVector3 *vectors, size_t vectorCount);
|
||||
/*
|
||||
Assumes 1 in the w component and divides the resulting vector by w before returning.
|
||||
*/
|
||||
static __inline__ void GLKMatrix4MultiplyAndProjectVector3Array(GLKMatrix4 matrix, GLKVector3 *vectors, size_t vectorCount);
|
||||
|
||||
static __inline__ GLKVector4 GLKMatrix4MultiplyVector4(GLKMatrix4 matrixLeft, GLKVector4 vectorRight);
|
||||
|
||||
static __inline__ void GLKMatrix4MultiplyVector4Array(GLKMatrix4 matrix, GLKVector4 *vectors, size_t vectorCount);
|
||||
|
||||
#pragma mark -
|
||||
#pragma mark Implementations
|
||||
#pragma mark -
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Make(float m00, float m01, float m02, float m03,
|
||||
float m10, float m11, float m12, float m13,
|
||||
float m20, float m21, float m22, float m23,
|
||||
float m30, float m31, float m32, float m33)
|
||||
{
|
||||
GLKMatrix4 m = { m00, m01, m02, m03,
|
||||
m10, m11, m12, m13,
|
||||
m20, m21, m22, m23,
|
||||
m30, m31, m32, m33 };
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeAndTranspose(float m00, float m01, float m02, float m03,
|
||||
float m10, float m11, float m12, float m13,
|
||||
float m20, float m21, float m22, float m23,
|
||||
float m30, float m31, float m32, float m33)
|
||||
{
|
||||
GLKMatrix4 m = { m00, m10, m20, m30,
|
||||
m01, m11, m21, m31,
|
||||
m02, m12, m22, m32,
|
||||
m03, m13, m23, m33 };
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeWithArray(float values[16])
|
||||
{
|
||||
GLKMatrix4 m = { values[0], values[1], values[2], values[3],
|
||||
values[4], values[5], values[6], values[7],
|
||||
values[8], values[9], values[10], values[11],
|
||||
values[12], values[13], values[14], values[15] };
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeWithArrayAndTranspose(float values[16])
|
||||
{
|
||||
#if defined(__ARM_NEON__)
|
||||
float32x4x4_t m = vld4q_f32(values);
|
||||
return *(GLKMatrix4 *)&m;
|
||||
#else
|
||||
GLKMatrix4 m = { values[0], values[4], values[8], values[12],
|
||||
values[1], values[5], values[9], values[13],
|
||||
values[2], values[6], values[10], values[14],
|
||||
values[3], values[7], values[11], values[15] };
|
||||
return m;
|
||||
#endif
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeWithRows(GLKVector4 row0,
|
||||
GLKVector4 row1,
|
||||
GLKVector4 row2,
|
||||
GLKVector4 row3)
|
||||
{
|
||||
GLKMatrix4 m = { row0.v[0], row1.v[0], row2.v[0], row3.v[0],
|
||||
row0.v[1], row1.v[1], row2.v[1], row3.v[1],
|
||||
row0.v[2], row1.v[2], row2.v[2], row3.v[2],
|
||||
row0.v[3], row1.v[3], row2.v[3], row3.v[3] };
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeWithColumns(GLKVector4 column0,
|
||||
GLKVector4 column1,
|
||||
GLKVector4 column2,
|
||||
GLKVector4 column3)
|
||||
{
|
||||
#if defined(__ARM_NEON__)
|
||||
float32x4x4_t m;
|
||||
m.val[0] = vld1q_f32(column0.v);
|
||||
m.val[1] = vld1q_f32(column1.v);
|
||||
m.val[2] = vld1q_f32(column2.v);
|
||||
m.val[3] = vld1q_f32(column3.v);
|
||||
return *(GLKMatrix4 *)&m;
|
||||
#else
|
||||
GLKMatrix4 m = { column0.v[0], column0.v[1], column0.v[2], column0.v[3],
|
||||
column1.v[0], column1.v[1], column1.v[2], column1.v[3],
|
||||
column2.v[0], column2.v[1], column2.v[2], column2.v[3],
|
||||
column3.v[0], column3.v[1], column3.v[2], column3.v[3] };
|
||||
return m;
|
||||
#endif
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeWithQuaternion(GLKQuaternion quaternion)
|
||||
{
|
||||
quaternion = GLKQuaternionNormalize(quaternion);
|
||||
|
||||
float x = quaternion.q[0];
|
||||
float y = quaternion.q[1];
|
||||
float z = quaternion.q[2];
|
||||
float w = quaternion.q[3];
|
||||
|
||||
float _2x = x + x;
|
||||
float _2y = y + y;
|
||||
float _2z = z + z;
|
||||
float _2w = w + w;
|
||||
|
||||
GLKMatrix4 m = { 1.0f - _2y * y - _2z * z,
|
||||
_2x * y + _2w * z,
|
||||
_2x * z - _2w * y,
|
||||
0.0f,
|
||||
_2x * y - _2w * z,
|
||||
1.0f - _2x * x - _2z * z,
|
||||
_2y * z + _2w * x,
|
||||
0.0f,
|
||||
_2x * z + _2w * y,
|
||||
_2y * z - _2w * x,
|
||||
1.0f - _2x * x - _2y * y,
|
||||
0.0f,
|
||||
0.0f,
|
||||
0.0f,
|
||||
0.0f,
|
||||
1.0f };
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeTranslation(float tx, float ty, float tz)
|
||||
{
|
||||
GLKMatrix4 m = GLKMatrix4Identity;
|
||||
m.m[12] = tx;
|
||||
m.m[13] = ty;
|
||||
m.m[14] = tz;
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeScale(float sx, float sy, float sz)
|
||||
{
|
||||
GLKMatrix4 m = GLKMatrix4Identity;
|
||||
m.m[0] = sx;
|
||||
m.m[5] = sy;
|
||||
m.m[10] = sz;
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeRotation(float radians, float x, float y, float z)
|
||||
{
|
||||
GLKVector3 v = GLKVector3Normalize(GLKVector3Make(x, y, z));
|
||||
float cos = cosf(radians);
|
||||
float cosp = 1.0f - cos;
|
||||
float sin = sinf(radians);
|
||||
|
||||
GLKMatrix4 m = { cos + cosp * v.v[0] * v.v[0],
|
||||
cosp * v.v[0] * v.v[1] + v.v[2] * sin,
|
||||
cosp * v.v[0] * v.v[2] - v.v[1] * sin,
|
||||
0.0f,
|
||||
cosp * v.v[0] * v.v[1] - v.v[2] * sin,
|
||||
cos + cosp * v.v[1] * v.v[1],
|
||||
cosp * v.v[1] * v.v[2] + v.v[0] * sin,
|
||||
0.0f,
|
||||
cosp * v.v[0] * v.v[2] + v.v[1] * sin,
|
||||
cosp * v.v[1] * v.v[2] - v.v[0] * sin,
|
||||
cos + cosp * v.v[2] * v.v[2],
|
||||
0.0f,
|
||||
0.0f,
|
||||
0.0f,
|
||||
0.0f,
|
||||
1.0f };
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeXRotation(float radians)
|
||||
{
|
||||
float cos = cosf(radians);
|
||||
float sin = sinf(radians);
|
||||
|
||||
GLKMatrix4 m = { 1.0f, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, cos, sin, 0.0f,
|
||||
0.0f, -sin, cos, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f };
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeYRotation(float radians)
|
||||
{
|
||||
float cos = cosf(radians);
|
||||
float sin = sinf(radians);
|
||||
|
||||
GLKMatrix4 m = { cos, 0.0f, -sin, 0.0f,
|
||||
0.0f, 1.0f, 0.0f, 0.0f,
|
||||
sin, 0.0f, cos, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f };
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeZRotation(float radians)
|
||||
{
|
||||
float cos = cosf(radians);
|
||||
float sin = sinf(radians);
|
||||
|
||||
GLKMatrix4 m = { cos, sin, 0.0f, 0.0f,
|
||||
-sin, cos, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, 1.0f, 0.0f,
|
||||
0.0f, 0.0f, 0.0f, 1.0f };
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakePerspective(float fovyRadians, float aspect, float nearZ, float farZ)
|
||||
{
|
||||
float cotan = 1.0f / tanf(fovyRadians / 2.0f);
|
||||
|
||||
GLKMatrix4 m = { cotan / aspect, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, cotan, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, (farZ + nearZ) / (nearZ - farZ), -1.0f,
|
||||
0.0f, 0.0f, (2.0f * farZ * nearZ) / (nearZ - farZ), 0.0f };
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeFrustum(float left, float right,
|
||||
float bottom, float top,
|
||||
float nearZ, float farZ)
|
||||
{
|
||||
float ral = right + left;
|
||||
float rsl = right - left;
|
||||
float tsb = top - bottom;
|
||||
float tab = top + bottom;
|
||||
float fan = farZ + nearZ;
|
||||
float fsn = farZ - nearZ;
|
||||
|
||||
GLKMatrix4 m = { 2.0f * nearZ / rsl, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 2.0f * nearZ / tsb, 0.0f, 0.0f,
|
||||
ral / rsl, tab / tsb, -fan / fsn, -1.0f,
|
||||
0.0f, 0.0f, (-2.0f * farZ * nearZ) / fsn, 0.0f };
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeOrtho(float left, float right,
|
||||
float bottom, float top,
|
||||
float nearZ, float farZ)
|
||||
{
|
||||
float ral = right + left;
|
||||
float rsl = right - left;
|
||||
float tab = top + bottom;
|
||||
float tsb = top - bottom;
|
||||
float fan = farZ + nearZ;
|
||||
float fsn = farZ - nearZ;
|
||||
|
||||
GLKMatrix4 m = { 2.0f / rsl, 0.0f, 0.0f, 0.0f,
|
||||
0.0f, 2.0f / tsb, 0.0f, 0.0f,
|
||||
0.0f, 0.0f, -2.0f / fsn, 0.0f,
|
||||
-ral / rsl, -tab / tsb, -fan / fsn, 1.0f };
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4MakeLookAt(float eyeX, float eyeY, float eyeZ,
|
||||
float centerX, float centerY, float centerZ,
|
||||
float upX, float upY, float upZ)
|
||||
{
|
||||
GLKVector3 ev = { eyeX, eyeY, eyeZ };
|
||||
GLKVector3 cv = { centerX, centerY, centerZ };
|
||||
GLKVector3 uv = { upX, upY, upZ };
|
||||
GLKVector3 n = GLKVector3Normalize(GLKVector3Add(ev, GLKVector3Negate(cv)));
|
||||
GLKVector3 u = GLKVector3Normalize(GLKVector3CrossProduct(uv, n));
|
||||
GLKVector3 v = GLKVector3CrossProduct(n, u);
|
||||
|
||||
GLKMatrix4 m = { u.v[0], v.v[0], n.v[0], 0.0f,
|
||||
u.v[1], v.v[1], n.v[1], 0.0f,
|
||||
u.v[2], v.v[2], n.v[2], 0.0f,
|
||||
GLKVector3DotProduct(GLKVector3Negate(u), ev),
|
||||
GLKVector3DotProduct(GLKVector3Negate(v), ev),
|
||||
GLKVector3DotProduct(GLKVector3Negate(n), ev),
|
||||
1.0f };
|
||||
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix3 GLKMatrix4GetMatrix3(GLKMatrix4 matrix)
|
||||
{
|
||||
GLKMatrix3 m = { matrix.m[0], matrix.m[1], matrix.m[2],
|
||||
matrix.m[4], matrix.m[5], matrix.m[6],
|
||||
matrix.m[8], matrix.m[9], matrix.m[10] };
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix2 GLKMatrix4GetMatrix2(GLKMatrix4 matrix)
|
||||
{
|
||||
GLKMatrix2 m = { matrix.m[0], matrix.m[1],
|
||||
matrix.m[4], matrix.m[5] };
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKVector4 GLKMatrix4GetRow(GLKMatrix4 matrix, int row)
|
||||
{
|
||||
GLKVector4 v = { matrix.m[row], matrix.m[4 + row], matrix.m[8 + row], matrix.m[12 + row] };
|
||||
return v;
|
||||
}
|
||||
|
||||
static __inline__ GLKVector4 GLKMatrix4GetColumn(GLKMatrix4 matrix, int column)
|
||||
{
|
||||
#if defined(__ARM_NEON__)
|
||||
float32x4_t v = vld1q_f32(&(matrix.m[column * 4]));
|
||||
return *(GLKVector4 *)&v;
|
||||
#else
|
||||
GLKVector4 v = { matrix.m[column * 4 + 0], matrix.m[column * 4 + 1], matrix.m[column * 4 + 2], matrix.m[column * 4 + 3] };
|
||||
return v;
|
||||
#endif
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4SetRow(GLKMatrix4 matrix, int row, GLKVector4 vector)
|
||||
{
|
||||
matrix.m[row] = vector.v[0];
|
||||
matrix.m[row + 4] = vector.v[1];
|
||||
matrix.m[row + 8] = vector.v[2];
|
||||
matrix.m[row + 12] = vector.v[3];
|
||||
|
||||
return matrix;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4SetColumn(GLKMatrix4 matrix, int column, GLKVector4 vector)
|
||||
{
|
||||
#if defined(__ARM_NEON__)
|
||||
float *dst = &(matrix.m[column * 4]);
|
||||
vst1q_f32(dst, vld1q_f32(vector.v));
|
||||
return matrix;
|
||||
#else
|
||||
matrix.m[column * 4 + 0] = vector.v[0];
|
||||
matrix.m[column * 4 + 1] = vector.v[1];
|
||||
matrix.m[column * 4 + 2] = vector.v[2];
|
||||
matrix.m[column * 4 + 3] = vector.v[3];
|
||||
|
||||
return matrix;
|
||||
#endif
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Transpose(GLKMatrix4 matrix)
|
||||
{
|
||||
#if defined(__ARM_NEON__)
|
||||
float32x4x4_t m = vld4q_f32(matrix.m);
|
||||
return *(GLKMatrix4 *)&m;
|
||||
#else
|
||||
GLKMatrix4 m = { matrix.m[0], matrix.m[4], matrix.m[8], matrix.m[12],
|
||||
matrix.m[1], matrix.m[5], matrix.m[9], matrix.m[13],
|
||||
matrix.m[2], matrix.m[6], matrix.m[10], matrix.m[14],
|
||||
matrix.m[3], matrix.m[7], matrix.m[11], matrix.m[15] };
|
||||
return m;
|
||||
#endif
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Multiply(GLKMatrix4 matrixLeft, GLKMatrix4 matrixRight)
|
||||
{
|
||||
#if defined(__ARM_NEON__)
|
||||
float32x4x4_t iMatrixLeft = *(float32x4x4_t *)&matrixLeft;
|
||||
float32x4x4_t iMatrixRight = *(float32x4x4_t *)&matrixRight;
|
||||
float32x4x4_t m;
|
||||
|
||||
m.val[0] = vmulq_n_f32(iMatrixLeft.val[0], vgetq_lane_f32(iMatrixRight.val[0], 0));
|
||||
m.val[1] = vmulq_n_f32(iMatrixLeft.val[0], vgetq_lane_f32(iMatrixRight.val[1], 0));
|
||||
m.val[2] = vmulq_n_f32(iMatrixLeft.val[0], vgetq_lane_f32(iMatrixRight.val[2], 0));
|
||||
m.val[3] = vmulq_n_f32(iMatrixLeft.val[0], vgetq_lane_f32(iMatrixRight.val[3], 0));
|
||||
|
||||
m.val[0] = vmlaq_n_f32(m.val[0], iMatrixLeft.val[1], vgetq_lane_f32(iMatrixRight.val[0], 1));
|
||||
m.val[1] = vmlaq_n_f32(m.val[1], iMatrixLeft.val[1], vgetq_lane_f32(iMatrixRight.val[1], 1));
|
||||
m.val[2] = vmlaq_n_f32(m.val[2], iMatrixLeft.val[1], vgetq_lane_f32(iMatrixRight.val[2], 1));
|
||||
m.val[3] = vmlaq_n_f32(m.val[3], iMatrixLeft.val[1], vgetq_lane_f32(iMatrixRight.val[3], 1));
|
||||
|
||||
m.val[0] = vmlaq_n_f32(m.val[0], iMatrixLeft.val[2], vgetq_lane_f32(iMatrixRight.val[0], 2));
|
||||
m.val[1] = vmlaq_n_f32(m.val[1], iMatrixLeft.val[2], vgetq_lane_f32(iMatrixRight.val[1], 2));
|
||||
m.val[2] = vmlaq_n_f32(m.val[2], iMatrixLeft.val[2], vgetq_lane_f32(iMatrixRight.val[2], 2));
|
||||
m.val[3] = vmlaq_n_f32(m.val[3], iMatrixLeft.val[2], vgetq_lane_f32(iMatrixRight.val[3], 2));
|
||||
|
||||
m.val[0] = vmlaq_n_f32(m.val[0], iMatrixLeft.val[3], vgetq_lane_f32(iMatrixRight.val[0], 3));
|
||||
m.val[1] = vmlaq_n_f32(m.val[1], iMatrixLeft.val[3], vgetq_lane_f32(iMatrixRight.val[1], 3));
|
||||
m.val[2] = vmlaq_n_f32(m.val[2], iMatrixLeft.val[3], vgetq_lane_f32(iMatrixRight.val[2], 3));
|
||||
m.val[3] = vmlaq_n_f32(m.val[3], iMatrixLeft.val[3], vgetq_lane_f32(iMatrixRight.val[3], 3));
|
||||
|
||||
return *(GLKMatrix4 *)&m;
|
||||
#else
|
||||
GLKMatrix4 m;
|
||||
|
||||
m.m[0] = matrixLeft.m[0] * matrixRight.m[0] + matrixLeft.m[4] * matrixRight.m[1] + matrixLeft.m[8] * matrixRight.m[2] + matrixLeft.m[12] * matrixRight.m[3];
|
||||
m.m[4] = matrixLeft.m[0] * matrixRight.m[4] + matrixLeft.m[4] * matrixRight.m[5] + matrixLeft.m[8] * matrixRight.m[6] + matrixLeft.m[12] * matrixRight.m[7];
|
||||
m.m[8] = matrixLeft.m[0] * matrixRight.m[8] + matrixLeft.m[4] * matrixRight.m[9] + matrixLeft.m[8] * matrixRight.m[10] + matrixLeft.m[12] * matrixRight.m[11];
|
||||
m.m[12] = matrixLeft.m[0] * matrixRight.m[12] + matrixLeft.m[4] * matrixRight.m[13] + matrixLeft.m[8] * matrixRight.m[14] + matrixLeft.m[12] * matrixRight.m[15];
|
||||
|
||||
m.m[1] = matrixLeft.m[1] * matrixRight.m[0] + matrixLeft.m[5] * matrixRight.m[1] + matrixLeft.m[9] * matrixRight.m[2] + matrixLeft.m[13] * matrixRight.m[3];
|
||||
m.m[5] = matrixLeft.m[1] * matrixRight.m[4] + matrixLeft.m[5] * matrixRight.m[5] + matrixLeft.m[9] * matrixRight.m[6] + matrixLeft.m[13] * matrixRight.m[7];
|
||||
m.m[9] = matrixLeft.m[1] * matrixRight.m[8] + matrixLeft.m[5] * matrixRight.m[9] + matrixLeft.m[9] * matrixRight.m[10] + matrixLeft.m[13] * matrixRight.m[11];
|
||||
m.m[13] = matrixLeft.m[1] * matrixRight.m[12] + matrixLeft.m[5] * matrixRight.m[13] + matrixLeft.m[9] * matrixRight.m[14] + matrixLeft.m[13] * matrixRight.m[15];
|
||||
|
||||
m.m[2] = matrixLeft.m[2] * matrixRight.m[0] + matrixLeft.m[6] * matrixRight.m[1] + matrixLeft.m[10] * matrixRight.m[2] + matrixLeft.m[14] * matrixRight.m[3];
|
||||
m.m[6] = matrixLeft.m[2] * matrixRight.m[4] + matrixLeft.m[6] * matrixRight.m[5] + matrixLeft.m[10] * matrixRight.m[6] + matrixLeft.m[14] * matrixRight.m[7];
|
||||
m.m[10] = matrixLeft.m[2] * matrixRight.m[8] + matrixLeft.m[6] * matrixRight.m[9] + matrixLeft.m[10] * matrixRight.m[10] + matrixLeft.m[14] * matrixRight.m[11];
|
||||
m.m[14] = matrixLeft.m[2] * matrixRight.m[12] + matrixLeft.m[6] * matrixRight.m[13] + matrixLeft.m[10] * matrixRight.m[14] + matrixLeft.m[14] * matrixRight.m[15];
|
||||
|
||||
m.m[3] = matrixLeft.m[3] * matrixRight.m[0] + matrixLeft.m[7] * matrixRight.m[1] + matrixLeft.m[11] * matrixRight.m[2] + matrixLeft.m[15] * matrixRight.m[3];
|
||||
m.m[7] = matrixLeft.m[3] * matrixRight.m[4] + matrixLeft.m[7] * matrixRight.m[5] + matrixLeft.m[11] * matrixRight.m[6] + matrixLeft.m[15] * matrixRight.m[7];
|
||||
m.m[11] = matrixLeft.m[3] * matrixRight.m[8] + matrixLeft.m[7] * matrixRight.m[9] + matrixLeft.m[11] * matrixRight.m[10] + matrixLeft.m[15] * matrixRight.m[11];
|
||||
m.m[15] = matrixLeft.m[3] * matrixRight.m[12] + matrixLeft.m[7] * matrixRight.m[13] + matrixLeft.m[11] * matrixRight.m[14] + matrixLeft.m[15] * matrixRight.m[15];
|
||||
|
||||
return m;
|
||||
#endif
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Add(GLKMatrix4 matrixLeft, GLKMatrix4 matrixRight)
|
||||
{
|
||||
#if defined(__ARM_NEON__)
|
||||
float32x4x4_t iMatrixLeft = *(float32x4x4_t *)&matrixLeft;
|
||||
float32x4x4_t iMatrixRight = *(float32x4x4_t *)&matrixRight;
|
||||
float32x4x4_t m;
|
||||
|
||||
m.val[0] = vaddq_f32(iMatrixLeft.val[0], iMatrixRight.val[0]);
|
||||
m.val[1] = vaddq_f32(iMatrixLeft.val[1], iMatrixRight.val[1]);
|
||||
m.val[2] = vaddq_f32(iMatrixLeft.val[2], iMatrixRight.val[2]);
|
||||
m.val[3] = vaddq_f32(iMatrixLeft.val[3], iMatrixRight.val[3]);
|
||||
|
||||
return *(GLKMatrix4 *)&m;
|
||||
#else
|
||||
GLKMatrix4 m;
|
||||
|
||||
m.m[0] = matrixLeft.m[0] + matrixRight.m[0];
|
||||
m.m[1] = matrixLeft.m[1] + matrixRight.m[1];
|
||||
m.m[2] = matrixLeft.m[2] + matrixRight.m[2];
|
||||
m.m[3] = matrixLeft.m[3] + matrixRight.m[3];
|
||||
|
||||
m.m[4] = matrixLeft.m[4] + matrixRight.m[4];
|
||||
m.m[5] = matrixLeft.m[5] + matrixRight.m[5];
|
||||
m.m[6] = matrixLeft.m[6] + matrixRight.m[6];
|
||||
m.m[7] = matrixLeft.m[7] + matrixRight.m[7];
|
||||
|
||||
m.m[8] = matrixLeft.m[8] + matrixRight.m[8];
|
||||
m.m[9] = matrixLeft.m[9] + matrixRight.m[9];
|
||||
m.m[10] = matrixLeft.m[10] + matrixRight.m[10];
|
||||
m.m[11] = matrixLeft.m[11] + matrixRight.m[11];
|
||||
|
||||
m.m[12] = matrixLeft.m[12] + matrixRight.m[12];
|
||||
m.m[13] = matrixLeft.m[13] + matrixRight.m[13];
|
||||
m.m[14] = matrixLeft.m[14] + matrixRight.m[14];
|
||||
m.m[15] = matrixLeft.m[15] + matrixRight.m[15];
|
||||
|
||||
return m;
|
||||
#endif
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Subtract(GLKMatrix4 matrixLeft, GLKMatrix4 matrixRight)
|
||||
{
|
||||
#if defined(__ARM_NEON__)
|
||||
float32x4x4_t iMatrixLeft = *(float32x4x4_t *)&matrixLeft;
|
||||
float32x4x4_t iMatrixRight = *(float32x4x4_t *)&matrixRight;
|
||||
float32x4x4_t m;
|
||||
|
||||
m.val[0] = vsubq_f32(iMatrixLeft.val[0], iMatrixRight.val[0]);
|
||||
m.val[1] = vsubq_f32(iMatrixLeft.val[1], iMatrixRight.val[1]);
|
||||
m.val[2] = vsubq_f32(iMatrixLeft.val[2], iMatrixRight.val[2]);
|
||||
m.val[3] = vsubq_f32(iMatrixLeft.val[3], iMatrixRight.val[3]);
|
||||
|
||||
return *(GLKMatrix4 *)&m;
|
||||
#else
|
||||
GLKMatrix4 m;
|
||||
|
||||
m.m[0] = matrixLeft.m[0] - matrixRight.m[0];
|
||||
m.m[1] = matrixLeft.m[1] - matrixRight.m[1];
|
||||
m.m[2] = matrixLeft.m[2] - matrixRight.m[2];
|
||||
m.m[3] = matrixLeft.m[3] - matrixRight.m[3];
|
||||
|
||||
m.m[4] = matrixLeft.m[4] - matrixRight.m[4];
|
||||
m.m[5] = matrixLeft.m[5] - matrixRight.m[5];
|
||||
m.m[6] = matrixLeft.m[6] - matrixRight.m[6];
|
||||
m.m[7] = matrixLeft.m[7] - matrixRight.m[7];
|
||||
|
||||
m.m[8] = matrixLeft.m[8] - matrixRight.m[8];
|
||||
m.m[9] = matrixLeft.m[9] - matrixRight.m[9];
|
||||
m.m[10] = matrixLeft.m[10] - matrixRight.m[10];
|
||||
m.m[11] = matrixLeft.m[11] - matrixRight.m[11];
|
||||
|
||||
m.m[12] = matrixLeft.m[12] - matrixRight.m[12];
|
||||
m.m[13] = matrixLeft.m[13] - matrixRight.m[13];
|
||||
m.m[14] = matrixLeft.m[14] - matrixRight.m[14];
|
||||
m.m[15] = matrixLeft.m[15] - matrixRight.m[15];
|
||||
|
||||
return m;
|
||||
#endif
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Translate(GLKMatrix4 matrix, float tx, float ty, float tz)
|
||||
{
|
||||
GLKMatrix4 m = { matrix.m[0], matrix.m[1], matrix.m[2], matrix.m[3],
|
||||
matrix.m[4], matrix.m[5], matrix.m[6], matrix.m[7],
|
||||
matrix.m[8], matrix.m[9], matrix.m[10], matrix.m[11],
|
||||
matrix.m[0] * tx + matrix.m[4] * ty + matrix.m[8] * tz + matrix.m[12],
|
||||
matrix.m[1] * tx + matrix.m[5] * ty + matrix.m[9] * tz + matrix.m[13],
|
||||
matrix.m[2] * tx + matrix.m[6] * ty + matrix.m[10] * tz + matrix.m[14],
|
||||
matrix.m[15] };
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4TranslateWithVector3(GLKMatrix4 matrix, GLKVector3 translationVector)
|
||||
{
|
||||
GLKMatrix4 m = { matrix.m[0], matrix.m[1], matrix.m[2], matrix.m[3],
|
||||
matrix.m[4], matrix.m[5], matrix.m[6], matrix.m[7],
|
||||
matrix.m[8], matrix.m[9], matrix.m[10], matrix.m[11],
|
||||
matrix.m[0] * translationVector.v[0] + matrix.m[4] * translationVector.v[1] + matrix.m[8] * translationVector.v[2] + matrix.m[12],
|
||||
matrix.m[1] * translationVector.v[0] + matrix.m[5] * translationVector.v[1] + matrix.m[9] * translationVector.v[2] + matrix.m[13],
|
||||
matrix.m[2] * translationVector.v[0] + matrix.m[6] * translationVector.v[1] + matrix.m[10] * translationVector.v[2] + matrix.m[14],
|
||||
matrix.m[15] };
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4TranslateWithVector4(GLKMatrix4 matrix, GLKVector4 translationVector)
|
||||
{
|
||||
GLKMatrix4 m = { matrix.m[0], matrix.m[1], matrix.m[2], matrix.m[3],
|
||||
matrix.m[4], matrix.m[5], matrix.m[6], matrix.m[7],
|
||||
matrix.m[8], matrix.m[9], matrix.m[10], matrix.m[11],
|
||||
matrix.m[0] * translationVector.v[0] + matrix.m[4] * translationVector.v[1] + matrix.m[8] * translationVector.v[2] + matrix.m[12],
|
||||
matrix.m[1] * translationVector.v[0] + matrix.m[5] * translationVector.v[1] + matrix.m[9] * translationVector.v[2] + matrix.m[13],
|
||||
matrix.m[2] * translationVector.v[0] + matrix.m[6] * translationVector.v[1] + matrix.m[10] * translationVector.v[2] + matrix.m[14],
|
||||
matrix.m[15] };
|
||||
return m;
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Scale(GLKMatrix4 matrix, float sx, float sy, float sz)
|
||||
{
|
||||
#if defined(__ARM_NEON__)
|
||||
float32x4x4_t iMatrix = *(float32x4x4_t *)&matrix;
|
||||
float32x4x4_t m;
|
||||
|
||||
m.val[0] = vmulq_n_f32(iMatrix.val[0], (float32_t)sx);
|
||||
m.val[1] = vmulq_n_f32(iMatrix.val[1], (float32_t)sy);
|
||||
m.val[2] = vmulq_n_f32(iMatrix.val[2], (float32_t)sz);
|
||||
m.val[3] = iMatrix.val[3];
|
||||
|
||||
return *(GLKMatrix4 *)&m;
|
||||
#else
|
||||
GLKMatrix4 m = { matrix.m[0] * sx, matrix.m[1] * sx, matrix.m[2] * sx, matrix.m[3] * sx,
|
||||
matrix.m[4] * sy, matrix.m[5] * sy, matrix.m[6] * sy, matrix.m[7] * sy,
|
||||
matrix.m[8] * sz, matrix.m[9] * sz, matrix.m[10] * sz, matrix.m[11] * sz,
|
||||
matrix.m[12], matrix.m[13], matrix.m[14], matrix.m[15] };
|
||||
return m;
|
||||
#endif
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4ScaleWithVector3(GLKMatrix4 matrix, GLKVector3 scaleVector)
|
||||
{
|
||||
#if defined(__ARM_NEON__)
|
||||
float32x4x4_t iMatrix = *(float32x4x4_t *)&matrix;
|
||||
float32x4x4_t m;
|
||||
|
||||
m.val[0] = vmulq_n_f32(iMatrix.val[0], (float32_t)scaleVector.v[0]);
|
||||
m.val[1] = vmulq_n_f32(iMatrix.val[1], (float32_t)scaleVector.v[1]);
|
||||
m.val[2] = vmulq_n_f32(iMatrix.val[2], (float32_t)scaleVector.v[2]);
|
||||
m.val[3] = iMatrix.val[3];
|
||||
|
||||
return *(GLKMatrix4 *)&m;
|
||||
#else
|
||||
GLKMatrix4 m = { matrix.m[0] * scaleVector.v[0], matrix.m[1] * scaleVector.v[0], matrix.m[2] * scaleVector.v[0], matrix.m[3] * scaleVector.v[0],
|
||||
matrix.m[4] * scaleVector.v[1], matrix.m[5] * scaleVector.v[1], matrix.m[6] * scaleVector.v[1], matrix.m[7] * scaleVector.v[1],
|
||||
matrix.m[8] * scaleVector.v[2], matrix.m[9] * scaleVector.v[2], matrix.m[10] * scaleVector.v[2], matrix.m[11] * scaleVector.v[2],
|
||||
matrix.m[12], matrix.m[13], matrix.m[14], matrix.m[15] };
|
||||
return m;
|
||||
#endif
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4ScaleWithVector4(GLKMatrix4 matrix, GLKVector4 scaleVector)
|
||||
{
|
||||
#if defined(__ARM_NEON__)
|
||||
float32x4x4_t iMatrix = *(float32x4x4_t *)&matrix;
|
||||
float32x4x4_t m;
|
||||
|
||||
m.val[0] = vmulq_n_f32(iMatrix.val[0], (float32_t)scaleVector.v[0]);
|
||||
m.val[1] = vmulq_n_f32(iMatrix.val[1], (float32_t)scaleVector.v[1]);
|
||||
m.val[2] = vmulq_n_f32(iMatrix.val[2], (float32_t)scaleVector.v[2]);
|
||||
m.val[3] = iMatrix.val[3];
|
||||
|
||||
return *(GLKMatrix4 *)&m;
|
||||
#else
|
||||
GLKMatrix4 m = { matrix.m[0] * scaleVector.v[0], matrix.m[1] * scaleVector.v[0], matrix.m[2] * scaleVector.v[0], matrix.m[3] * scaleVector.v[0],
|
||||
matrix.m[4] * scaleVector.v[1], matrix.m[5] * scaleVector.v[1], matrix.m[6] * scaleVector.v[1], matrix.m[7] * scaleVector.v[1],
|
||||
matrix.m[8] * scaleVector.v[2], matrix.m[9] * scaleVector.v[2], matrix.m[10] * scaleVector.v[2], matrix.m[11] * scaleVector.v[2],
|
||||
matrix.m[12], matrix.m[13], matrix.m[14], matrix.m[15] };
|
||||
return m;
|
||||
#endif
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4Rotate(GLKMatrix4 matrix, float radians, float x, float y, float z)
|
||||
{
|
||||
GLKMatrix4 rm = GLKMatrix4MakeRotation(radians, x, y, z);
|
||||
return GLKMatrix4Multiply(matrix, rm);
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4RotateWithVector3(GLKMatrix4 matrix, float radians, GLKVector3 axisVector)
|
||||
{
|
||||
GLKMatrix4 rm = GLKMatrix4MakeRotation(radians, axisVector.v[0], axisVector.v[1], axisVector.v[2]);
|
||||
return GLKMatrix4Multiply(matrix, rm);
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4RotateWithVector4(GLKMatrix4 matrix, float radians, GLKVector4 axisVector)
|
||||
{
|
||||
GLKMatrix4 rm = GLKMatrix4MakeRotation(radians, axisVector.v[0], axisVector.v[1], axisVector.v[2]);
|
||||
return GLKMatrix4Multiply(matrix, rm);
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4RotateX(GLKMatrix4 matrix, float radians)
|
||||
{
|
||||
GLKMatrix4 rm = GLKMatrix4MakeXRotation(radians);
|
||||
return GLKMatrix4Multiply(matrix, rm);
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4RotateY(GLKMatrix4 matrix, float radians)
|
||||
{
|
||||
GLKMatrix4 rm = GLKMatrix4MakeYRotation(radians);
|
||||
return GLKMatrix4Multiply(matrix, rm);
|
||||
}
|
||||
|
||||
static __inline__ GLKMatrix4 GLKMatrix4RotateZ(GLKMatrix4 matrix, float radians)
|
||||
{
|
||||
GLKMatrix4 rm = GLKMatrix4MakeZRotation(radians);
|
||||
return GLKMatrix4Multiply(matrix, rm);
|
||||
}
|
||||
|
||||
static __inline__ GLKVector3 GLKMatrix4MultiplyVector3(GLKMatrix4 matrixLeft, GLKVector3 vectorRight)
|
||||
{
|
||||
GLKVector4 v4 = GLKMatrix4MultiplyVector4(matrixLeft, GLKVector4Make(vectorRight.v[0], vectorRight.v[1], vectorRight.v[2], 0.0f));
|
||||
return GLKVector3Make(v4.v[0], v4.v[1], v4.v[2]);
|
||||
}
|
||||
|
||||
static __inline__ GLKVector3 GLKMatrix4MultiplyVector3WithTranslation(GLKMatrix4 matrixLeft, GLKVector3 vectorRight)
|
||||
{
|
||||
GLKVector4 v4 = GLKMatrix4MultiplyVector4(matrixLeft, GLKVector4Make(vectorRight.v[0], vectorRight.v[1], vectorRight.v[2], 1.0f));
|
||||
return GLKVector3Make(v4.v[0], v4.v[1], v4.v[2]);
|
||||
}
|
||||
|
||||
static __inline__ GLKVector3 GLKMatrix4MultiplyAndProjectVector3(GLKMatrix4 matrixLeft, GLKVector3 vectorRight)
|
||||
{
|
||||
GLKVector4 v4 = GLKMatrix4MultiplyVector4(matrixLeft, GLKVector4Make(vectorRight.v[0], vectorRight.v[1], vectorRight.v[2], 1.0f));
|
||||
return GLKVector3MultiplyScalar(GLKVector3Make(v4.v[0], v4.v[1], v4.v[2]), 1.0f / v4.v[3]);
|
||||
}
|
||||
|
||||
static __inline__ void GLKMatrix4MultiplyVector3Array(GLKMatrix4 matrix, GLKVector3 *vectors, size_t vectorCount)
|
||||
{
|
||||
size_t i;
|
||||
for (i=0; i < vectorCount; i++)
|
||||
vectors[i] = GLKMatrix4MultiplyVector3(matrix, vectors[i]);
|
||||
}
|
||||
|
||||
static __inline__ void GLKMatrix4MultiplyVector3ArrayWithTranslation(GLKMatrix4 matrix, GLKVector3 *vectors, size_t vectorCount)
|
||||
{
|
||||
size_t i;
|
||||
for (i=0; i < vectorCount; i++)
|
||||
vectors[i] = GLKMatrix4MultiplyVector3WithTranslation(matrix, vectors[i]);
|
||||
}
|
||||
|
||||
static __inline__ void GLKMatrix4MultiplyAndProjectVector3Array(GLKMatrix4 matrix, GLKVector3 *vectors, size_t vectorCount)
|
||||
{
|
||||
size_t i;
|
||||
for (i=0; i < vectorCount; i++)
|
||||
vectors[i] = GLKMatrix4MultiplyAndProjectVector3(matrix, vectors[i]);
|
||||
}
|
||||
|
||||
static __inline__ GLKVector4 GLKMatrix4MultiplyVector4(GLKMatrix4 matrixLeft, GLKVector4 vectorRight)
|
||||
{
|
||||
#if defined(__ARM_NEON__)
|
||||
float32x4x4_t iMatrix = *(float32x4x4_t *)&matrixLeft;
|
||||
float32x4_t v;
|
||||
|
||||
iMatrix.val[0] = vmulq_n_f32(iMatrix.val[0], (float32_t)vectorRight.v[0]);
|
||||
iMatrix.val[1] = vmulq_n_f32(iMatrix.val[1], (float32_t)vectorRight.v[1]);
|
||||
iMatrix.val[2] = vmulq_n_f32(iMatrix.val[2], (float32_t)vectorRight.v[2]);
|
||||
iMatrix.val[3] = vmulq_n_f32(iMatrix.val[3], (float32_t)vectorRight.v[3]);
|
||||
|
||||
iMatrix.val[0] = vaddq_f32(iMatrix.val[0], iMatrix.val[1]);
|
||||
iMatrix.val[2] = vaddq_f32(iMatrix.val[2], iMatrix.val[3]);
|
||||
|
||||
v = vaddq_f32(iMatrix.val[0], iMatrix.val[2]);
|
||||
|
||||
return *(GLKVector4 *)&v;
|
||||
#else
|
||||
GLKVector4 v = { matrixLeft.m[0] * vectorRight.v[0] + matrixLeft.m[4] * vectorRight.v[1] + matrixLeft.m[8] * vectorRight.v[2] + matrixLeft.m[12] * vectorRight.v[3],
|
||||
matrixLeft.m[1] * vectorRight.v[0] + matrixLeft.m[5] * vectorRight.v[1] + matrixLeft.m[9] * vectorRight.v[2] + matrixLeft.m[13] * vectorRight.v[3],
|
||||
matrixLeft.m[2] * vectorRight.v[0] + matrixLeft.m[6] * vectorRight.v[1] + matrixLeft.m[10] * vectorRight.v[2] + matrixLeft.m[14] * vectorRight.v[3],
|
||||
matrixLeft.m[3] * vectorRight.v[0] + matrixLeft.m[7] * vectorRight.v[1] + matrixLeft.m[11] * vectorRight.v[2] + matrixLeft.m[15] * vectorRight.v[3] };
|
||||
return v;
|
||||
#endif
|
||||
}
|
||||
|
||||
static __inline__ void GLKMatrix4MultiplyVector4Array(GLKMatrix4 matrix, GLKVector4 *vectors, size_t vectorCount)
|
||||
{
|
||||
size_t i;
|
||||
for (i=0; i < vectorCount; i++)
|
||||
vectors[i] = GLKMatrix4MultiplyVector4(matrix, vectors[i]);
|
||||
}
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __GLK_MATRIX_4_H */
|
||||
99
samples/C/NWMan.h
Normal file
99
samples/C/NWMan.h
Normal file
@@ -0,0 +1,99 @@
|
||||
#ifndef _NME_WMAN_H
|
||||
#define _NME_WMAN_H
|
||||
|
||||
// Internal window manager API
|
||||
|
||||
#include "NCompat.h"
|
||||
|
||||
START_HEAD
|
||||
|
||||
#include "NPos.h"
|
||||
#include "NUtil.h"
|
||||
#include "NTypes.h"
|
||||
|
||||
NTS(NWMan_event);
|
||||
|
||||
NSTRUCT(NWMan, {
|
||||
// Init stuff
|
||||
bool (*init)();
|
||||
bool (*destroy)();
|
||||
|
||||
// Window stuff
|
||||
bool (*create_window)();
|
||||
bool (*destroy_window)();
|
||||
|
||||
void (*swap_buffers)();
|
||||
|
||||
// Event stuff
|
||||
bool (*next_event)(NWMan_event* event);
|
||||
|
||||
// Time stuff
|
||||
uint (*get_millis)();
|
||||
void (*sleep)(uint millis);
|
||||
|
||||
// Info
|
||||
int rshift_key;
|
||||
int lshift_key;
|
||||
int left_key;
|
||||
int right_key;
|
||||
});
|
||||
|
||||
NENUM(NWMan_event_type, {
|
||||
N_WMAN_MOUSE_MOVE = 0,
|
||||
N_WMAN_MOUSE_BUTTON = 1,
|
||||
N_WMAN_MOUSE_WHEEL = 2,
|
||||
|
||||
N_WMAN_KEYBOARD = 10,
|
||||
|
||||
N_WMAN_QUIT = 20,
|
||||
N_WMAN_RESIZE = 21,
|
||||
N_WMAN_FOCUS = 22
|
||||
});
|
||||
|
||||
#define N_WMAN_MOUSE_LEFT 0
|
||||
#define N_WMAN_MOUSE_RIGHT 1
|
||||
#define N_WMAN_MOUSE_MIDDLE 2
|
||||
|
||||
NSTRUCT(NWMan_event, {
|
||||
NWMan_event_type type;
|
||||
|
||||
union {
|
||||
// Mouse
|
||||
|
||||
NPos2i mouse_pos;
|
||||
|
||||
struct {
|
||||
short id;
|
||||
bool state;
|
||||
} mouse_button;
|
||||
|
||||
signed char mouse_wheel; // 1 if up, -1 if down
|
||||
|
||||
// Keyboard
|
||||
|
||||
struct {
|
||||
int key;
|
||||
bool state;
|
||||
} keyboard;
|
||||
|
||||
// Window
|
||||
|
||||
bool window_quit; // Will always be true if WM_QUIT
|
||||
|
||||
NPos2i window_size;
|
||||
|
||||
bool window_focus;
|
||||
};
|
||||
});
|
||||
|
||||
NWMan_event NWMan_event_new(NWMan_event_type type);
|
||||
|
||||
|
||||
bool NWMan_init();
|
||||
bool NWMan_destroy();
|
||||
|
||||
extern NWMan N_WMan;
|
||||
|
||||
END_HEAD
|
||||
|
||||
#endif
|
||||
27
samples/C/Nightmare.h
Normal file
27
samples/C/Nightmare.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef _NMEX_NIGHTMARE_H
|
||||
#define _NMEX_NIGHTMARE_H
|
||||
|
||||
//#define NMEX
|
||||
|
||||
#include "../src/NCompat.h"
|
||||
|
||||
START_HEAD
|
||||
|
||||
#include "../src/NTypes.h"
|
||||
#include "../src/NUtil.h"
|
||||
#include "../src/NPorting.h"
|
||||
#include "../src/NGlobals.h"
|
||||
#include "../src/NLog.h"
|
||||
#include "../src/NWMan.h"
|
||||
#include "../src/NRsc.h"
|
||||
#include "../src/NShader.h"
|
||||
#include "../src/NSquare.h"
|
||||
#include "../src/NImage.h"
|
||||
#include "../src/NSprite.h"
|
||||
#include "../src/NSpritesheet.h"
|
||||
#include "../src/NEntity.h"
|
||||
#include "../src/Game.h"
|
||||
|
||||
END_HEAD
|
||||
|
||||
#endif
|
||||
89
samples/C/ntru_encrypt.h
Normal file
89
samples/C/ntru_encrypt.h
Normal file
@@ -0,0 +1,89 @@
|
||||
/*
|
||||
* Copyright (C) 2014 FH Bielefeld
|
||||
*
|
||||
* This file is part of a FH Bielefeld project.
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||||
* MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
/**
|
||||
* @file ntru_encrypt.h
|
||||
* Header for the internal API of ntru_encrypt.c.
|
||||
* @brief header for encrypt.c
|
||||
*/
|
||||
|
||||
#ifndef PQC_ENCRYPT_H
|
||||
#define PQC_ENCRYPT_H
|
||||
|
||||
|
||||
#include "ntru_params.h"
|
||||
#include "ntru_poly.h"
|
||||
#include "ntru_string.h"
|
||||
|
||||
#include <fmpz_poly.h>
|
||||
#include <fmpz.h>
|
||||
|
||||
|
||||
/**
|
||||
* encrypt the msg, using the math:
|
||||
* e = (h ∗ r) + m (mod q)
|
||||
*
|
||||
* e = the encrypted poly
|
||||
*
|
||||
* h = the public key
|
||||
*
|
||||
* r = the random poly
|
||||
*
|
||||
* m = the message poly
|
||||
*
|
||||
* q = large mod
|
||||
*
|
||||
* @param msg_tern the message to encrypt, in ternary format
|
||||
* @param pub_key the public key
|
||||
* @param rnd the random poly (should have relatively small
|
||||
* coefficients, but not restricted to {-1, 0, 1})
|
||||
* @param out the output poly which is in the range {0, q-1}
|
||||
* (not ternary!) [out]
|
||||
* @param params ntru_params the ntru context
|
||||
*/
|
||||
void
|
||||
ntru_encrypt_poly(
|
||||
const fmpz_poly_t msg_tern,
|
||||
const fmpz_poly_t pub_key,
|
||||
const fmpz_poly_t rnd,
|
||||
fmpz_poly_t out,
|
||||
const ntru_params *params);
|
||||
|
||||
/**
|
||||
* Encrypt a message in the form of a null-terminated char array and
|
||||
* return a string.
|
||||
*
|
||||
* @param msg the message
|
||||
* @param pub_key the public key
|
||||
* @param rnd the random poly (should have relatively small
|
||||
* coefficients, but not restricted to {-1, 0, 1})
|
||||
* @param params ntru_params the ntru context
|
||||
* @return the newly allocated encrypted string
|
||||
*/
|
||||
string *
|
||||
ntru_encrypt_string(
|
||||
const string *msg,
|
||||
const fmpz_poly_t pub_key,
|
||||
const fmpz_poly_t rnd,
|
||||
const ntru_params *params);
|
||||
|
||||
|
||||
#endif /* PQC_ENCRYPT_H */
|
||||
26
samples/Cool/list.cl
Normal file
26
samples/Cool/list.cl
Normal file
@@ -0,0 +1,26 @@
|
||||
(* This simple example of a list class is adapted from an example in the
|
||||
Cool distribution. *)
|
||||
|
||||
class List {
|
||||
isNil() : Bool { true };
|
||||
head() : Int { { abort(); 0; } };
|
||||
tail() : List { { abort(); self; } };
|
||||
cons(i : Int) : List {
|
||||
(new Cons).init(i, self)
|
||||
};
|
||||
};
|
||||
|
||||
class Cons inherits List {
|
||||
car : Int; -- The element in this list cell
|
||||
cdr : List; -- The rest of the list
|
||||
isNil() : Bool { false };
|
||||
head() : Int { car };
|
||||
tail() : List { cdr };
|
||||
init(i : Int, rest : List) : List {
|
||||
{
|
||||
car <- i;
|
||||
cdr <- rest;
|
||||
self;
|
||||
}
|
||||
};
|
||||
};
|
||||
71
samples/Cool/sample.cl
Normal file
71
samples/Cool/sample.cl
Normal file
@@ -0,0 +1,71 @@
|
||||
(* Refer to Alex Aiken, "The Cool Reference Manual":
|
||||
http://theory.stanford.edu/~aiken/software/cool/cool-manual.pdf
|
||||
for language specification.
|
||||
*)
|
||||
|
||||
-- Exhibit various language constructs
|
||||
class Sample {
|
||||
testCondition(x: Int): Bool {
|
||||
if x = 0
|
||||
then false
|
||||
else
|
||||
if x < (1 + 2) * 3
|
||||
then true
|
||||
else false
|
||||
fi
|
||||
fi
|
||||
};
|
||||
|
||||
testLoop(y: Int): Bool {
|
||||
while y > 0 loop
|
||||
{
|
||||
if not condition(y)
|
||||
then y <- y / 2
|
||||
else y <- y - 1;
|
||||
}
|
||||
pool
|
||||
};
|
||||
|
||||
testAssign(z: Int): Bool {
|
||||
i : Int;
|
||||
i <- ~z;
|
||||
};
|
||||
|
||||
testCase(var: Sample): SELF_TYPE {
|
||||
io : IO <- new IO;
|
||||
case var of
|
||||
a : A => io.out_string("Class type is A\n");
|
||||
b : B => io.out_string("Class type is B\n");
|
||||
s : Sample => io.out_string("Class type is Sample\n");
|
||||
o : Object => io.out_string("Class type is object\n");
|
||||
esac
|
||||
};
|
||||
|
||||
testLet(i: Int): Int {
|
||||
let (a: Int in
|
||||
let(b: Int <- 3, c: Int <- 4 in
|
||||
{
|
||||
a <- 2;
|
||||
a * b * 2 / c;
|
||||
}
|
||||
)
|
||||
)
|
||||
};
|
||||
};
|
||||
|
||||
-- Used to test subclasses
|
||||
class A inherits Sample {};
|
||||
class B inherits A {};
|
||||
|
||||
class C {
|
||||
main() : Int {
|
||||
(new Sample).testLet(1)
|
||||
};
|
||||
};
|
||||
|
||||
-- "Hello, world" example
|
||||
class Main inherits IO {
|
||||
main(): SELF_TYPE {
|
||||
out_string("Hello, World.\n")
|
||||
};
|
||||
};
|
||||
238
samples/Gosu/Ronin.gs
Normal file
238
samples/Gosu/Ronin.gs
Normal file
@@ -0,0 +1,238 @@
|
||||
/**
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
package ronin
|
||||
|
||||
uses gw.util.concurrent.LockingLazyVar
|
||||
uses gw.lang.reflect.*
|
||||
uses java.lang.*
|
||||
uses java.io.*
|
||||
uses ronin.config.*
|
||||
uses org.slf4j.*
|
||||
|
||||
/**
|
||||
* The central location for Ronin utility methods. Controllers and templates should generally access the
|
||||
* methods and properties they inherit from {@link ronin.IRoninUtils} instead of using the methods and
|
||||
* properties here.
|
||||
*/
|
||||
class Ronin {
|
||||
|
||||
// One static field to rule the all...
|
||||
static var _CONFIG : IRoninConfig as Config
|
||||
|
||||
// And one thread local to bind them
|
||||
static var _CURRENT_REQUEST = new ThreadLocal<RoninRequest>();
|
||||
|
||||
// That's inconstructable
|
||||
private construct() {}
|
||||
|
||||
internal static function init(servlet : RoninServlet, m : ApplicationMode, src : File) {
|
||||
if(_CONFIG != null) {
|
||||
throw "Cannot initialize a Ronin application multiple times!"
|
||||
}
|
||||
var cfg = TypeSystem.getByFullNameIfValid("config.RoninConfig")
|
||||
var defaultWarning = false
|
||||
if(cfg != null) {
|
||||
var ctor = cfg.TypeInfo.getConstructor({ronin.config.ApplicationMode, ronin.RoninServlet})
|
||||
if(ctor == null) {
|
||||
throw "config.RoninConfig must have a constructor with the same signature as ronin.config.RoninConfig"
|
||||
}
|
||||
_CONFIG = ctor.Constructor.newInstance({m, servlet}) as IRoninConfig
|
||||
} else {
|
||||
_CONFIG = new DefaultRoninConfig(m, servlet)
|
||||
defaultWarning = true
|
||||
}
|
||||
var roninLogger = TypeSystem.getByFullNameIfValid("ronin.RoninLoggerFactory")
|
||||
if(roninLogger != null) {
|
||||
roninLogger.TypeInfo.getMethod("init", {ronin.config.LogLevel}).CallHandler.handleCall(null, {LogLevel})
|
||||
}
|
||||
if(defaultWarning) {
|
||||
log("No configuration was found at config.RoninConfig, using the default configuration...", :level=WARN)
|
||||
}
|
||||
Quartz.maybeStart()
|
||||
ReloadManager.setSourceRoot(src)
|
||||
}
|
||||
|
||||
internal static property set CurrentRequest(req : RoninRequest) {
|
||||
_CURRENT_REQUEST.set(req)
|
||||
}
|
||||
|
||||
//============================================
|
||||
// Public API
|
||||
//============================================
|
||||
|
||||
/**
|
||||
* The trace handler for the current request.
|
||||
*/
|
||||
static property get CurrentTrace() : Trace {
|
||||
return CurrentRequest?.Trace
|
||||
}
|
||||
|
||||
/**
|
||||
* Ronin's representation of the current request.
|
||||
*/
|
||||
static property get CurrentRequest() : RoninRequest {
|
||||
return _CURRENT_REQUEST.get()
|
||||
}
|
||||
|
||||
/**
|
||||
* The mode in which this application is running.
|
||||
*/
|
||||
static property get Mode() : ApplicationMode {
|
||||
return _CONFIG?.Mode ?: TESTING
|
||||
}
|
||||
|
||||
/**
|
||||
* The log level at and above which log messages should be displayed.
|
||||
*/
|
||||
static property get LogLevel() : LogLevel {
|
||||
return _CONFIG?.LogLevel ?: DEBUG
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether or not to display detailed trace information on each request.
|
||||
*/
|
||||
static property get TraceEnabled() : boolean {
|
||||
return _CONFIG != null ? _CONFIG.TraceEnabled : true
|
||||
}
|
||||
|
||||
/**
|
||||
* The default controller method to call when no method name is present in the request URL.
|
||||
*/
|
||||
static property get DefaultAction() : String {
|
||||
return _CONFIG?.DefaultAction
|
||||
}
|
||||
|
||||
/**
|
||||
* The default controller to call when no controller name is present in the request URL.
|
||||
*/
|
||||
static property get DefaultController() : Type {
|
||||
return _CONFIG?.DefaultController
|
||||
}
|
||||
|
||||
/**
|
||||
* The servlet responsible for handling Ronin requests.
|
||||
*/
|
||||
static property get RoninServlet() : RoninServlet {
|
||||
return _CONFIG?.RoninServlet
|
||||
}
|
||||
|
||||
/**
|
||||
* The handler for request processing errors.
|
||||
*/
|
||||
static property get ErrorHandler() : IErrorHandler {
|
||||
return _CONFIG?.ErrorHandler
|
||||
}
|
||||
|
||||
/**
|
||||
* The custom handler for logging messages.
|
||||
*/
|
||||
static property get LogHandler() : ILogHandler {
|
||||
return _CONFIG?.LogHandler
|
||||
}
|
||||
|
||||
/**
|
||||
* Logs a message using the configured log handler.
|
||||
* @param msg The text of the message to log, or a block which returns said text.
|
||||
* @param level (Optional) The level at which to log the message.
|
||||
* @param component (Optional) The logical component from whence the message originated.
|
||||
* @param exception (Optional) An exception to associate with the message.
|
||||
*/
|
||||
static function log(msg : Object, level : LogLevel = null, component : String = null, exception : java.lang.Throwable = null) {
|
||||
if(level == null) {
|
||||
level = INFO
|
||||
}
|
||||
if(LogLevel <= level) {
|
||||
var msgStr : String
|
||||
if(msg typeis block():String) {
|
||||
msgStr = (msg as block():String)()
|
||||
} else {
|
||||
msgStr = msg as String
|
||||
}
|
||||
if(_CONFIG?.LogHandler != null) {
|
||||
_CONFIG.LogHandler.log(msgStr, level, component, exception)
|
||||
} else {
|
||||
switch(level) {
|
||||
case TRACE:
|
||||
LoggerFactory.getLogger(component?:Logger.ROOT_LOGGER_NAME).trace(msgStr, exception)
|
||||
break
|
||||
case DEBUG:
|
||||
LoggerFactory.getLogger(component?:Logger.ROOT_LOGGER_NAME).debug(msgStr, exception)
|
||||
break
|
||||
case INFO:
|
||||
LoggerFactory.getLogger(component?:Logger.ROOT_LOGGER_NAME).info(msgStr, exception)
|
||||
break
|
||||
case WARN:
|
||||
LoggerFactory.getLogger(component?:Logger.ROOT_LOGGER_NAME).warn(msgStr, exception)
|
||||
break
|
||||
case ERROR:
|
||||
case FATAL:
|
||||
LoggerFactory.getLogger(component?:Logger.ROOT_LOGGER_NAME).error(msgStr, exception)
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The caches known to Ronin.
|
||||
*/
|
||||
static enum CacheStore {
|
||||
REQUEST,
|
||||
SESSION,
|
||||
APPLICATION
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves a value from a cache, or computes and stores it if it is not in the cache.
|
||||
* @param value A block which will compute the desired value.
|
||||
* @param name (Optional) A unique identifier for the value. Default is null, which means one will be
|
||||
* generated from the type of the value.
|
||||
* @param store (Optional) The cache store used to retrieve or store the value. Default is the request cache.
|
||||
* @return The retrieved or computed value.
|
||||
*/
|
||||
static function cache<T>(value : block():T, name : String = null, store : CacheStore = null) : T {
|
||||
if(store == null or store == REQUEST) {
|
||||
return _CONFIG.RequestCache.getValue(value, name)
|
||||
} else if (store == SESSION) {
|
||||
return _CONFIG.SessionCache.getValue(value, name)
|
||||
} else if (store == APPLICATION) {
|
||||
return _CONFIG.ApplicationCache.getValue(value, name)
|
||||
} else {
|
||||
throw "Don't know about CacheStore ${store}"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Invalidates a cached value in a cache.
|
||||
* @param name The unique identifier for the value.
|
||||
* @param store The cache store in which to invalidate the value.
|
||||
*/
|
||||
static function invalidate<T>(name : String, store : CacheStore) {
|
||||
if(store == null or store == REQUEST) {
|
||||
_CONFIG.RequestCache.invalidate(name)
|
||||
} else if (store == SESSION) {
|
||||
_CONFIG.SessionCache.invalidate(name)
|
||||
} else if (store == APPLICATION) {
|
||||
_CONFIG.ApplicationCache.invalidate(name)
|
||||
} else {
|
||||
throw "Don't know about CacheStore ${store}"
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Detects changes made to resources in the Ronin application and
|
||||
* reloads them. This function should only be called when Ronin is
|
||||
* in development mode.
|
||||
*/
|
||||
static function loadChanges() {
|
||||
ReloadManager.detectAndReloadChangedResources()
|
||||
}
|
||||
|
||||
}
|
||||
18
samples/Gradle/build.gradle
Normal file
18
samples/Gradle/build.gradle
Normal file
@@ -0,0 +1,18 @@
|
||||
apply plugin: GreetingPlugin
|
||||
|
||||
greeting.message = 'Hi from Gradle'
|
||||
|
||||
class GreetingPlugin implements Plugin<Project> {
|
||||
void apply(Project project) {
|
||||
// Add the 'greeting' extension object
|
||||
project.extensions.create("greeting", GreetingPluginExtension)
|
||||
// Add a task that uses the configuration
|
||||
project.task('hello') << {
|
||||
println project.greeting.message
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GreetingPluginExtension {
|
||||
def String message = 'Hello from GreetingPlugin'
|
||||
}
|
||||
20
samples/Gradle/builder.gradle
Normal file
20
samples/Gradle/builder.gradle
Normal file
@@ -0,0 +1,20 @@
|
||||
apply plugin: GreetingPlugin
|
||||
|
||||
greeting {
|
||||
message = 'Hi'
|
||||
greeter = 'Gradle'
|
||||
}
|
||||
|
||||
class GreetingPlugin implements Plugin<Project> {
|
||||
void apply(Project project) {
|
||||
project.extensions.create("greeting", GreetingPluginExtension)
|
||||
project.task('hello') << {
|
||||
println "${project.greeting.message} from ${project.greeting.greeter}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class GreetingPluginExtension {
|
||||
String message
|
||||
String greeter
|
||||
}
|
||||
78
samples/JavaScript/chart_composers.gs
Normal file
78
samples/JavaScript/chart_composers.gs
Normal file
@@ -0,0 +1,78 @@
|
||||
/*
|
||||
License
|
||||
Copyright [2013] [Farruco Sanjurjo Arcay]
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
|
||||
*/
|
||||
|
||||
var TagsTotalPerMonth;
|
||||
|
||||
TagsTotalPerMonth = (function(){
|
||||
function TagsTotalPerMonth(){};
|
||||
|
||||
TagsTotalPerMonth.getDatasource = function (category, months, values){
|
||||
return new CategoryMonthlyExpenseBarChartDataSource(category, months, values);
|
||||
};
|
||||
|
||||
TagsTotalPerMonth.getType = function (){ return Charts.ChartType.COLUMN};
|
||||
|
||||
return TagsTotalPerMonth;
|
||||
})();
|
||||
|
||||
|
||||
var TagsTotalPerMonthWithMean;
|
||||
|
||||
TagsTotalPerMonthWithMean = (function(){
|
||||
function TagsTotalPerMonthWithMean(){};
|
||||
|
||||
TagsTotalPerMonthWithMean.getDatasource = function (category, months, values){
|
||||
return new CategoryMonthlyWithMeanExpenseDataSource(category, months, values);
|
||||
};
|
||||
|
||||
TagsTotalPerMonthWithMean.getType = function (){ return Charts.ChartType.LINE};
|
||||
|
||||
return TagsTotalPerMonthWithMean;
|
||||
})();
|
||||
|
||||
|
||||
var TagsAccumulatedPerMonth;
|
||||
|
||||
TagsAccumulatedPerMonth = (function(){
|
||||
function TagsAccumulatedPerMonth(){};
|
||||
|
||||
TagsAccumulatedPerMonth.getDatasource = function (category, months, values){
|
||||
return new CategoryMonthlyAccumulated(category, months, values);
|
||||
};
|
||||
|
||||
TagsAccumulatedPerMonth.getType = function (){ return Charts.ChartType.AREA};
|
||||
|
||||
return TagsAccumulatedPerMonth;
|
||||
})();
|
||||
|
||||
var MonthTotalsPerTags;
|
||||
|
||||
MonthTotalsPerTags = (function(){
|
||||
function MonthTotalsPerTags(){};
|
||||
|
||||
MonthTotalsPerTags.getDatasource = function (month, tags, values){
|
||||
return new CategoryExpenseDataSource(tags, month, values);
|
||||
};
|
||||
|
||||
MonthTotalsPerTags.getType = function (){ return Charts.ChartType.PIE; };
|
||||
|
||||
return MonthTotalsPerTags;
|
||||
})();
|
||||
|
||||
var SavingsFlowChartComposer = (function(){
|
||||
function SavingsFlowChartComposer(){};
|
||||
|
||||
SavingsFlowChartComposer.getDatasource = function(months, values){
|
||||
return new SavingsFlowDataSource(months, values);
|
||||
};
|
||||
|
||||
SavingsFlowChartComposer.getType = function(){ return Charts.ChartType.COLUMN; };
|
||||
|
||||
return SavingsFlowChartComposer;
|
||||
})();
|
||||
150
samples/JavaScript/itau.gs
Normal file
150
samples/JavaScript/itau.gs
Normal file
@@ -0,0 +1,150 @@
|
||||
/*
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2014 Thiago Brandão Damasceno
|
||||
|
||||
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.
|
||||
*/
|
||||
|
||||
// based on http://ctrlq.org/code/19053-send-to-google-drive
|
||||
function sendToGoogleDrive() {
|
||||
|
||||
var gmailLabels = 'inbox';
|
||||
var driveFolder = 'Itaú Notifications';
|
||||
var spreadsheetName = 'itau';
|
||||
var archiveLabel = 'itau.processed';
|
||||
var itauNotificationEmail = 'comunicacaodigital@itau-unibanco.com.br';
|
||||
var filter = "from: " +
|
||||
itauNotificationEmail +
|
||||
" -label:" +
|
||||
archiveLabel +
|
||||
" label:" +
|
||||
gmailLabels;
|
||||
|
||||
// Create label for 'itau.processed' if it doesn't exist
|
||||
var moveToLabel = GmailApp.getUserLabelByName(archiveLabel);
|
||||
|
||||
if (!moveToLabel) {
|
||||
moveToLabel = GmailApp.createLabel(archiveLabel);
|
||||
}
|
||||
|
||||
// Create folder 'Itaú Notifications' if it doesn't exist
|
||||
var folders = DriveApp.getFoldersByName(driveFolder);
|
||||
var folder;
|
||||
|
||||
if (folders.hasNext()) {
|
||||
folder = folders.next();
|
||||
} else {
|
||||
folder = DriveApp.createFolder(driveFolder);
|
||||
}
|
||||
|
||||
// Create spreadsheet file 'itau' if it doesn't exist
|
||||
var files = folder.getFilesByName(spreadsheetName);
|
||||
|
||||
// File is in DriveApp
|
||||
// Doc is in SpreadsheetApp
|
||||
// They are not interchangeable
|
||||
var file, doc;
|
||||
|
||||
// Confusing :\
|
||||
// As per: https://code.google.com/p/google-apps-script-issues/issues/detail?id=3578
|
||||
if (files.hasNext()){
|
||||
file = files.next();
|
||||
doc = SpreadsheetApp.openById(file.getId());
|
||||
} else {
|
||||
doc = SpreadsheetApp.create(spreadsheetName);
|
||||
file = DriveApp.getFileById(doc.getId());
|
||||
folder.addFile(file);
|
||||
DriveApp.removeFile(file);
|
||||
}
|
||||
|
||||
var sheet = doc.getSheets()[0];
|
||||
|
||||
// Append header if first line
|
||||
if(sheet.getLastRow() == 0){
|
||||
sheet.appendRow(['Conta', 'Operação', 'Valor', 'Data', 'Hora', 'Email ID']);
|
||||
}
|
||||
|
||||
var message, messages, account, operation, value, date, hour, emailID, plainBody;
|
||||
var accountRegex = /Conta: (XXX[0-9\-]+)/;
|
||||
var operationRegex = /Tipo de operação: ([A-Z]+)/;
|
||||
var paymentRegex = /Pagamento de ([0-9A-Za-z\-]+)\ ?([0-9]+)?/;
|
||||
var valueRegex = /Valor: R\$ ([0-9\,\.]+)/;
|
||||
var dateRegex = /Data: ([0-9\/]+)/;
|
||||
var hourRegex = /Hora: ([0-9\:]+)/;
|
||||
var emailIDRegex = /E-mail nº ([0-9]+)/;
|
||||
|
||||
var threads = GmailApp.search(filter, 0, 100);
|
||||
|
||||
for (var x = 0; x < threads.length; x++) {
|
||||
messages = threads[x].getMessages();
|
||||
|
||||
for (var i = 0; i < messages.length; i++) {
|
||||
account, operation, value, date, hour, emailID = [];
|
||||
|
||||
message = messages[i];
|
||||
|
||||
plainBody = message.getPlainBody();
|
||||
|
||||
if(accountRegex.test(plainBody)) {
|
||||
account = RegExp.$1;
|
||||
}
|
||||
|
||||
if(operationRegex.test(plainBody)) {
|
||||
operation = RegExp.$1;
|
||||
}
|
||||
|
||||
if(valueRegex.test(plainBody)) {
|
||||
value = RegExp.$1;
|
||||
}
|
||||
|
||||
if(dateRegex.test(plainBody)) {
|
||||
date = RegExp.$1;
|
||||
}
|
||||
|
||||
if(hourRegex.test(plainBody)) {
|
||||
hour = RegExp.$1;
|
||||
}
|
||||
|
||||
if(emailIDRegex.test(plainBody)){
|
||||
emailID = RegExp.$1;
|
||||
}
|
||||
|
||||
if(paymentRegex.test(plainBody)){
|
||||
operation = RegExp.$1;
|
||||
if(RegExp.$2){
|
||||
operation += ' ' + RegExp.$2
|
||||
}
|
||||
date = hour = ' - ';
|
||||
}
|
||||
|
||||
if(account && operation && value && date && hour){
|
||||
sheet.appendRow([account, operation, value, date, hour, emailID]);
|
||||
}
|
||||
|
||||
// Logger.log(account);
|
||||
// Logger.log(operation);
|
||||
// Logger.log(value);
|
||||
// Logger.log(date);
|
||||
// Logger.log(hour);
|
||||
}
|
||||
|
||||
threads[x].addLabel(moveToLabel);
|
||||
}
|
||||
}
|
||||
52
samples/Oz/example.oz
Normal file
52
samples/Oz/example.oz
Normal file
@@ -0,0 +1,52 @@
|
||||
% You can get a lot of information about Oz by following theses links :
|
||||
% - http://mozart.github.io/
|
||||
% - http://en.wikipedia.org/wiki/Oz_(programming_language)
|
||||
% There is also a well known book that uses Oz for pedagogical reason :
|
||||
% - http://mitpress.mit.edu/books/concepts-techniques-and-models-computer-programming
|
||||
% And there are two courses on edX about 'Paradigms of Computer Programming' that also uses Oz for pedagogical reason :
|
||||
% - https://www.edx.org/node/2751#.VHijtfl5OSo
|
||||
% - https://www.edx.org/node/4436#.VHijzfl5OSo
|
||||
%
|
||||
% Here is an example of some code written with Oz.
|
||||
|
||||
declare
|
||||
% Computes the sum of square of the N first integers.
|
||||
fun {Sum N}
|
||||
local SumAux in
|
||||
fun {SumAux N Acc}
|
||||
if N==0 then Acc
|
||||
else
|
||||
{Sum N-1 Acc}
|
||||
end
|
||||
end
|
||||
{SumAux N 0}
|
||||
end
|
||||
end
|
||||
|
||||
% Returns true if N is a prime and false otherwize
|
||||
fun {Prime N}
|
||||
local PrimeAcc in
|
||||
fun {PrimeAcc N Acc}
|
||||
if(N == 1) then false
|
||||
elseif(Acc == 1) then true
|
||||
else
|
||||
if (N mod Acc) == 0 then false
|
||||
else
|
||||
{PrimeAcc N Acc-1}
|
||||
end
|
||||
end
|
||||
end
|
||||
{PrimeAcc N (N div 2)}
|
||||
end
|
||||
end
|
||||
|
||||
% Reverse a list using cells and for loop (instead of recursivity)
|
||||
fun {Reverse L}
|
||||
local RevList in
|
||||
RevList = {NewCell nil}
|
||||
for E in L do
|
||||
RevList := E|@RevList
|
||||
end
|
||||
@RevList
|
||||
end
|
||||
end
|
||||
39
samples/RAML/api.raml
Normal file
39
samples/RAML/api.raml
Normal file
@@ -0,0 +1,39 @@
|
||||
#%RAML 0.8
|
||||
|
||||
title: World Music API
|
||||
baseUri: http://example.api.com/{version}
|
||||
version: v1
|
||||
traits:
|
||||
- paged:
|
||||
queryParameters:
|
||||
pages:
|
||||
description: The number of pages to return
|
||||
type: number
|
||||
- secured: !include http://raml-example.com/secured.yml
|
||||
/songs:
|
||||
is: [ paged, secured ]
|
||||
get:
|
||||
queryParameters:
|
||||
genre:
|
||||
description: filter the songs by genre
|
||||
post:
|
||||
/{songId}:
|
||||
get:
|
||||
responses:
|
||||
200:
|
||||
body:
|
||||
application/json:
|
||||
schema: |
|
||||
{ "$schema": "http://json-schema.org/schema",
|
||||
"type": "object",
|
||||
"description": "A canonical song",
|
||||
"properties": {
|
||||
"title": { "type": "string" },
|
||||
"artist": { "type": "string" }
|
||||
},
|
||||
"required": [ "title", "artist" ]
|
||||
}
|
||||
application/xml:
|
||||
delete:
|
||||
description: |
|
||||
This method will *delete* an **individual song**
|
||||
89
samples/XML/some-ideas.mm
Normal file
89
samples/XML/some-ideas.mm
Normal file
@@ -0,0 +1,89 @@
|
||||
<map version="0.9.0">
|
||||
<!-- To view this file, download free mind mapping software FreeMind from http://freemind.sourceforge.net -->
|
||||
<node COLOR="#000000" CREATED="1385819664217" ID="ID_1105859543" MODIFIED="1385820134114" TEXT="Some ideas on demexp">
|
||||
<font NAME="SansSerif" SIZE="20"/>
|
||||
<hook NAME="accessories/plugins/AutomaticLayout.properties"/>
|
||||
<node COLOR="#0033ff" CREATED="1385819753503" ID="ID_1407588370" MODIFIED="1385819767173" POSITION="right" TEXT="User Interface">
|
||||
<edge STYLE="sharp_bezier" WIDTH="8"/>
|
||||
<font NAME="SansSerif" SIZE="18"/>
|
||||
<node COLOR="#00b439" CREATED="1385819771320" ID="ID_1257512743" MODIFIED="1385819783131" TEXT="Text file">
|
||||
<edge STYLE="bezier" WIDTH="thin"/>
|
||||
<font NAME="SansSerif" SIZE="16"/>
|
||||
</node>
|
||||
<node COLOR="#00b439" CREATED="1385819783831" ID="ID_997633499" MODIFIED="1385819786761" TEXT="Web">
|
||||
<edge STYLE="bezier" WIDTH="thin"/>
|
||||
<font NAME="SansSerif" SIZE="16"/>
|
||||
</node>
|
||||
<node COLOR="#00b439" CREATED="1385819787041" ID="ID_204106158" MODIFIED="1385819794885" TEXT="Graphical interface">
|
||||
<edge STYLE="bezier" WIDTH="thin"/>
|
||||
<font NAME="SansSerif" SIZE="16"/>
|
||||
</node>
|
||||
<node COLOR="#00b439" CREATED="1385819795339" ID="ID_768498137" MODIFIED="1385819800338" TEXT="Email">
|
||||
<edge STYLE="bezier" WIDTH="thin"/>
|
||||
<font NAME="SansSerif" SIZE="16"/>
|
||||
</node>
|
||||
<node COLOR="#00b439" CREATED="1385819801043" ID="ID_1660630451" MODIFIED="1385819802441" TEXT="SMS">
|
||||
<edge STYLE="bezier" WIDTH="thin"/>
|
||||
<font NAME="SansSerif" SIZE="16"/>
|
||||
</node>
|
||||
</node>
|
||||
<node COLOR="#0033ff" CREATED="1385819872899" ID="ID_281388957" MODIFIED="1385819878444" POSITION="left" TEXT="Cordorcet voting module">
|
||||
<edge STYLE="sharp_bezier" WIDTH="8"/>
|
||||
<font NAME="SansSerif" SIZE="18"/>
|
||||
<node COLOR="#00b439" CREATED="1385819880540" ID="ID_1389666909" MODIFIED="1385819948101" TEXT="Input">
|
||||
<edge STYLE="bezier" WIDTH="thin"/>
|
||||
<font NAME="SansSerif" SIZE="16"/>
|
||||
<node COLOR="#990000" CREATED="1385819893834" ID="ID_631111389" MODIFIED="1385819901697" TEXT="Number of votes">
|
||||
<font NAME="SansSerif" SIZE="14"/>
|
||||
</node>
|
||||
<node COLOR="#990000" CREATED="1385819902442" ID="ID_838201093" MODIFIED="1385819910452" TEXT="Number of possible choices">
|
||||
<font NAME="SansSerif" SIZE="14"/>
|
||||
</node>
|
||||
<node COLOR="#990000" CREATED="1385819910703" ID="ID_1662888975" MODIFIED="1385819933316" TEXT="For a vote: number of votes and list of choices">
|
||||
<font NAME="SansSerif" SIZE="14"/>
|
||||
</node>
|
||||
</node>
|
||||
<node COLOR="#00b439" CREATED="1385819949027" ID="ID_1504837261" MODIFIED="1385819952198" TEXT="Format">
|
||||
<edge STYLE="bezier" WIDTH="thin"/>
|
||||
<font NAME="SansSerif" SIZE="16"/>
|
||||
<node COLOR="#990000" CREATED="1385819955105" ID="ID_647722151" MODIFIED="1385819962151" TEXT="A single file?">
|
||||
<font NAME="SansSerif" SIZE="14"/>
|
||||
</node>
|
||||
<node COLOR="#990000" CREATED="1385819962642" ID="ID_1374756253" MODIFIED="1385819976939" TEXT="Several files (parameters + 1 per vote)?">
|
||||
<font NAME="SansSerif" SIZE="14"/>
|
||||
</node>
|
||||
<node COLOR="#990000" CREATED="1385819977578" ID="ID_979556559" MODIFIED="1385819984309" TEXT="JSON?">
|
||||
<font NAME="SansSerif" SIZE="14"/>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
<node COLOR="#0033ff" CREATED="1385820005408" ID="ID_1886566753" MODIFIED="1385820009909" POSITION="right" TEXT="Technologies">
|
||||
<edge STYLE="sharp_bezier" WIDTH="8"/>
|
||||
<font NAME="SansSerif" SIZE="18"/>
|
||||
<node COLOR="#00b439" CREATED="1385820011913" ID="ID_1291489552" MODIFIED="1385820014698" TEXT="SPARK 2014">
|
||||
<edge STYLE="bezier" WIDTH="thin"/>
|
||||
<font NAME="SansSerif" SIZE="16"/>
|
||||
</node>
|
||||
<node COLOR="#00b439" CREATED="1385820015481" ID="ID_1825929484" MODIFIED="1385820017935" TEXT="Frama-C">
|
||||
<edge STYLE="bezier" WIDTH="thin"/>
|
||||
<font NAME="SansSerif" SIZE="16"/>
|
||||
</node>
|
||||
<node COLOR="#00b439" CREATED="1385820018603" ID="ID_253774957" MODIFIED="1385820027363" TEXT="Why3 -> OCaml">
|
||||
<edge STYLE="bezier" WIDTH="thin"/>
|
||||
<font NAME="SansSerif" SIZE="16"/>
|
||||
</node>
|
||||
</node>
|
||||
<node COLOR="#0033ff" CREATED="1385820136808" ID="ID_1002115371" MODIFIED="1385820139813" POSITION="left" TEXT="Vote storage">
|
||||
<edge STYLE="sharp_bezier" WIDTH="8"/>
|
||||
<font NAME="SansSerif" SIZE="18"/>
|
||||
<node COLOR="#00b439" CREATED="1385820141400" ID="ID_1882609124" MODIFIED="1385820145261" TEXT="Database">
|
||||
<edge STYLE="bezier" WIDTH="thin"/>
|
||||
<font NAME="SansSerif" SIZE="16"/>
|
||||
</node>
|
||||
<node COLOR="#00b439" CREATED="1385820146138" ID="ID_1771403777" MODIFIED="1385820154334" TEXT="Text file (XML?)">
|
||||
<edge STYLE="bezier" WIDTH="thin"/>
|
||||
<font NAME="SansSerif" SIZE="16"/>
|
||||
</node>
|
||||
</node>
|
||||
</node>
|
||||
</map>
|
||||
@@ -11,6 +11,11 @@ class TestHeuristcs < Test::Unit::TestCase
|
||||
File.read(File.join(samples_path, name))
|
||||
end
|
||||
|
||||
def file_blob(name)
|
||||
path = File.exist?(name) ? name : File.join(samples_path, name)
|
||||
FileBlob.new(path)
|
||||
end
|
||||
|
||||
def all_fixtures(language_name, file="*")
|
||||
Dir.glob("#{samples_path}/#{language_name}/#{file}")
|
||||
end
|
||||
@@ -18,16 +23,17 @@ class TestHeuristcs < Test::Unit::TestCase
|
||||
# Candidate languages = ["C++", "Objective-C"]
|
||||
def test_obj_c_by_heuristics
|
||||
# Only calling out '.h' filenames as these are the ones causing issues
|
||||
all_fixtures("Objective-C", "*.h").each do |fixture|
|
||||
results = Heuristics.disambiguate_c(fixture("Objective-C/#{File.basename(fixture)}"))
|
||||
assert_equal Language["Objective-C"], results.first
|
||||
end
|
||||
assert_heuristics({
|
||||
"Objective-C" => all_fixtures("Objective-C", "*.h"),
|
||||
"C++" => ["C++/render_adapter.cpp", "C++/ThreadedQueue.h"],
|
||||
"C" => nil
|
||||
})
|
||||
end
|
||||
|
||||
# Candidate languages = ["C++", "Objective-C"]
|
||||
def test_cpp_by_heuristics
|
||||
results = Heuristics.disambiguate_c(fixture("C++/render_adapter.cpp"))
|
||||
assert_equal Language["C++"], results.first
|
||||
def test_c_by_heuristics
|
||||
languages = [Language["C++"], Language["Objective-C"], Language["C"]]
|
||||
results = Heuristics.call(file_blob("C/ArrowLeft.h"), languages)
|
||||
assert_equal [], results
|
||||
end
|
||||
|
||||
def test_detect_still_works_if_nothing_matches
|
||||
@@ -37,103 +43,82 @@ class TestHeuristcs < Test::Unit::TestCase
|
||||
end
|
||||
|
||||
# Candidate languages = ["Perl", "Prolog"]
|
||||
def test_pl_prolog_by_heuristics
|
||||
results = Heuristics.disambiguate_pl(fixture("Prolog/turing.pl"))
|
||||
assert_equal Language["Prolog"], results.first
|
||||
end
|
||||
|
||||
# Candidate languages = ["Perl", "Prolog"]
|
||||
def test_pl_perl_by_heuristics
|
||||
results = Heuristics.disambiguate_pl(fixture("Perl/perl-test.t"))
|
||||
assert_equal Language["Perl"], results.first
|
||||
def test_pl_prolog_perl_by_heuristics
|
||||
assert_heuristics({
|
||||
"Prolog" => "Prolog/turing.pl",
|
||||
"Perl" => "Perl/perl-test.t",
|
||||
})
|
||||
end
|
||||
|
||||
# Candidate languages = ["ECL", "Prolog"]
|
||||
def test_ecl_prolog_by_heuristics
|
||||
results = Heuristics.disambiguate_ecl(fixture("Prolog/or-constraint.ecl"))
|
||||
assert_equal Language["Prolog"], results.first
|
||||
end
|
||||
|
||||
# Candidate languages = ["ECL", "Prolog"]
|
||||
def test_ecl_ecl_by_heuristics
|
||||
results = Heuristics.disambiguate_ecl(fixture("ECL/sample.ecl"))
|
||||
assert_equal Language["ECL"], results.first
|
||||
assert_heuristics({
|
||||
"ECL" => "ECL/sample.ecl",
|
||||
"Prolog" => "Prolog/or-constraint.ecl"
|
||||
})
|
||||
end
|
||||
|
||||
# Candidate languages = ["IDL", "Prolog"]
|
||||
def test_pro_prolog_by_heuristics
|
||||
results = Heuristics.disambiguate_pro(fixture("Prolog/logic-problem.pro"))
|
||||
assert_equal Language["Prolog"], results.first
|
||||
end
|
||||
|
||||
# Candidate languages = ["IDL", "Prolog"]
|
||||
def test_pro_idl_by_heuristics
|
||||
results = Heuristics.disambiguate_pro(fixture("IDL/mg_acosh.pro"))
|
||||
assert_equal Language["IDL"], results.first
|
||||
def test_pro_prolog_idl_by_heuristics
|
||||
assert_heuristics({
|
||||
"Prolog" => "Prolog/logic-problem.pro",
|
||||
"IDL" => "IDL/mg_acosh.pro"
|
||||
})
|
||||
end
|
||||
|
||||
# Candidate languages = ["AGS Script", "AsciiDoc"]
|
||||
def test_asc_asciidoc_by_heuristics
|
||||
results = Heuristics.disambiguate_asc(fixture("AsciiDoc/list.asc"))
|
||||
assert_equal Language["AsciiDoc"], results.first
|
||||
end
|
||||
|
||||
# Candidate languages = ["TypeScript", "XML"]
|
||||
def test_ts_typescript_by_heuristics
|
||||
results = Heuristics.disambiguate_ts(fixture("TypeScript/classes.ts"))
|
||||
assert_equal Language["TypeScript"], results.first
|
||||
end
|
||||
|
||||
# Candidate languages = ["TypeScript", "XML"]
|
||||
def test_ts_xml_by_heuristics
|
||||
results = Heuristics.disambiguate_ts(fixture("XML/pt_BR.xml"))
|
||||
assert_equal Language["XML"], results.first
|
||||
assert_heuristics({
|
||||
"AsciiDoc" => "AsciiDoc/list.asc",
|
||||
"AGS Script" => nil
|
||||
})
|
||||
end
|
||||
|
||||
def test_cl_by_heuristics
|
||||
languages = ["Common Lisp", "OpenCL"]
|
||||
languages.each do |language|
|
||||
all_fixtures(language).each do |fixture|
|
||||
results = Heuristics.disambiguate_cl(fixture("#{language}/#{File.basename(fixture)}"))
|
||||
assert_equal Language[language], results.first
|
||||
end
|
||||
end
|
||||
assert_heuristics({
|
||||
"Common Lisp" => all_fixtures("Common Lisp"),
|
||||
"OpenCL" => all_fixtures("OpenCL")
|
||||
})
|
||||
end
|
||||
|
||||
def test_f_by_heuristics
|
||||
languages = ["FORTRAN", "Forth"]
|
||||
languages.each do |language|
|
||||
all_fixtures(language).each do |fixture|
|
||||
results = Heuristics.disambiguate_f(fixture("#{language}/#{File.basename(fixture)}"))
|
||||
assert_equal Language[language], results.first
|
||||
end
|
||||
end
|
||||
assert_heuristics({
|
||||
"FORTRAN" => all_fixtures("FORTRAN"),
|
||||
"Forth" => all_fixtures("Forth")
|
||||
})
|
||||
end
|
||||
|
||||
# Candidate languages = ["Hack", "PHP"]
|
||||
def test_hack_by_heuristics
|
||||
results = Heuristics.disambiguate_hack(fixture("Hack/funs.php"))
|
||||
assert_equal Language["Hack"], results.first
|
||||
assert_heuristics({
|
||||
"Hack" => "Hack/funs.php",
|
||||
"PHP" => "PHP/Model.php"
|
||||
})
|
||||
end
|
||||
|
||||
# Candidate languages = ["Scala", "SuperCollider"]
|
||||
def test_sc_supercollider_by_heuristics
|
||||
results = Heuristics.disambiguate_sc(fixture("SuperCollider/WarpPreset.sc"))
|
||||
assert_equal Language["SuperCollider"], results.first
|
||||
end
|
||||
|
||||
# Candidate languages = ["Scala", "SuperCollider"]
|
||||
def test_sc_scala_by_heuristics
|
||||
results = Heuristics.disambiguate_sc(fixture("Scala/node11.sc"))
|
||||
assert_equal Language["Scala"], results.first
|
||||
def test_sc_supercollider_scala_by_heuristics
|
||||
assert_heuristics({
|
||||
"SuperCollider" => "SuperCollider/WarpPreset.sc",
|
||||
"Scala" => "Scala/node11.sc"
|
||||
})
|
||||
end
|
||||
|
||||
def test_fs_by_heuristics
|
||||
languages = ["F#", "Forth", "GLSL"]
|
||||
languages.each do |language|
|
||||
all_fixtures(language).each do |fixture|
|
||||
results = Heuristics.disambiguate_fs(fixture("#{language}/#{File.basename(fixture)}"))
|
||||
assert_equal Language[language], results.first
|
||||
assert_heuristics({
|
||||
"F#" => all_fixtures("F#"),
|
||||
"Forth" => all_fixtures("Forth"),
|
||||
"GLSL" => all_fixtures("GLSL")
|
||||
})
|
||||
end
|
||||
|
||||
def assert_heuristics(hash)
|
||||
candidates = hash.keys.map { |l| Language[l] }
|
||||
|
||||
hash.each do |language, blobs|
|
||||
Array(blobs).each do |blob|
|
||||
result = Heuristics.call(file_blob(blob), candidates)
|
||||
assert_equal [Language[language]], result
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -223,34 +223,21 @@ class TestLanguage < Test::Unit::TestCase
|
||||
assert_equal [Language['Chapel']], Language.find_by_filename('examples/hello.chpl')
|
||||
end
|
||||
|
||||
def test_find_by_shebang
|
||||
assert_equal 'ruby', Linguist.interpreter_from_shebang("#!/usr/bin/ruby\n# baz")
|
||||
{ [] => ["",
|
||||
"foo",
|
||||
"#bar",
|
||||
"#baz",
|
||||
"///",
|
||||
"\n\n\n\n\n",
|
||||
" #!/usr/sbin/ruby",
|
||||
"\n#!/usr/sbin/ruby"],
|
||||
['Ruby'] => ["#!/usr/bin/env ruby\n# baz",
|
||||
"#!/usr/sbin/ruby\n# bar",
|
||||
"#!/usr/bin/ruby\n# foo",
|
||||
"#!/usr/sbin/ruby",
|
||||
"#!/usr/sbin/ruby foo bar baz\n"],
|
||||
['R'] => ["#!/usr/bin/env Rscript\n# example R script\n#\n"],
|
||||
['Shell'] => ["#!/usr/bin/bash\n", "#!/bin/sh"],
|
||||
['Python'] => ["#!/bin/python\n# foo\n# bar\n# baz",
|
||||
"#!/usr/bin/python2.7\n\n\n\n",
|
||||
"#!/usr/bin/python3\n\n\n\n"],
|
||||
["Common Lisp"] => ["#!/usr/bin/sbcl --script\n\n"]
|
||||
}.each do |languages, bodies|
|
||||
bodies.each do |body|
|
||||
assert_equal([body, languages.map{|l| Language[l]}],
|
||||
[body, Language.find_by_shebang(body)])
|
||||
|
||||
end
|
||||
def test_find_by_interpreter
|
||||
{
|
||||
"ruby" => "Ruby",
|
||||
"Rscript" => "R",
|
||||
"sh" => "Shell",
|
||||
"bash" => "Shell",
|
||||
"python" => "Python",
|
||||
"python2" => "Python",
|
||||
"python3" => "Python",
|
||||
"sbcl" => "Common Lisp"
|
||||
}.each do |interpreter, language|
|
||||
assert_equal [Language[language]], Language.find_by_interpreter(interpreter)
|
||||
end
|
||||
|
||||
assert_equal [], Language.find_by_interpreter(nil)
|
||||
end
|
||||
|
||||
def test_find
|
||||
|
||||
@@ -82,9 +82,4 @@ class TestSamples < Test::Unit::TestCase
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def test_shebang
|
||||
assert_equal "crystal", Linguist.interpreter_from_shebang("#!/usr/bin/env bin/crystal")
|
||||
assert_equal "python2", Linguist.interpreter_from_shebang("#!/usr/bin/python2.4")
|
||||
end
|
||||
end
|
||||
|
||||
38
test/test_shebang.rb
Normal file
38
test/test_shebang.rb
Normal file
@@ -0,0 +1,38 @@
|
||||
require_relative "./helper"
|
||||
|
||||
class TestShebang < Test::Unit::TestCase
|
||||
include Linguist
|
||||
|
||||
def assert_interpreter(interpreter, body)
|
||||
assert_equal interpreter, Shebang.interpreter(body)
|
||||
end
|
||||
|
||||
def test_shebangs
|
||||
assert_interpreter nil, ""
|
||||
assert_interpreter nil, "foo"
|
||||
assert_interpreter nil, "#bar"
|
||||
assert_interpreter nil, "#baz"
|
||||
assert_interpreter nil, "///"
|
||||
assert_interpreter nil, "\n\n\n\n\n"
|
||||
assert_interpreter nil, " #!/usr/sbin/ruby"
|
||||
assert_interpreter nil, "\n#!/usr/sbin/ruby"
|
||||
|
||||
assert_interpreter "ruby", "#!/usr/sbin/ruby\n# bar"
|
||||
assert_interpreter "ruby", "#!/usr/bin/ruby\n# foo"
|
||||
assert_interpreter "ruby", "#!/usr/sbin/ruby"
|
||||
assert_interpreter "ruby", "#!/usr/sbin/ruby foo bar baz\n"
|
||||
|
||||
assert_interpreter "Rscript", "#!/usr/bin/env Rscript\n# example R script\n#\n"
|
||||
assert_interpreter "crystal", "#!/usr/bin/env bin/crystal"
|
||||
assert_interpreter "ruby", "#!/usr/bin/env ruby\n# baz"
|
||||
|
||||
assert_interpreter "bash", "#!/usr/bin/bash\n"
|
||||
assert_interpreter "sh", "#!/bin/sh"
|
||||
assert_interpreter "python", "#!/bin/python\n# foo\n# bar\n# baz"
|
||||
assert_interpreter "python2", "#!/usr/bin/python2.7\n\n\n\n"
|
||||
assert_interpreter "python3", "#!/usr/bin/python3\n\n\n\n"
|
||||
assert_interpreter "sbcl", "#!/usr/bin/sbcl --script\n\n"
|
||||
assert_interpreter "perl", "#! perl"
|
||||
end
|
||||
|
||||
end
|
||||
BIN
vendor/cache/yajl-ruby-1.1.0.gem
vendored
Normal file
BIN
vendor/cache/yajl-ruby-1.1.0.gem
vendored
Normal file
Binary file not shown.
BIN
vendor/cache/yajl-ruby-1.2.1.gem
vendored
BIN
vendor/cache/yajl-ruby-1.2.1.gem
vendored
Binary file not shown.
Reference in New Issue
Block a user